Created
July 25, 2012 11:22
-
-
Save cblunt/3175620 to your computer and use it in GitHub Desktop.
Android MaskedImageView - Uses the Background resource as a mask for the src bitmap. If set, foregroundBitmap is overlaid on top of the masked image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import android.content.Context; | |
import android.graphics.*; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.NinePatchDrawable; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.widget.ImageView; | |
public class MaskedImageView extends ImageView { | |
private Bitmap srcBitmap; | |
private Drawable foregroundDrawable; | |
public MaskedImageView(Context context) { | |
super(context); | |
} | |
public MaskedImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public MaskedImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onDraw(Canvas onDrawCanvas) { | |
Bitmap mutableBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(mutableBitmap); | |
Paint paint = new Paint(); | |
paint.setFilterBitmap(false); | |
if (srcBitmap != null) { | |
canvas.drawBitmap(srcBitmap, 0, 0, paint); | |
} | |
if (getBackground() != null) { | |
NinePatchDrawable background = (NinePatchDrawable) getBackground(); | |
background.getPaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); | |
background.draw(canvas); | |
} | |
final Drawable foreground = getForeground(); | |
if (foreground != null) { | |
foreground.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); | |
final int scrollX = getScrollX(); | |
final int scrollY = getScrollY(); | |
if ((scrollX | scrollY) == 0) { | |
foreground.draw(canvas); | |
} | |
else { | |
canvas.translate(scrollX, scrollY); | |
foreground.draw(canvas); | |
canvas.translate(-scrollX, -scrollY); | |
} | |
} | |
onDrawCanvas.drawBitmap(mutableBitmap, 0, 0, paint); | |
} | |
private Drawable getForeground() { | |
return foregroundDrawable; | |
} | |
@Override | |
public void setImageBitmap(Bitmap bitmap) { | |
super.setImageBitmap(bitmap); | |
srcBitmap = bitmap; | |
invalidate(); | |
} | |
public void setForegroundResource(int resId) { | |
setForegroundDrawable(getContext().getResources().getDrawable(resId)); | |
} | |
public void setForegroundDrawable(Drawable d) { | |
d.setCallback(this); | |
d.setVisible(getVisibility() == VISIBLE, false); | |
foregroundDrawable = d; | |
requestLayout(); | |
invalidate(); | |
} | |
} |
@ipsonic I might be (a bit?) late, but yes. You can mask any View. Have a look at this repo ShapeOfView
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you think there is a way to Mask a View instead of an ImageView? As in, get the clipPath from the image and apply it to a View?