我是android的新手,我在Api Demo中使用FingerPaint.java帮助准备了一个绘画视图。它工作正常。
同一个视图包含一个按钮(清除),如果我们点击按钮颜色将是清晰的(空格和一个按钮)。我的问题是如何清除油漆。我的查看代码是,
public class MyDraw extends View {
Paint mPaintAlphabet = new Paint();
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
float x, y;
float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint;
public MyDraw(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public MyDraw(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
mBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaintAlphabet.setDither(true);
mPaintAlphabet.setColor(0xFFFF0000);
mPaintAlphabet.setStyle(Paint.Style.STROKE);
mPaintAlphabet.setStrokeJoin(Paint.Join.ROUND);
mPaintAlphabet.setStrokeCap(Paint.Cap.ROUND);
mPaintAlphabet.setStrokeWidth(3);
mPaintAlphabet.setTextSize(350);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
public void onDraw(Canvas canvas) {
if (MyAlphabetsActivity.mArryLstAlphabets.get(
MyAlphabetsActivity.mAlphaIndex).equals("I")) {
canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
.get(MyAlphabetsActivity.mAlphaIndex), 180, 300,
mPaintAlphabet);
} else if (MyAlphabetsActivity.mArryLstAlphabets.get(
MyAlphabetsActivity.mAlphaIndex).equals("J")) {
canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
.get(MyAlphabetsActivity.mAlphaIndex), 180, 300,
mPaintAlphabet);
} else {
canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
.get(MyAlphabetsActivity.mAlphaIndex), 120, 300,
mPaintAlphabet);
}
System.out.println("in on draw");
System.out.println("path is----" + mPath);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, MyAlphabetsActivity.mPaint);
}
private void touch_start(float x, float y) {
mPath.reset();
System.out.println("mPath-----"+mPath);
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, MyAlphabetsActivity.mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
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;
}
}
我的活动代码是,
public class MyAnimViewActivity extends Activity
{
public static Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
Button mBtnClear;
MyDraw mMyDraw=new MyDraw();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyOwnView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
mBtnClear=(Button)findViewById(R.id.btnClear);
mBtnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnClear:
mMyDraw.mPath.reset();
break;
}
}
});
}
}
请帮帮我..谢谢提前
答案 0 :(得分:2)
在自定义绘图类中创建名为clearDrawing()的公共函数;写下这个:
mBitmap = null;
mPath = null;
mBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888);
mPath = new Path();
现在调用clearDrawing()来重置绘图控件。