3
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SingleTouchEventView extends View {
 private Paint paint = new Paint();
 private Path path = new Path();
 public boolean cc = false;

 public SingleTouchEventView(Context context, AttributeSet attrs) {
  super(context, attrs);

  paint.setAntiAlias(true);
  paint.setStrokeWidth(18f);
  paint.setColor(Color.LTGRAY);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeJoin(Paint.Join.ROUND);
  paint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
    if(cc)
    {
        Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square);
        Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false);
        canvas.drawBitmap(cb,0,0,null);
        cc = false;

    }
  canvas.drawPath(path, paint);
}

public void clearCanvas()
{
  cc =true;
  invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
  float eventX = event.getX();
  float eventY = event.getY();

  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    path.moveTo(eventX, eventY);
    return true;
   case MotionEvent.ACTION_MOVE:
    path.lineTo(eventX, eventY);
    break;
   case MotionEvent.ACTION_UP:
    // nothing to do
    break;
   default:
    return false;
  }

  // Schedules a repaint.
  invalidate();
  return true;
  }
} 

上面的文件是我的SingleTouchEventView.Java 这是我的MainActivity.java

public class MainActivity extends Activity {

    Button reset;;
LinearLayout canvasAlphabets;
    SingleTouchEventView myView; 


   @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);


    reset = (Button)findViewById(R.id.reset_canvas);

    myView = new SingleTouchEventView(this, null);
    canvasAlphabets = (LinearLayout)findViewById(R.id.canvas_Alphabets);
    canvasAlphabets.addView(myView);


           reset.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        }
        });

      }

  }

我的问题是我应该在重置按钮中使用什么代码来删除画布的所有内容。请帮助我,我已经尝试实施myView.clearCanvas(),但这没有帮助。如果我添加此代码以在单击时重置按钮,则会导致 FC

谢谢

4

3 回答 3

6
 path = new Path(); 
 Paint clearPaint = new Paint(); 
 clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
 canvas.drawRect(0, 0, 0, 0, clearPaint); 
 cc = false; 

我用上面的代码修复了它

于 2013-08-04T11:52:03.943 回答
2

也许我不明白你想画什么,但你试过这个:

protected void onDraw(Canvas canvas) 
{
    if (cc)
    {
        Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square);
        Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false);
        canvas.drawBitmap(cb,0,0,null);
        cc = false;
    } 
    else
        canvas.drawPath(path, paint);
    }
}

否则,如果你想擦除所有,你可以使用这个新的油漆:

Paint transparent = new Paint();
transparent.setAlpha(0);
于 2013-06-13T10:49:49.470 回答
0

您可以使用透明颜色对所有内容进行 panit,以清除所有内容。

于 2013-06-13T10:49:34.813 回答