From:http://blog.csdn.net/androiddevelop/article/details/8267312
一、Canvas第一种使用方式
最简单的使用Canvas, 画一个方形,一行字。 下面是效果图
- public class CustomView extends View {
- private Paint mPaint;
- public CustomView(Context context) {
- super(context);
- }
- public CustomView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mPaint = new Paint();
- mPaint.setColor(Color.GREEN);
- mPaint.setTextSize(36);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- mPaint.setStyle(Style.FILL); //设置填充
- canvas.drawRect(10, 10, 200, 200, mPaint); //绘制矩形
- mPaint.setColor(Color.BLUE);
- canvas.drawText("我不是自定义View", 10, 120, mPaint);
- }
public class CustomView extends View {
private Paint mPaint;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setTextSize(36);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Style.FILL); //设置填充
canvas.drawRect(10, 10, 200, 200, mPaint); //绘制矩形
mPaint.setColor(Color.BLUE);
canvas.drawText("我不是自定义View", 10, 120, mPaint);
}
参考资料:
Android Graphic : apk and Skia/OpenGL|ES
二、Canvas第二种使用方式
Canvas的使用通常只有两种形式:
1. 在已有的Canvas上绘制图形或文字,通过VIew.onDraw回调获取Canvas对象。Canvas第一种使用方式
2. 图形或文字直接绘制到View上(本文使用此种方式)
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Bitmap b =Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
- Canvas canvas =new Canvas(b);
- Paint paint = new Paint();
- paint.setColor(Color.BLUE);
- canvas.drawText("love_world_", 20, 20, paint);
- ImageView imageView = (ImageView) findViewById(R.id.image_view);
- BitmapDrawable bitmapDrawable = new BitmapDrawable(b);
- imageView.setBackgroundDrawable(bitmapDrawable);
- }
- }
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap b =Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas =new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawText("love_world_", 20, 20, paint);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
BitmapDrawable bitmapDrawable = new BitmapDrawable(b);
imageView.setBackgroundDrawable(bitmapDrawable);
}
}
效果图
Android入门第十四篇之画图
http://blog.csdn.net/hellogv/article/category/761980
Andriod中绘(画)图----Canvas的使用详解
http://blog.csdn.net/qinjuning/article/details/6936783
Android图像处理之Bitmap类
http://blog.csdn.net/thl789/article/details/6762030
Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
http://blog.csdn.net/rhljiayou/article/details/7212620
画了个Android——Canvas类的使用
http://blog.sina.com.cn/s/blog_61ef49250100qw9x.html
Android Canvas rotate
http://verydemo.com/demo_c131_i3507.html
Android入门第十四篇之画图
http://blog.csdn.net/hellogv/article/category/761980
Andriod中绘(画)图----Canvas的使用详解
http://blog.csdn.net/qinjuning/article/details/6936783
Android图像处理之Bitmap类
http://blog.csdn.net/thl789/article/details/6762030
Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
http://blog.csdn.net/rhljiayou/article/details/7212620
画了个Android——Canvas类的使用
http://blog.sina.com.cn/s/blog_61ef49250100qw9x.html
Android Canvas rotate