当前位置: 代码迷 >> Android >> Canvas与Bit地图画图的有关问题
  详细解决方案

Canvas与Bit地图画图的有关问题

热度:41   发布时间:2016-05-01 13:28:59.0
Canvas与Bitmap画图的问题
这是自定义的一个View.做为涂鸦用的,两个问题,1.怎么把mBitmap放到画板中间,并且整个画板都可以执行画的操作,因为目前来说R.drawable.bg在左上角并且只能在图片上做画的操作,空白处不能。2.另一个是怎么作清除画板上的已经画的东西和撤销功能。知道的请说下,成分感谢
代码如下:
public class CanvasView extends View {

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public CanvasView(Context c) {
super(c);
mPaint = new Paint();

mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg).copy(Bitmap.Config.ARGB_8888, true);
// mBitmap = Bitmap.createBitmap(300, 400, Bitmap.Config.ARGB_8888);
// mBitmap = Bitmap.createBitmap(mBitmap);
//Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, 300, 400);

mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xff000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFFFFFFF);// set background color

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawPath(mPath, mPaint);
}


------解决方案--------------------
Canvas进行new的时候不要直接用Bitmap,而是等new好了以后在drawBitmap,这样就能控制它的位置了。

清除的话直接 drawColor成你的背景色
------解决方案--------------------
使用 canvas.DrawBitmap()