我們今天就來看看一個比較簡單的會畫圖形,也就是我們常用的paint的方法。那麼就不多說什麼了還是來看看代碼吧:
Java代碼:
- package eoe.demo;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.view.VIEw;
- public class MyView extends VIEw {
- private Paint paint ;
- public MyVIEw(Context context) {
- super(context);
- paint = new Paint();
- paint.setAntiAlias(true);//設置畫筆無鋸齒(如果不設置可以看到效果很差)
- this.setKeepScreenOn(true);//設置背景常亮
- paint.setColor(Color.RED);
- }
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(Color.WHITE);//設置刷屏顏色
- Rect rect = new Rect(30,30,50,50); //這裡最後兩個參數不是寬高、而是矩形右下角的坐標
- canvas.drawRect(rect, paint);
- RectF rectF = new RectF(70f,30f,90f,90f);//RectF 只是矩形 float形式 只是跟Rect精確度不一樣
- canvas.drawArc(rectF, 0, 360, true, paint);
- canvas.drawCircle(150, 30, 20, paint);//這也是畫圓 第三個參數為半徑
- float[] points =new float[]{200f,10f,200f,40f,300f,30f,400f,70f};
- canvas.drawLines(points, paint);
- // canvas.drawLines(points, 1, 4, paint);//選取特定點數組中兩點來畫出一條直線
- canvas.drawText("Himi", 230, 30, paint);
- }
- }