-1

I need to make the layout semi-transparent as my app opens and make it look back normal again after the onclick of and icon that I have set in the toolbar

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yesha Shah
  • 408
  • 1
  • 5
  • 17

3 Answers3

3

In MainActivity.java

Add below line for declaration.

public LinearLayout linearlayout;

Add below two lines in onCreate of MainActivity.java

linearLayout=(LinearLayout) findViewById(R.id.layout_main);
linearLayout.setAlpha((float) 0.5);

And write below line on onclick() of your icon, on whose onclick() you need to make it look normal again.

linearlayout.setAlpha((float) 1);
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
komal akhani
  • 553
  • 6
  • 20
0

set alpha in your xml layout for blur image.

Rims
  • 78
  • 2
  • 9
  • alpha? for blur effect? – pskink May 11 '17 at 05:15
  • Yes.Or you can see also this link.http://stackoverflow.com/questions/14780006/transparent-blurry-view-which-blurs-layout-underneath – Rims May 11 '17 at 05:16
  • do you know what alpha is? and how is it related to image bluring? – pskink May 11 '17 at 05:17
  • In xml: android:alpha="0.5" or in code yourView.getBackground().setAlpha(100); – Rims May 11 '17 at 05:19
  • @pskink actually you can set alpha from your mainActivity.java also. Set it as linearlayout.setAlpha((float) 0.5) to make it blur and write linearlayout.setAlpha((float)1) to make it completely normal again......... (0) make it completely transperant, I am not sure regarding zero. – Yesha Shah May 11 '17 at 05:34
  • @YeshaShah alpha has nothing to do with [blurring](https://en.wikipedia.org/wiki/Gaussian_blur), if you want semi transparent look use alpha but as i said it is not blurring – pskink May 11 '17 at 05:36
0

You can create blurred bitmap from a background image or view then set it as background.I used the below class to create a blurred bitmap

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.view.View;

public class BlurBuilder {
        private static final float BITMAP_SCALE = 0.2f;
        private static final float BLUR_RADIUS = 20f;

        public static Bitmap blur(View v) {
            return blur(v.getContext(), getScreenshot(v));
        }

        public static Bitmap blur(Context ctx, 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(ctx);
            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;
        }

        private static Bitmap getScreenshot(View v) {
            Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            v.draw(c);
            return b;
        }
    }

Then

 mBackgroundBlurBitmap = BlurBuilder.blur(this, mBackgroundBitmap);
Nidhin
  • 1,818
  • 22
  • 23