• 几何图形的绘制:


      在Android中还可以绘制几何图形:

    下面我们先来看几个方法:

      drawRect:绘制矩形

      drawCircle:绘制圆

      drawOval:绘制椭圆

      drawPath:绘制任意多边形

      drawLine:绘制直线

      drawPoint:绘制点

    下面通过一个实例解释怎么使用这些方法

    package com.example.kutuke;

    import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.util.Log; import android.view.View;

    public class MyGameView extends View implements Runnable{    public Paint paint = null;  public MyGameView02  mgv02 = null;    public MyGameView(Context context){   super(context);   paint = new Paint();   mgv02 = new MyGameView02(context);   new Thread(this).start();  }    public void run(){   while(!Thread.currentThread().isInterrupted()){    try{     Thread.sleep(100);    }catch(InterruptedException e){     Thread.currentThread().interrupt();    }    postInvalidate();   }  }    public void onDraw(Canvas canvas){   super.onDraw(canvas);   //设置画笔为空心   paint.setStyle(Style.STROKE);   canvas.drawColor(Color.BLACK);   {    //定义矩形对象    Rect rect = new Rect();    rect.left = 10;    rect.top = 10;    rect.right = 110;    rect.bottom = 110;        paint.setColor(Color.RED);    canvas.drawRect(rect, paint);        canvas.drawCircle(250, 60, 50, paint);        //定义椭圆对象    RectF rectF = new RectF();    rectF.left = (getWidth()-220);    rectF.top = (10);    rectF.right = (getWidth()-20);    rectF.bottom = (160);    paint.setColor(Color.YELLOW);    //绘制椭圆    canvas.drawOval(rectF, paint);        //绘制多边形    Path path = new Path();    //设置多边形的点    path.moveTo(0, 150);    path.lineTo(150, 150);    path.lineTo(90, 250);    path.lineTo(50, 250);    path.close();    //绘制多边形    paint.setColor(Color.BLUE);    canvas.drawPath(path, paint);    //绘制直线    canvas.drawLine(0, 255, getWidth(), 255, paint);   }  }

    }

  • 相关阅读:
    spring_three
    报错:java.sql.SQLException: The server
    Spring_two
    Spring_One
    Mybatis中的collection和association一关系
    Mybatis_three
    文件操作1
    面向对象编程三大特征7
    面向对象编程三大特征6
    面向对象编程三大特征5
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302429.html
Copyright © 2020-2023  润新知