AlertDialog with Blur background in position center

前端 未结 1 1837
面向向阳花
面向向阳花 2020-12-03 16:30

Question

Tried to blur (not DIM) the background of a Dialog.

and FLAG_BLUR_BEHIND is deprecated.

<
相关标签:
1条回答
  • 2020-12-03 17:21

    Note: This screen background can be anything you have on it, what it does is take a screenshot of it and blur that finally set it as the background of the alertDialog with blur effect

    Problem was alertDialog attached with that blur background was always on the top and i couldn't change that.

    I came up with this solution.

    I had to use two alertDialogs one is for the blur effect(for the use of getWindow()) and it does not get display now(I removed its setContentView / can use transparent background)

    The other one is the real alert dialog got display after the blur effect.

    MainActivity

    public class MainActivity extends AppCompatActivity {
    
        private  AlertDialog dialogWhichDisplayAlert;
        private  Dialog fakeDialogUseToGetWindowForBlurEffect;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
        }
    
    
        @Override
        public void onBackPressed() {
    
    
            fakeDialogUseToGetWindowForBlurEffect = new Dialog(MainActivity.this);
            //  fakeDialogUseToGetWindowForBlurEffect.setContentView(R.layout.fakealert); // removed the content so not visible
    
    
    
            new BlurAsyncTask().execute();
    
        }
    
        class BlurAsyncTask extends AsyncTask<Void, Integer, Bitmap> {
    
    
            protected Bitmap doInBackground(Void...arg0) {
    
                Bitmap map  = AppUtils.takeScreenShot(MainActivity.this);
                Bitmap fast = new BlurView().fastBlur(map, 10);
                return fast;
            }
    
    
            protected void onPostExecute(Bitmap result) {
                if (result != null){
                    final Drawable draw=new BitmapDrawable(getResources(),result);
                    Window window = fakeDialogUseToGetWindowForBlurEffect.getWindow();
                    window.setBackgroundDrawable(draw);
                    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
                    window.setGravity(Gravity.CENTER);
                    fakeDialogUseToGetWindowForBlurEffect.show();
    
    
                    // real one
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
                    builder.setTitle("Lets Blur");
                    builder.setMessage("This is Blur Demo");
                    builder.setCancelable(false);
                    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            fakeDialogUseToGetWindowForBlurEffect.dismiss();
                            dialog.cancel();
                        }
                    });
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            fakeDialogUseToGetWindowForBlurEffect.dismiss();
                            dialog.cancel();
                        }
                    });
                    dialogWhichDisplayAlert = builder.create();
    
                    // position real dialogWhichDisplayAlert using Gravity.CENTER;
                    dialogWhichDisplayAlert.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    WindowManager.LayoutParams wmlp = dialogWhichDisplayAlert.getWindow().getAttributes();
                    wmlp.gravity = Gravity.CENTER;
                    dialogWhichDisplayAlert.show();
    
                }
    
            }
        }
    }
    

    [other classes used are same as the question]

    style

    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
            <item name="android:textColor">#00FF00</item>
            <item name="android:typeface">monospace</item>
            <item name="android:textSize">10sp</item>
        </style>
    

    Out put

    0 讨论(0)
提交回复
热议问题