2014-09-03 150 views
4

我想要模糊屏幕下的對話框,所以我把活動的「截圖」,模糊它,並設置爲對話窗口的背景作爲BitmapDrawable。奇怪的是,即使setCanceledOnTouchOutside(true)被調用,對話框也不會在屏幕上居中並且觸摸外部對話框也不會解除它。對話框背後的模糊背景

問題是:爲什麼這不起作用?分別如何創建模糊背景的對話框?

public class BlurDialog extends DialogFragment { 

public BlurDialog() { 
} 

public static BlurDialog newInstance() { 
    return new BlurDialog(); 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()) 
      .setTitle("Title") 
      .setMessage("Message") 
      .setPositiveButton("OK", null) 
      .setNegativeButton("Cancel", null) 
      .create(); 
    alertDialog.setCanceledOnTouchOutside(true); 


    View view = getActivity().getWindow().getDecorView(); 
    view.setDrawingCacheEnabled(true); 
    Bitmap b1 = view.getDrawingCache(); 

    Rect frame = new Rect(); 
    getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
    int statusBarHeight = frame.top; 
    final int width = getActivity().getWindow().getDecorView().getWidth(); 
    final int height = getActivity().getWindow().getDecorView().getHeight(); 

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height-statusBarHeight); 

    //define this only once if blurring multiple times 
    RenderScript rs = RenderScript.create(getActivity()); 

    //this will blur the bitmapOriginal with a radius of 8 and save it in bitmapOriginal 
    final Allocation input = Allocation.createFromBitmap(rs, b); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory 
    final Allocation output = Allocation.createTyped(rs, input.getType()); 
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
    script.setRadius(8f); 
    script.setInput(input); 
    script.forEach(output); 
    output.copyTo(b); 

    alertDialog.getWindow().setBackgroundDrawable(new BitmapDrawable(getResources(), b)); 


    return alertDialog; 
} 
} 

Screenshot

+0

嘿,我也面臨着同樣的問題,你有沒有得到任何解決方案? – 2014-09-12 06:55:26

+0

[Blur BackGround Behind AlertDialog]可能重複(https://stackoverflow.com/questions/19311192/blur-background-behind-alertdialog) – Vaikesh 2017-06-15 09:13:01

回答

-1

這個職位是舊的,但你的信息:

要創建具有背景模糊對話框中你可以使用這個庫:

https://github.com/tvbarthel/BlurDialogFragment

您可以創建一個擴展BlurDialogFragment的類,並且可以在onCreateView方法中擴展自定義佈局。看下面的例子:

public class CustomDialogFragment extends BlurDialogFragment { 


@Override 
protected boolean isActionBarBlurred() { 
// Enable or disable the blur effect on the action bar. 
// Disabled by default. 
return true; 
} 

@Override 
protected int getBlurRadius() { 
// Allow to customize the blur radius factor. 
return 7; 
} 

@Override 
protected boolean isDimmingEnable() { 
// Enable or disable the dimming effect. 
// Disabled by default. 
return false; 
} 


@Override 
public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
        Bundle savedInstanceState) { 
View v = inflater.inflate(R.layout.dialog_fragment_layout, container, 
false); 


return v; 
} 

以從活動顯示對話框:

FragmentManager fragmentManager = getFragmentManager(); 
CustomDialogFragment cdf = new CustomDialogFragment(); 
cdf.show(fragmentManager,"yourTag"); 

`

+1

雖然此鏈接可能回答此問題,但最好包含在這裏回答並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [發表評論](/ review/low-quality-posts/16421697) – Drise 2017-06-14 18:52:46

+0

@Drise你是對的,我更新了答案 – fgueli 2017-06-15 09:14:23

0

檢查:https://stackoverflow.com/a/21278278/3891036

它很適合我。

OR

創建styles.xml

<style name="Theme.D1NoTitleDim" parent="android:style/Theme.Translucent"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:backgroundDimEnabled">true</item> 
    <item name="android:background">@android:color/transparent</item>   
</style> 

,然後在你的對話框

dialog = new Dialog(context,styles); 
+0

使用styles.xml讓背景變暗但不模糊 – fgueli 2017-06-15 09:17:43

+0

你可以試試這個:https:/ /stackoverflow.com/a/21278278/3891036 – Vaikesh 2017-06-15 09:26:44