• 几何图形的绘制:


      在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);   }  }

    }

  • 相关阅读:
    gitlab的数据库磁盘坏了已经没有办法恢复情况下如何恢复git上的代码
    psql: FATAL: the database system is in recovery mode
    k8s 下 jenkins 分布式部署:利用pipeline动态增加slave节点
    pipeline 流水线:持续部署(docker)-企业微信群通知消息
    查看私有仓库镜像的版本列表
    MyBatis与Hibernate比较
    MyBatis与JDBC的对比
    Java_Ant详解(转载)
    IntelliJ Idea 常用快捷键列表
    隔行换色
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302429.html
Copyright © 2020-2023  润新知