2011-07-22 12 views
66

透明なだけでなく、ぼかし効果も持つ背景を持つビューを作成しようとしています。そうすることで、下のビューは焦点が外れているように見えます。電源ボタンを押したまま画面が見えるようにしたい。何か案は?ぼんやりした透明な背景効果を作成する

答えて

118

ウィンドウフラグが廃止されたので、自分自身をぼかす必要があります。他の場所でこれに答えましたが、ビューをぼかす方法は次のとおりです。

レンダースクリプトライブラリのScriptIntrinsicBlurをすぐにぼかして使用できるようになりました。 HereはRenderScript APIへのアクセス方法です。以下は、私がビューとビットマップをぼかすために作られたクラスです。

import android.support.v8.renderscript.*; 

public class BlurBuilder { 
    private static final float BITMAP_SCALE = 0.4f; 
    private static final float BLUR_RADIUS = 7.5f; 

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

onCreateViewに以下を追加し、フラグメントにこれを適用するには:

final Activity activity = getActivity(); 
final View content = activity.findViewById(android.R.id.content).getRootView(); 
if (content.getWidth() > 0) { 
    Bitmap image = BlurBuilder.blur(content); 
    window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image)); 
} else { 
    content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      Bitmap image = BlurBuilder.blur(content); 
      window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image)); 
     } 
    }); 
} 

注:このソリューションは、に分SDKが必要ですAPIも17

EDIT:のrenderScriptはサポートV8に含まれているこの回答ダウを有効にします(このanswerから)あなたのGradleファイルに次の行を含めGradleのを使用してそれを有効にして、パッケージandroid.support.v8.renderscriptからのrenderScriptを使用するためのAPI 8にN:

android { 
    ... 
    defaultConfig { 
    ... 
    renderscriptTargetApi *your target api* 
    renderscriptSupportModeEnabled true 
    } 
    ... 
} 
+1

あなたは揺れます。これは真剣に私を助けました。私が追加したことの1つは、ぼかし関数から返されるビットマップがTransitionDrawableだということです。これは、ぼかし効果をよりスムーズに見せるのに役立ちます。 –

+0

これは素晴らしいです。ありがとうございました! –

+1

BITMAP_SCALE定数とBLUR_RADIUS定数がどのように機能するかについての注意が必要ですか?ありがとうございます –

15

あなたがしたいすべては、あなたが(窓を持ってAlertDialogのように、または任意のクラス)あなたの活動の中から、このコールを使用することができ、あなたのactivtyの後ろのウィンドウの背景をぼかしている場合

getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 

更新:このフラグはAndroid 4.0では廃止されましたが、もう動作しません

+0

私が探していたものです。それは有り難いです! –

+3

これは廃止され、Android Lでは動作しなくなりました。 –

+1

これは廃止予定です。回答を編集してください –

0

ぼやけがいいですそれはプロジェクト、あなたがでframeLayoutにぼかしたいビューをラップ、あなたはぼかしたいときに、このラインを使用するために追加した後 https://github.com/wasabeef/Blurry

簡単にあなたが探している効果を与えるオプション10
Blurry.with(this).radius(25).sampling(2).onto((ViewGroup) findViewById(R.id.viewToBlurWrapper)); 
関連する問題