您的位置:首页 > 移动开发 > Android开发

模糊背景处理--当前界面背景

2015-08-24 16:20 656 查看
获取手机墙纸或当前activity背景,做模糊处理,此方法有些耗时!

rootView为当前要设置的布局:

/**

* 以下为获取模糊背景代码

*/

private
void applyBlur() { 

 

// 获取壁纸管理器  

// WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());  

// // 获取当前壁纸  

// Drawable wallpaperDrawable = wallpaperManager.getDrawable();  

// // 将Drawable,转成Bitmap  

// Bitmap
bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();  

blur(myShot((Activity)context)); 



/**

* 获取当前显示的activity背景

* @param activity

* @return

*/

public Bitmap myShot(Activity activity) {

// 获取windows中最顶层的view

View view = activity.getWindow().getDecorView();

view.buildDrawingCache();

// 获取状态栏高度

// Rect
rect = new
Rect();

// view.getWindowVisibleDisplayFrame(rect);

// int statusBarHeights = rect.top;

Display display = activity.getWindowManager().getDefaultDisplay();

// 获取屏幕宽和高

int widths = display.getWidth();

int heights = display.getHeight();

// 允许当前窗口保存缓存信息

view.setDrawingCacheEnabled(true);

// 去掉状态栏

// Bitmap
bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,statusBarHeights, widths, heights – statusBarHeights);

Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,0, widths, heights);

// 销毁缓存信息

view.destroyDrawingCache();

return bmp;

}

private
void blur(Bitmap bkg) { 

long startMs = System.currentTimeMillis(); 

float radius = 20; 

bkg = small(bkg);

Bitmap bitmap = bkg.copy(bkg.getConfig(),
true);

final RenderScript rs = RenderScript.create(this.getContext());

final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,

Allocation.USAGE_SCRIPT);

final Allocation output = Allocation.createTyped(rs, input.getType());

final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

script.setRadius(radius);

script.setInput(input);

script.forEach(output);

output.copyTo(bitmap);

bitmap = big(bitmap);

rootView.setBackground(new BitmapDrawable(context.getResources(), bitmap)); 

rs.destroy(); 

LddLogUtils.d("jack.log","blur take away:" + (System.currentTimeMillis() - startMs )+
"ms"); 



private
static Bitmap big(Bitmap bitmap) {

  Matrix matrix =
new Matrix(); 

  matrix.postScale(4f,4f);
//长和宽放大缩小的比例

  Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

  return resizeBmp;

}

private
static Bitmap small(Bitmap bitmap) {

  Matrix matrix =
new Matrix(); 

  matrix.postScale(0.25f,0.25f);
//长和宽放大缩小的比例

  Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

  return resizeBmp;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 界面