2

I want to rub Image.Actually i have two images one in foreground and another in background when i rub forground image then background image will have to come how to do that?I found a lot here but cannot get the solution yet.So Someone please help me for my this issue. I tried this:

public class MainActivity extends Activity {

ImageView img;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    // img = (ImageView) findViewById(R.id.img);
    //
    // new Panel(this);
    setContentView(new Panel(this));

}

class Panel extends View {

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mPaint;
    Bitmap bitmap;
    Canvas pcanvas;
    int x = 0;
    int y = 0;
    int r = 0;

    public Panel(Context context) {
        super(context);

        Log.v("Panel", ">>>>>>");

        setFocusable(true);
        setBackgroundColor(Color.MAGENTA);

        // setting paint
        mPaint = new Paint();
        mPaint.setAlpha(1);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setAntiAlias(true);

        // getting image from resources
        Resources r = this.getContext().getResources();

        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.verse);

        // converting image bitmap into mutable bitmap

        // bitmap = Bitmap.createBitmap(295, 260, Config.ARGB_8888);
        bitmap = Bitmap.createBitmap(185, 120, Config.ARGB_8888);
        pcanvas = new Canvas();
        pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
        pcanvas.drawBitmap(bm, 0, 0, null);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        // draw a circle that is erasing bitmap
        // pcanvas.drawCircle(x, y, r, mPaint);
        pcanvas.drawColor(color.transparent);
        canvas.drawBitmap(bitmap, 0, 0, null);

        super.onDraw(canvas);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // set paramete to draw circle on touch event
        x = (int) event.getX();
        y = (int) event.getY();

        r = 20;
        // Atlast invalidate canvas
        invalidate();
        return true;
    }

}
}
Vinay
  • 6,891
  • 4
  • 32
  • 50
Biginner
  • 243
  • 1
  • 2
  • 15

1 Answers1

0

You might want to try using Xfermodes/Porterduff, possibly this thread might help you: Android bitmap mask color, remove color

To hard set the color you could use

int[] pix=new int[bitmap.getWidth()*bitmap.getHeight()];
bitmap.getPixels(pix, 0, 0, bitmap.getWidth(), bitmap.getHeight);

// do some processing here
// possible set color of some pixels to Color.TRANSPARENT

bitmap.setPixels(pix, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
Community
  • 1
  • 1
tilpner
  • 4,351
  • 2
  • 22
  • 45