【发布时间】:2016-08-19 17:44:57
【问题描述】:
好的,我在为背景设置图片时遇到问题。起初我只是从“属性”框中设置背景。我将 .jpg 文件复制到我的可绘制文件夹中,然后从 properities 框中单击 background 属性,然后从资源文件夹中选择文件。没关系,它起作用了,但后来我想让背景变得模糊,我找到了一个为此编写的类,称为 BlurBuilder,然后我将该类放入我的项目中。到目前为止一切正常,我添加了该类,但是当我尝试应用该类中的功能时,如以下示例所示:
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
编辑说'无法解析符号'view''。 我尝试将第二行更改为;
View.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
但是这次android studio说'非静态方法'setBackgroudDrawable(android.graphic.drawables.Drawable)不能从静态上下文中引用
这是我使用的 BlurBuilder 类:
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;
}}
提前致谢。
编辑:我想让我的活动背景模糊。
【问题讨论】:
-
我假设 view 是你的 ImageView 变量名?
-
不,不是。我刚开始android编程。所以我只是离开那里,因为我从示例中复制。我知道我应该改变它,但我不知道该怎么做。应该怎么改?我正在尝试为整个布局设置图片。
标签: android android-studio background-image blurry setbackground