Android Canvas.drawBitmap() 使用教程:绘制位图详解
Canvas.drawBitmap() 是 Android Canvas 类中的一个重要方法,用于在 Canvas 上绘制位图。
使用方法如下:
- 创建 Bitmap 对象
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
- 绘制 Bitmap
canvas.drawBitmap(bitmap, x, y, null);
其中 x 和 y 是位图的左上角坐标,null 表示不使用 Paint 对象。
使用 Paint 对象
如果您需要使用 Paint 对象进行绘制,可以这样做:
Paint paint = new Paint();
paint.setAntiAlias(true); // 设置抗锯齿
canvas.drawBitmap(bitmap, x, y, paint);
完整示例代码:
public class MyView extends View {
private Bitmap mBitmap;
private Paint mPaint;
public MyView(Context context) {
super(context);
// 创建 Bitmap 对象
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// 创建 Paint 对象
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制 Bitmap
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}
}
通过以上代码,您可以在 Android 应用中使用 Canvas.drawBitmap() 方法轻松绘制位图。
原文地址: http://www.cveoy.top/t/topic/gSAW 著作权归作者所有。请勿转载和采集!