Ahmet Urun Ahmet Urun - 3 months ago 26
Android Question

Setting Background with setBackgroungDrawable function in Android

Okey I have a problem with setting a picture for the background. At first I just set the background from 'properities' box. I copy the .jpg file to my drawable folder and just click on the background properity from properities box then select the file from the resources folder. That was okey and it worked but then i wanted to make the background blurry and I found a class that is wrote for that, called BlurBuilder and I put that class to my project. Everything is okey to this point, I added the class but when I tried to apply the funciton from that class as shown in an example like that:

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


editor says 'cannot resolve symbol 'view' '.
I tryied to change the second line as;

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


But this time android studio says 'Non-static method 'setBackgroudDrawable(android.graphic.drawables.Drawable) cannot be referenced from a static context

Here's the BlurBuilder class I used:

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;
}}


Thanks in advance.

EDİT: I want to make my activity's background a blurry image.

Answer

You call a non-static function i.e setDrawableBackground from a non-static context which means to say that you need to create an instance of some View for example:

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

Once done now you can call any function that view supports.

Comments