如何在 android 透明渐变背景中给出线性布局,布局当前有一个模糊的图像作为其背景,但我需要模糊的图像在布局顶部稍微透明,然后在其底部转到不透明。
布局嵌套在视差滚动视图中,因此布局可以在标题图像的顶部滑动。
我像这样生成模糊的可绘制对象:
final View content = img_header;
if (content.getWidth() > 0) {
Bitmap image = BlurBuilder.blur(content);
BitmapDrawable background = new BitmapDrawable(image);
ll_parent_layout.setBackground(background);
} else {
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Bitmap image = BlurBuilder.blur(content);
BitmapDrawable background = new BitmapDrawable(image);
ll_parent_layout.setBackground(background);
}
});
}
模糊生成器如下所示:
public class BlurBuilder {
private static final float BITMAP_SCALE = 1f;
private static final float BLUR_RADIUS = 25f;
public static Bitmap blur(View v) {
return blur(v.getContext(), getScreenshot(v));
}
public static Bitmap blur(Context ctx, 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);
/// compress bitmap here
RenderScript rs = RenderScript.create(ctx);
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;
}
public static Bitmap blur(Context ctx, Bitmap image, float scale, float radius) {
int width = Math.round(image.getWidth() * scale);
int height = Math.round(image.getHeight() * scale);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(ctx);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(radius);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
private static Bitmap getScreenshot(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
由于您正在操作Bitmap
以生成模糊的图像,因此您可以使用LinearGradient
着色器为其添加透明度,如本答案所示。
public Bitmap addGradient(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap overlay = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, 0, 0, h, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.REPEAT);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawRect(0, h - h, w, h, paint);
return overlay;
}
您可以使用可绘制的 xml 将渐变设置为布局,如下所示。
将下面的代码放在文件中,例如mybg.xml并将其放在可绘制文件夹中。
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="@color/mycolor1"
android:endColor="@color/mycolor2"
android:angle="270" />
</shape>
使用 8 个字符的哈希码作为颜色,这样你的颜色就可以像这样
mycolor1 -> #000000aa //this will give you transparent colored background
mycolor2 -> #ff00aa00 //this will be opaque like background
此处的前两个字符设置颜色的 alpha 级别。
可以这样做:
只要确保位图是可变的,同样使用淡入淡出部分值(>= 0 和 <=1),您只能使背景渐变的一部分透明
public static Bitmap makeBitmapTransparent(Bitmap mutableBitmap, float fadingPortion) {
final Shader gradientShader = new LinearGradient(
mutableBitmap.getWidth() / 2, 0,
mutableBitmap.getWidth() / 2, mutableBitmap.getHeight() * fadingPortion,
0x00000000, 0xFF000000,
Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(gradientShader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawRect(new Rect(0, 0, mutableBitmap.getWidth(), (int) (mutableBitmap.getHeight() * FADING_PORTION)), paint);
canvas.drawBitmap(mutableBitmap, 0, 0, null);
return mutableBitmap;
}