1

我想用手指在图像上涂鸦(在图像上而不是在白色背景上),我正在使用此代码在图像上绘图。它在图像上绘制,但是当我释放触摸时,绘制消失了。我认为问题出在板条箱画布上。还有我加载的图像很大,所以只显示了一部分图像。那么我如何在屏幕上拉伸和调整整个图像以便在其上绘图。

公共类 MyView 扩展视图 {

private ImageView image;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
private float mX, mY;
private final float TOUCH_TOLERANCE = 4;

public MyView(Context c,View v) {
    super(c);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(0XFF000000);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);

    image=(ImageView) v.findViewById(R.id.my_image);
}

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

    // Scribbling on white background
//  mBitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
//  mCanvas =new Canvas(mBitmap);

    // Scribbling on image
    mBitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.batman);
    mBitmap= Bitmap.createBitmap( mBitmap.getWidth() ,mBitmap.getHeight(), mBitmap.getConfig());
    mCanvas =new Canvas(mBitmap.copy(Bitmap.Config.ARGB_8888, true));
}

@Override
public void onDraw(Canvas c) {
    //  c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);

    c.drawBitmap(mBitmap, 0, 0,mBitmapPaint);
    c.drawPath(mPath, mPaint);
}

@Override
public boolean onTouchEvent(MotionEvent e) {
    float x=e.getX();
    float y=e.getY();

    switch(e.getAction()){
    case MotionEvent.ACTION_DOWN:
        touchStart(x,y);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touchMove(x,y);
        this.invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touchUp(x,y);
        invalidate();
    }

    return true;
}

private void touchUp(float x, float y) {
    mPath.lineTo(mX, mY);
    mCanvas.drawPath(mPath, mPaint);
    mPath.reset(); 
}

private void touchMove(float x, float y) {
    float dx= Math.abs(x-mX);
    float dy= Math.abs(y-mY);
    if(dx>=TOUCH_TOLERANCE && dy>=TOUCH_TOLERANCE){
        mPath.quadTo(mX, mY, (x+mX)/2, (y+mY)/2);
        mX=x;
        mY=y;
    }
}

private void touchStart(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX=x;
    mY=y;
}

}

请帮忙

4

0 回答 0