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

作者:KevinSpaces 时间:2023-10-20 16:10:23 

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

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,高斯模糊
0
投稿

猜你喜欢

  • Android开发使用HttpURLConnection进行网络编程详解【附源码下载】

    2022-01-11 23:27:37
  • Android中使用TextView实现高仿京东淘宝各种倒计时效果

    2021-05-26 21:32:26
  • c# 可选参数、命名参数

    2022-08-06 05:38:41
  • java volatile关键字的含义详细介绍

    2021-11-01 16:52:17
  • Java网络编程UDP实现多线程在线聊天

    2022-01-08 21:48:32
  • 一口气说出Java 6种延时队列的实现方法(面试官也得服)

    2022-12-15 16:40:12
  • SpringBoot整合TKMyBatis实现单表增删改查操作

    2022-01-30 19:52:28
  • 解决异常FileNotFoundException:class path resource找不到资源文件的问题

    2021-12-26 18:24:14
  • java,android,MD5加密算法的实现代码(16位,32位)

    2022-07-12 20:40:10
  • 基于java math API 的详细解释说明

    2021-10-04 06:51:44
  • 完全解析Android多线程中线程池ThreadPool的原理和使用

    2022-04-07 20:19:40
  • Spring JDBC的使用方法详解

    2021-12-08 14:05:42
  • 深入理解Java虚拟机体系结构

    2021-09-15 19:46:53
  • Android Studio 运行按钮灰色的完美解决方法

    2023-08-16 05:59:42
  • 如何解决Spring in action @valid验证不生效的问题

    2023-08-29 07:59:56
  • Flutter 设置全局字体的实现

    2023-05-04 06:27:25
  • springboot操作静态资源文件的方法

    2022-07-13 06:29:11
  • 浅谈springboot之JoinPoint的getSignature方法

    2022-12-25 11:23:20
  • ConcurrentMap.putIfAbsent(key,value)用法实例

    2023-03-14 06:45:43
  • Android编程使用Service实现Notification定时发送功能示例

    2023-03-12 09:54:44
  • asp之家 软件编程 m.aspxhome.com