Issue
I need to make RecyclerView's items glowing when they are focused. Like this
9 patch isn't a proper solution because images that have transparency aren't always square.
Solution
Using the code from this answer I've created a utils class.
public class GlowEffectUtils {
@NonNull
public static Drawable createSelector(@NonNull Resources resources, @NonNull View view) {
Bitmap glow = createGlow(view);
StateListDrawable selector = new StateListDrawable();
BitmapDrawable glowDrawable = new BitmapDrawable(resources, glow);
selector.addState(new int[]{android.R.attr.state_focused}, glowDrawable);
return selector;
}
@NonNull
private static Bitmap createGlow(@NonNull View view) {
int glowRadius = 15;
int glowColor = Color.rgb(255, 255, 255);
Bitmap src = createBitmap(view);
Bitmap alpha = src.extractAlpha();
Bitmap bmp = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(glowColor);
paint.setMaskFilter(new BlurMaskFilter(glowRadius, BlurMaskFilter.Blur.OUTER));
canvas.drawBitmap(alpha, 0, 0, paint);
return bmp;
}
@NonNull
private static Bitmap createBitmap(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
And set it like this:
Resources resources = itemView.getContext().getResources();
Drawable selector = GlowEffectUtils.createSelector(resources, itemView);
itemView.setBackground(selector);
Answered By - 0neel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.