我有以下手指画代码。问题是我需要为其添加背景图片。我尝试将它添加到位图中,但如果我运行擦除功能,它将清除背景图像。
这是代码:
public class MainActivity extends Activity {
private Paint mPaint, mBitmapPaint;
private Path mPath;
private Canvas mCanvas;
private Bitmap mBitmap;
public MyView myView;
int screenheight;
int screenwidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
myView = new MyView( this );
myView.setDrawingCacheEnabled(true);
setContentView( myView );
mPaint = new Paint();
mPaint.setAntiAlias( true );
mPaint.setColor( Color.BLACK );
mPaint.setStyle( Paint.Style.STROKE );
mPaint.setStrokeJoin( Paint.Join.ROUND );
mPaint.setStrokeCap( Paint.Cap.ROUND );
mPaint.setStrokeWidth(3);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
DisplayMetrics displaymetrics = new DisplayMetrics();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenheight = displaymetrics.heightPixels;
screenwidth = displaymetrics.widthPixels;
mBitmap = Bitmap.createBitmap(screenwidth, screenheight, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas( mBitmap );
mPath = new Path();
mBitmapPaint = new Paint( Paint.DITHER_FLAG );
mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(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 touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath( mPath, mPaint );
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
private static final int SAVE_MENU_ID = Menu.FIRST;
private static final int ERASE_MENU_ID = Menu.FIRST + 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, SAVE_MENU_ID, 0, "Save");
menu.add(0, ERASE_MENU_ID, 0, "Erase");
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
mPaint.setXfermode(null);
switch (item.getItemId()) {
case SAVE_MENU_ID:
return true;
case ERASE_MENU_ID:
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
myView.invalidate();
}
return super.onOptionsItemSelected(item);
}
}
请就如何在位图下方添加背景图像(要在其上绘制)提出建议,而不会被位图阻塞。