Paint与Canvas的简单用法

 

 

参考:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=278237

 

自定义View 重写onDraw方法

 1 package com.example.draw;
 2 
 3 import android.content.Context;
 4 import android.graphics.BitmapFactory;
 5 import android.graphics.Canvas;
 6 import android.graphics.Color;
 7 import android.graphics.Paint;
 8 import android.view.View;
 9 
10  
11 
12 public class MyView extends View  {
13 
14     
15     public MyView(Context context) {
16         super(context);
17          
18     }
19     @Override
20     protected void onDraw(Canvas canvas) {
21      Paint paint=new Paint();
22      paint.setColor(Color.RED);
23      canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
24      canvas.drawLine(getWidth(), 0, 0, getHeight(), paint);
25      canvas.drawText("Hello World", 10, 3, paint);
26      canvas.drawCircle(0,0, 50, paint);
27      canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher), 0, 120, paint);
28     
29     }
30 
31 }

 

在Activity中设置自定义的View  :  setContentView(new MyView(this));


效果图:

  

          Paint与Canvas的简单用法

注意事项:

    canvas.drawText("Hello World", 10, 3, paint);    注意drawText依据的基准线!

   获取一个Bitmap的方法 : BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)

   获取屏幕的宽高  android.view.View.getWidth()  、 android.view.View.getHeight()

    

 

 

 

你可能感兴趣的:(canvas)