Android中實(shí)現(xiàn)布局背景模糊化處理的方法
在模仿 IOS 密碼輸入頁(yè)面的時(shí)候發(fā)現(xiàn)其背景有模糊處理,于是了解了一下并記錄下來,以便使用.在Android 中具體實(shí)現(xiàn)方法如下
查考 http://www.dbjr.com.cn/article/64781.htm
private void applyBlur() {
// 獲取壁紙管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// 獲取當(dāng)前壁紙
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 將Drawable,轉(zhuǎn)成Bitmap
Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
blur(bmp);
}
下面之所以要進(jìn)行small 和big的處理,是因?yàn)閮H僅靠ScriptIntrinsicBlur 來處理模式,不能到達(dá)更模式的效果,如果需要加深模式效果就需要先把背景圖片縮小,在處理完之后再放大.這個(gè)可以使用Matrix 來實(shí)現(xiàn),而且這樣可以縮短模糊化得時(shí)間
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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);
setBackground(new BitmapDrawable(getResources(), bitmap));
rs.destroy();
Log.d("zhangle","blur take away:" + (System.currentTimeMillis() - startMs )+ "ms");
}
private static Bitmap big(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(4f,4f); //長(zhǎng)和寬放大縮小的比例
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); //長(zhǎng)和寬放大縮小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
- Android一行代碼實(shí)現(xiàn)圓形頭像
- Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0)
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
- android dialog背景模糊化效果實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)個(gè)人資料頁(yè)面頭像背景模糊顯示包(狀態(tài)欄)
- Android實(shí)現(xiàn)用戶圓形頭像和模糊背景
相關(guān)文章
Android drawable微技巧,你不知道的drawable細(xì)節(jié)
今天小編就為大家分享一篇關(guān)于Android drawable微技巧,你不知道的drawable細(xì)節(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android 仿小米鎖屏實(shí)現(xiàn)九宮格解鎖功能(無需圖片資源)
最近公司要求做個(gè)九宮格解鎖,本人用的是小米手機(jī),看著他那個(gè)設(shè)置鎖屏九宮格很好看,就做了該組件,不使用圖片資源,純代碼實(shí)現(xiàn),感興趣的朋友參考下吧2016-12-12
Android 消息機(jī)制以及handler的內(nèi)存泄露
這篇文章主要介紹了Android 消息機(jī)制以及handler的內(nèi)存泄露的相關(guān)資料,需要的朋友可以參考下2016-09-09
淺談Android安全風(fēng)險(xiǎn)與防范措施
這篇文章主要介紹了淺談Android安全風(fēng)險(xiǎn)與防范措施,對(duì)安全感興趣的同學(xué)可以參考下2021-04-04
Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法
這篇文章主要介紹了Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

