用第二个canvas android替换一个位图

时间:2014-01-03 13:02:42

标签: android android-canvas

我想创建一个应用程序,因为有两个位图,我想删除图像每当我触摸我的手指或移动我的手指和第二个位图的部分显示将显示&第一个图像的部分将被删除我已经使用了以下代码,但是两个图像都在替换但我只想删除第一个,第二个将在删除区域显示,..可以任何身体帮助我如何处理这个

下面是代码

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 个答案:

答案 0 :(得分:0)

最后我通过

解决了这个问题
    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;
}
 }