我正在尝试开发一个应用程序。我现在想清除画布,我编写了清除屏幕的代码。它的工作..但是一个问题背景图像也被清除了。我只想清除图纸。如何只清除图纸?在这里,我动态设置背景图像。
package com.example.graphicsdraw;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
private Button Back, Next, Erase,clrScreen;
int FlagSet = 0, ImageSet = 0;
static int Flag = 0;
MyView myview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview = new MyView(this);
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Back = new Button(this);
Back.setText("Back");
Next = new Button(this);
Next.setText("Next");
Erase = new Button(this);
Erase.setText("Eraser");
clrScreen = new Button(this);
clrScreen.setText("Clear");
myLayout.addView(myview);
myLayout.addView(Back);
myLayout.addView(Next);
myLayout.addView(Erase);
myLayout.addView(clrScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(65, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_HORIZONTAL);
params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Erase.setLayoutParams(params3);
Erase.bringToFront();
Next.setLayoutParams(params2);
Next.bringToFront();
Back.setLayoutParams(params);
Back.bringToFront();
clrScreen.setLayoutParams(params3);
clrScreen.bringToFront();
this.setContentView(myLayout);
Back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, MainActivity.class);
Flag = Flag - 1;
startActivity(i);
finish();
}
});
Next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, MainActivity.class);
Flag = Flag + 1;
startActivity(i);
finish();
}
});
Erase.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
/*Intent i = new Intent(MainActivity.this, MainActivity.class);
Flag = Flag;
startActivity(i);
finish();*/
mPaint = new Paint();
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
}
});
clrScreen.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
myview.Clear_Drawing();
}
});
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);//0xffffffff
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(10);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
mPaint.setMaskFilter(mEmboss);
}
private Paint mPaint;
private MaskFilter mEmboss;
public class MyView extends View
{
MainActivity ma=new MainActivity();
Bitmap mBitmap = null;
private Canvas mCanvas;
Path mPath = null;
private Paint mBitmapPaint;
Bitmap bmp;
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
public MyView(Context c) {
super(c);
switch(Flag)
{
case 0:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 1:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 2:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 3:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 4:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 5:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 6:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 7:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 8:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 9:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 10:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 11:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 12:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 13:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 14:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 15:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 16:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 17:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 18:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 19:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 20:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 21:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 22:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 23:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 24:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 25:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 26:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 27:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 28:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 29:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 30:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 31:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 32:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 33:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 34:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 35:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 36:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 37:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 38:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 39:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 40:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 41:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 42:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 43:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 44:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
case 45:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
break;
case 46:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
break;
default:
break;
}
//Display display = getWindowManager().getDefaultDisplay();
// int width = display.getWidth();
mBitmap = Bitmap.createScaledBitmap(bmp, width, 350, true);
//mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;
}
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);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
public void Clear_Drawing()
{
//mBitmap = null;
mPath = null;
mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);
mPath = new Path();
}
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;
}
}
}