replacing one bitmap with second one canvas androi

2019-09-08 14:55发布

问题:

i want to make an application in that there is two bitmap, i want to remove image whenever i touch my finger or move my finger and second bitmap's part be shown will be shown & first image's part will be erased i have used following code but in that both images are replacing but i want to erase only first one, second will shown on erased area, ..can any body help me how to handle this

below is the code

public class PaintView extends View implements OnTouchListener {
private static final String TAG = "PaintView";
Bitmap Bitmap1, Bitmap2;
Bitmap Transparent;
Bitmap overlay;
int X = -100;
int Y = -100;
Canvas c2;

// List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();

public PaintView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    Bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bear);
    Bitmap2 = BitmapFactory
            .decodeResource(getResources(), R.drawable.camel);
    Transparent = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
    c2 = new Canvas();
    c2.setBitmap(Transparent);
    c2.drawBitmap(Bitmap1, 0, 0, null);


    paint.setAlpha(0);
    paint.setStyle(Style.FILL);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
    paint.setAntiAlias(true);

}

@Override
public void onDraw(Canvas canvas) {
    c2.drawBitmap(Bitmap2, 0, 0, null);
    c2.drawCircle(X, Y, 50, paint);
    canvas.drawBitmap(Transparent, 0, 0, null);

}

public boolean onTouch(View view, MotionEvent event) {
    X = (int) event.getX();
    Y = (int) event.getY();

    invalidate();
    return true;
}
 }

class Point {
float x, y;

@Override
public String toString() {
    return x + ", " + y;
}
}

回答1:

Finally i have solved that problem by

    public class PaintView extends View implements OnTouchListener {
private static final String TAG = "PaintView";
Bitmap Bitmap1, Bitmap2;
Bitmap Transparent;
Bitmap overlay;
int X = -100;
int Y = -100;
Canvas c2;
private boolean isTouched = false;

// List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();

public PaintView(Activity context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    Transparent = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bear);
    Bitmap2 = BitmapFactory
            .decodeResource(getResources(), R.drawable.date);

    c2 = new Canvas();
    c2.setBitmap(Transparent);
      //        c2.drawBitmap(Bitmap1, 0, 0, null);
    c2.drawBitmap(Bitmap2, 0, 0, paint);

    paint.setAlpha(0);
    paint.setStyle(Style.FILL);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
    paint.setAntiAlias(true);




}

 private static Point getDisplaySize(final Display display) {
  final Point point = new Point();
   point.x = display.getWidth();
   point.y = display.getHeight();
  return point;
 }

@Override
public void onDraw(Canvas canvas) {
    System.out.println("onDraw");
    c2.drawCircle(X, Y, 50, paint);
    if(isTouched)
    {
        canvas.drawBitmap(Bitmap1, 0, 0, null);
    }
    canvas.drawBitmap(Transparent, 0, 0, null);

}

public boolean onTouch(View view, MotionEvent event) {
    isTouched = true;
    X = (int) event.getX();
    Y = (int) event.getY();

    invalidate();
    return true;
}
 }

  class Point {
float x, y;

@Override
public String toString() {
    return x + ", " + y;
}
 }