【问题标题】:Setting Background with setBackgroungDrawable function in Android在 Android 中使用 setBackgroundDrawable 函数设置背景
【发布时间】: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


【解决方案1】:

您从非静态上下文调用非静态函数,即 setDrawableBackground,这意味着您需要创建某个 View 的实例,例如:

ImageView view = (ImageView) findViewById(R.id.imageview1);    
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );

现在完成后,您可以调用view 支持的任何函数。

【讨论】:

  • 感谢您的解释。但我试图将图片设置为整个布局的背景,而不是 ImageView。我该怎么办。
  • 布局是否填满整个屏幕?
  • 你不需要一个视图,你可以在布局上调用相同的 setBackgroundDrawable!
  • 所以不要使用视图,只需从 xml 资源中获取您的布局,或者如果您在代码中调用相同的方法并将其设置为 contentView()。一切都应该工作
  • 所以在 onCreate() 中调用它:getWindow().setBackgroundDrawableResource(R.drawable.your_image);
【解决方案2】:

知道了,

在 XML 中添加以下内容(main_activity.xml 或您的文件):

<ImageView
        android:id="@+id/whole_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" />

在 [in Main Activity] 中添加以下内容:这将强制缩放图像以显示屏幕尺寸并使用整个显示器来显示图像。

    /* where you set the activity */
    setContentView(R.layout.your_main_activity);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    /* adapt the image to the size of the display */
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
    Bitmap bmp = Bitmap.createScaledBitmap(blurredBitmap,size.x,size.y,true);

    /* fill the background ImageView with the resized image */
    ImageView iv_background = (ImageView) findViewById(R.id.whole_background);
    iv_background.setImageBitmap(bmp);

【讨论】:

  • 谢谢,我会试试看,但 ImageView 不会让我布局上的所有其他元素都消失。
  • 不准确。 :) 如果您使用 AppCompactActivity 并使用 FloatingActionButton 则不会。由于我不确定您的用例,您可以简单地使用模糊图像并将图像设置在布局背景中,以便其余视图保持不变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多