这是自定义的一个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);
}

解决方案 »

  1.   

    Canvas进行new的时候不要直接用Bitmap,而是等new好了以后在drawBitmap,这样就能控制它的位置了。清除的话直接 drawColor成你的背景色
      

  2.   

    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);
    这段代码和onDraw方法里面该怎么改,能说下吗?
      

  3.   

    能具体说下吗?
    @Override
    protected void onDraw(Canvas canvas) {
    canvas.drawColor(0xFFFFFFFF);// set background color
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
    canvas.drawPath(mPath, mPaint);
    }
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint)是改这里么?把0,0设大一点是吧?担是这样的话涂鸦的时候画完后也就跟把把画的东西x,y轴上加了相应的值。