zoukankan      html  css  js  c++  java
  • 几何图形的绘制:

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

    }

  • 相关阅读:
    [题解] LuoguP6185 [NOI Online 提高组]冒泡排序
    [题解] LuoguP5339 [TJOI2019]唱、跳、rap和篮球
    [题解] LuoguP4168 [Violet]蒲公英
    [题解] LuoguP4705玩游戏
    [题解 LuoguP4491 [HAOI2018]染色
    [题解] LuoguP3768 简单的数学题
    杜教筛
    莫比乌斯反演学习笔记
    [题解] LuoguP2257 YY的GCD
    [题解] LuoguP2764 最小路径覆盖问题
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302429.html
Copyright © 2011-2022 走看看