Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊以及高斯模糊布局
第一個(gè)為大家介紹圖片如何轉(zhuǎn)高斯模擬:
1.方法的實(shí)現(xiàn):
public static void updateBgToBlur(Activity a, Bitmap bmpToBlur, View view, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
opt.inSampleSize = 8;
opt.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), resId, opt);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(null);
} else {
view.setBackgroundDrawable(null);
}
if (bmpToBlur != null && !bmpToBlur.isRecycled()) {
bmpToBlur.recycle();
}
bmpToBlur = blurBitmap(a, bmp);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(new BitmapDrawable(a.getResources(), bmpToBlur));
} else {
view.setBackgroundDrawable(new BitmapDrawable(a.getResources(), bmpToBlur));
}
}
public static Bitmap blurBitmap(Context c, Bitmap bitmap) {
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(c.getApplicationContext());
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
2 調(diào)用:
Bitmap bitmap=null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
ImageUtil.updateBgToBlur(getActivity(), bitmap, slidingUpPanelLayout, R.drawable.bg_tageditor);
} else {
slidingUpPanelLayout.setBackgroundResource(R.drawable.bg_tageditor);
}
二、高斯模糊布局:
項(xiàng)目需求: 現(xiàn)有一個(gè)紫色背景圖片, 相冊(cè)圖片覆蓋在背景圖片 , 一個(gè)Framlayout 覆蓋在這個(gè)含有相冊(cè)圖片的背景圖中 ,實(shí)現(xiàn)模糊蓋在上面的高斯模擬效果:
1 引用BlurView:
compile 'com.eightbitlab:supportrenderscriptblur:1.0.0'
compile 'com.eightbitlab:blurview:1.3.3'
defaultConfig {
renderscriptTargetApi 25 //must match target sdk and build tools, 23+
renderscriptSupportModeEnabled true
}
2 .調(diào)用:
final float radius = 20;
final View decorView = getActivity().getWindow().getDecorView();
//Activity's root View. Can also be root View of your layout (preferably)
final ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content);
//set background, if your root layout doesn't have one
final Drawable windowBackground = decorView.getBackground();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mBlurView.setupWith(rootView)
.windowBackground(windowBackground)
.blurAlgorithm(new RenderScriptBlur(getActivity()))
.blurRadius(radius);
}else {
mBlurView.setupWith(rootView)
.windowBackground(windowBackground)
.blurAlgorithm(new SupportRenderScriptBlur(getActivity()))
.blurRadius(radius);
}
3 xml
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:blurOverlayColor="@color/colorOverlay">
<!--Any child View here, TabLayout for example-->
</eightbitlab.com.blurview.BlurView>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載更多
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播
這篇文章主要手把手教你用ViewPager自定義實(shí)現(xiàn)Banner輪播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android判斷后臺(tái)服務(wù)是否開(kāi)啟的兩種方法實(shí)例詳解
這篇文章主要介紹了Android判斷后臺(tái)服務(wù)是否開(kāi)啟的方法的相關(guān)資料,這里提供了兩種方法及實(shí)例,需要的朋友可以參考下2017-07-07
什么是Android靜默拍攝 Android靜默拍攝app制作方法
這篇文章主要告訴大家什么是Android靜默拍攝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Flutter 底部彈窗如何實(shí)現(xiàn)多項(xiàng)選擇
在Flutter中提供了一個(gè)showModelBottomSheet方法用于彈出底部彈窗,本篇基于這個(gè)方法介紹實(shí)現(xiàn)底部彈窗多選的思路和方式。2021-06-06
Android開(kāi)發(fā)中TextView文本過(guò)長(zhǎng)滾動(dòng)顯示實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android開(kāi)發(fā)中TextView文本過(guò)長(zhǎng)滾動(dòng)顯示實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android項(xiàng)目開(kāi)發(fā)中TextView顯示超長(zhǎng)文本的具體操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-02-02
Android自定義AvatarImageView實(shí)現(xiàn)頭像顯示效果
這篇文章主要為大家詳細(xì)介紹了Android自定義AvatarImageView實(shí)現(xiàn)頭像顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
android:descendantFocusability方法介紹
開(kāi)發(fā)中很常見(jiàn)的一個(gè)問(wèn)題,項(xiàng)目中的listview不僅僅是簡(jiǎn)單的文字,常常需要自己定義listview,問(wèn)題就出現(xiàn)了,可能會(huì)發(fā)生點(diǎn)擊每一個(gè)item的時(shí)候沒(méi)有反應(yīng),無(wú)法獲取的焦點(diǎn)2012-11-11
實(shí)例講解Android App使用自帶的SQLite數(shù)據(jù)庫(kù)的基本方法
這篇文章主要介紹了Android App使用自帶的SQLite數(shù)據(jù)庫(kù)的基本方法,SQLite是一個(gè)小巧的內(nèi)嵌型數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)需求不大的情況下使用SQLite其實(shí)非常有效,需要的朋友可以參考下2016-04-04
Android ViewDragHelper仿淘寶拖動(dòng)加載效果
這篇文章主要為大家詳細(xì)介紹了Android ViewDragHelper仿淘寶拖動(dòng)加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

