I am displaying layout background image every image getting from server and it is working fine. I need to layout background image to blur. I wrote a class for the same.
My question is: How to pick blur color from palette API?
Bluer.java
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
MainActivity.java
final LinearLayout mMainLayout = (LinearLayout)findViewById(R.id.revi_main_layout);
target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
mMainLayout.setBackground(new BitmapDrawable(bitmap));
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
movie_image.setImageBitmap(bitmap);
int defaultColor = getResources().getColor(R.color.whiteColor);
toolbar.setBackgroundColor(palette.getLightVibrantColor(defaultColor));
toolbar.setTitleTextColor(palette.getDarkMutedColor(defaultColor));
toolbar.setTitle(getString(R.string.title_activity_news_update_fulldec));
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(getApplicationContext()).load((Reviews_update.revData.get(mov_pos))
.getNewsImage()).into(target);
Please help me how to blur my layout background image, blur color from palette API?
Thank You in advance
RenderScript requires sdk version 22.2 as minimum sdk. This method works for older version as well. Use this method:
Example:
let me know if it work or not