好的,我在设置背景图片时遇到了问题。起初我只是从“属性”框中设置背景。我将 .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;
}}
提前致谢。
编辑:我想让我的 Activity 背景变得模糊。
最佳答案
您从非静态上下文调用非静态函数,即 setDrawableBackground,这意味着您需要创建某个 View 的实例,例如:
ImageView view = (ImageView) findViewById(R.id.imageview1);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
一旦完成,现在您可以调用 view
支持的任何函数。
关于android - 在 Android 中使用 setBackgroundDrawable 函数设置背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044966/