参考:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=278237
自定义View 重写onDraw方法
package com.example.draw; import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View; public class MyView extends View { public MyView(Context context) {
super(context); }
@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint();
paint.setColor(Color.RED);
canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
canvas.drawLine(getWidth(), 0, 0, getHeight(), paint);
canvas.drawText("Hello World", 10, 3, paint);
canvas.drawCircle(0,0, 50, paint);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher), 0, 120, paint); } }
在Activity中设置自定义的View : setContentView(new MyView(this));
效果图:
注意事项:
canvas.drawText("Hello World", 10, 3, paint); 注意drawText依据的基准线!
获取一个Bitmap的方法 : BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)
获取屏幕的宽高 android.view.View.getWidth() 、 android.view.View.getHeight()