Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink bitmap before blur,then zoom bitmap after blured #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.daimajia.androidviewhover.tools;

import android.graphics.Bitmap;
import android.content.Context;
import android.graphics.Matrix;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
Expand All @@ -10,12 +12,15 @@
public class Blur {

private static final int DEFAULT_BLUR_RADIUS = 10;
private static final float DEFAULT_SCALE = 2;

public static Bitmap apply(Context context, Bitmap sentBitmap) {
return apply(context, sentBitmap, DEFAULT_BLUR_RADIUS);
}

public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
// shrink half
sentBitmap = scaleBitmap(sentBitmap, 1 / DEFAULT_SCALE);
final Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Expand All @@ -26,13 +31,21 @@ public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);

//zoom bitmap
final Bitmap zoomBitmap = scaleBitmap(bitmap, DEFAULT_SCALE);
bitmap.recycle();
sentBitmap.recycle();
rs.destroy();
input.destroy();
output.destroy();
script.destroy();

return bitmap;
return zoomBitmap;
}

public static Bitmap scaleBitmap(Bitmap oldBitmap, float scale) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
return Bitmap.createBitmap(oldBitmap, 0, 0, oldBitmap.getWidth(), oldBitmap.getHeight(), matrix, true);
}
}
}