Created
January 20, 2014 21:33
-
-
Save yekmer/8529643 to your computer and use it in GitHub Desktop.
This is an imageview extension to create a masked imageview
This file contains 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
import java.io.InputStream; | |
import java.net.URL; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.Xfermode; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.util.TypedValue; | |
import android.widget.ImageView; | |
public class MaskedImageView extends ImageView { | |
Bitmap resultBitmap = null; | |
Paint paint = new Paint(); | |
Bitmap mOriginalBitmap; | |
Context context; | |
// I am using Mode.SRC_IN, you can choose your own mode by looking at demos that fits your needs | |
private static final Xfermode sModes = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); | |
//This is for creating image that have specified dp values, you do not have to do this | |
int iW = getPixelValueFromDip(120); | |
int iH = getPixelValueFromDip(70); | |
// create a bitmap with a circle, used for the "dst" image | |
private Bitmap makeDst() { | |
//you can return your mask image here | |
return RETURN_YOUR_MASK_BITMAP_HERE; | |
} | |
//get your source image that will be masked | |
private Bitmap makeSrc() { | |
if (mOriginalBitmap == null) { | |
Drawable drawable = getDrawable(); | |
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); | |
Bitmap mOriginalBitmap = bitmap; | |
mOriginalBitmap = Bitmap.createScaledBitmap(mOriginalBitmap, iW, iH, true); | |
return mOriginalBitmap; | |
} else { | |
return mOriginalBitmap; | |
} | |
} | |
//I use this class to convert correct pixel values from dp values, you do not have to use | |
public int getPixelValueFromDip(int dpIntValue) { | |
float dpValue = Float.valueOf(dpIntValue); | |
Resources r = getResources(); | |
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, r.getDisplayMetrics()); | |
return Math.round(px); | |
} | |
//constructors here | |
public MaskedImageView(Context context) { | |
this(context, null); | |
} | |
public MaskedImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
//onDraw method is important here, but remember that if you are animating image than onDraw will be called a lot, and these calculations can cause performance drop | |
@Override | |
protected void onDraw(Canvas canvas) { | |
//these width and height values are specific to my project, you can use anything you want | |
int w = 200, h = 200; | |
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types | |
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap | |
canvas.drawColor(Color.TRANSPARENT); | |
int x = 0; | |
int y = 0; | |
int sc = canvas.saveLayer(x, y, x + 200, y + 200, null, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | |
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | |
| Canvas.CLIP_TO_LAYER_SAVE_FLAG); | |
canvas.drawBitmap(makeDst(), 0, 0, paint); | |
paint.setXfermode(sModes); | |
canvas.drawBitmap(makeSrc(), 0, 0, paint); | |
paint.setXfermode(null); | |
canvas.restoreToCount(sc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment