在Android中使用setBackgroungDrawable函数设置背景

时间:2016-08-19 17:44:57

标签: android android-studio background-image blurry setbackground

Okey我在为背景设置图片时遇到问题。起初我只是从'专长'框中设置背景。我将.jpg文件复制到我的drawable文件夹,然后从专长框中单击背景特性,然后从资源文件夹中选择文件。这很好,但它确实有效,但后来我想让背景变得模糊,我发现了一个为此而写的类,名为 BlurBuilder ,我把那个类放到了我的项目中。到目前为止,一切都很好,我添加了类,但是当我尝试应用该类中的函数时,如下例所示:

    Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
    view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );

编辑说'无法解析符号'view''。 我试图将第二行更改为;

View.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );

但是这次android工作室说'非静态方法'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;
}}

提前致谢。

EDİT:我想让我的活动背景变得模糊。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

知道了,

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

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

在[主活动]中添加以下内容:这将强制缩放图像以显示屏幕大小,并使用整个显示来显示图像。

    /* 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);