一、实现效果图及说明
二、实现步骤
1、在armeabi-v7a文件夹下添加两个库文件(librsjni.so,libRSSupport.so)以及在libs文件夹下添加一个架包renderscript-v8.jar。
下载地址:http://download.csdn.net/detail/lmz14/8579719
2、在菜单界面(这里是popuwindow菜单界面)加载时,通过预览对菜单底部当前Activity显示的View保存成Bitmap形式,然后将Bitmap进行高斯模糊,最后将高斯模糊后的图设置为菜单的背景(即lin_menu的背景),关键代码如下所示。
(1)菜单界面加载时,进行预览,这里对收藏图标(id=“im_collection_dynamic”)添加预览监听事件,代码如下所示。
im_collection_dynamic.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
im_collection_dynamic.getViewTreeObserver().removeOnPreDrawListener(this);
im_collection_dynamic.buildDrawingCache();
if(v_bkg!=null){
Bitmap image = convertViewToBitmap(v_bkg);
blur(image,lin_menu);
}
return true;
}
});
(2)将当前Activity显示的View保存成Bitmap形式,代码如下所示。
(3)将Bitmap进行高斯模糊后设置为菜单的背景(即lin_menu的背景),代码如下所示。
/**
* 高斯模糊
* @param bkg
* @param view
*/
private void blur(Bitmap bkg, View view) {
float radius = 25;
Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
(int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.translate(-view.getLeft(), -view.getTop());
canvas.drawBitmap(bkg, 0, 0, null);
RenderScript rs = RenderScript.create(mContext);
Allocation overlayAlloc = Allocation.createFromBitmap(
rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(radius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(overlay);
view.setBackgroundDrawable(new BitmapDrawable(
mContext.getResources(), overlay));
rs.destroy();
}
(4)若Activity布局带有滚动条,则需要对滚动进行监听,实时跟新变量v_bkg(即更新整个Activity当前显示的界面画面),代码如下。
对滚动变化监听:
list_comment.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(spinnerMenuDialog != null){
spinnerMenuDialog.updateView(v_bkg);
}
}
});
注:View v_bkg = (RelativeLayout) findViewById(R.id.rala_main);//R.id.rala_main是Activity布局文件根布局id
更新变量v_bkg:
/**
* 监听scollView,更新当前的View
* @param vBkg
*/
public void updateView(View vBkg){
v_bkg = vBkg;
}