Android实现图片转高斯模糊以及高斯模糊布局

Hadara ·
更新时间:2024-11-14
· 998 次阅读

第一个为大家介绍图片如何转高斯模拟:

1.方法的实现:

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 调用:

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

二、高斯模糊布局:

项目需求: 现有一个紫色背景图片, 相册图片覆盖在背景图片 , 一个Framlayout 覆盖在这个含有相册图片的背景图中 ,实现模糊盖在上面的高斯模拟效果:

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 .调用:

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> 您可能感兴趣的文章:Android实现图片的高斯模糊(两种方式)Android实现动态高斯模糊效果示例代码Android 实现图片模糊、高斯模糊、毛玻璃效果的三种方法Android RenderScript高斯模糊Android 实现高斯模糊效果且兼容低版本Android 动态高斯模糊效果教程Android 高仿微信语音聊天页面高斯模糊(毛玻璃效果)Android实现动态高斯模糊效果Android 摄像头高斯模糊的示例代码



布局 图片 高斯 高斯模糊 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号