Skip to content

Commit

Permalink
Refactor allocation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
emersoncloud committed Sep 7, 2018
1 parent a0017ae commit 68cc0fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion blurkit/src/main/java/com/flurgle/blurkit/BlurKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class BlurKit {

private static final float FULL_SCALE = 1f;

private static BlurKit instance;

private RenderScript rs;
Expand All @@ -37,7 +39,7 @@ public Bitmap blur(Bitmap src, int radius) {
}

public Bitmap blur(View src, int radius) {
Bitmap bitmap = getBitmapForView(src, 1f);
Bitmap bitmap = getBitmapForView(src, FULL_SCALE);
return blur(bitmap, radius);
}

Expand Down
31 changes: 16 additions & 15 deletions blurkit/src/main/java/com/flurgle/blurkit/RoundedImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
Expand All @@ -16,41 +15,43 @@
public class RoundedImageView extends ImageView {

private float mCornerRadius = 0;
public static final int DEFAULT_COLOR = 0xff000000;

private RectF rectF;
private PorterDuffXfermode porterDuffXfermode;

public RoundedImageView(Context context) {
super(context, null);
rectF = new RectF();
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}

public RoundedImageView(Context context, AttributeSet attributes) {
super(context, attributes);
rectF = new RectF();
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}

@Override
protected void onDraw(Canvas canvas) {
Drawable myDrawable = getDrawable();
if (myDrawable!=null && myDrawable instanceof BitmapDrawable && mCornerRadius > 0) {
Paint paint = ((BitmapDrawable) myDrawable).getPaint();
final int color = 0xff000000;
Rect bitmapBounds = myDrawable.getBounds();
final RectF rectF = new RectF(bitmapBounds);
int saveCount = canvas.saveLayer(rectF, 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);
rectF.set(myDrawable.getBounds());
int prevCount = canvas.saveLayer(rectF, null, Canvas.ALL_SAVE_FLAG);
getImageMatrix().mapRect(rectF);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
paint.setColor(COLOR);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);

Xfermode oldMode = paint.getXfermode();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Xfermode prevMode = paint.getXfermode();
paint.setXfermode(porterDuffXfermode);
super.onDraw(canvas);
paint.setXfermode(oldMode);
canvas.restoreToCount(saveCount);

paint.setXfermode(prevMode);
canvas.restoreToCount(prevCount);
} else {
super.onDraw(canvas);
}
Expand Down

0 comments on commit 68cc0fe

Please sign in to comment.