user3843164
user3843164

Reputation: 379

Android draw certain pixels of a bitmap

Ok so I want to have a bitmap where it is invisible but if you touch it, then the area that is touched will show. This might not seem useful by itself, but I just want to know how to do it so I can apply it to what I really want to do. So how would you do this in android where you only draw certain pixels of a bitmap?

Upvotes: 0

Views: 1019

Answers (1)

MH.
MH.

Reputation: 45493

A good starting point would be Roman Guy's "Fun with shaders" recipe, which should get you pretty close:

Thus by moving a shape over the canvas you can reveal different parts of the bitmap.

The basic steps include:

Create an alpha mask (the linked example uses another bitmap for this):

private static Bitmap convertToAlphaMask(Bitmap b) {
    Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(a);
    c.drawBitmap(b, 0.0f, 0.0f, null);
    return a;
}

Bitmap mask;
mask = convertToAlphaMask(BitmapFactory.decodeResource(getResources(), R.drawable.spot_mask));

Create a Paint instance with a shader to draw the mask with:

private static Shader createShader(Bitmap b) {
    return new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}

Shader targetShader = createShader(mTargetBitmap);
paint.setShader(targetShader);

Finally, draw the mask with the shader:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mMask, 0.0f, 0.0f, mPaint);
}

Now, in stead of animating the 'light beam' over a predefined path (as in the example code), hook it up to touch events and go from there. I.e. you may have to keep track of the touched areas and/or paths to get the desired effect.

Upvotes: 1

Related Questions