Android 实现高斯模糊效果
16lz
2021-01-23
方法一的原文链接未查询到,对原创作者表示歉意。
目前我知道的有2中办法
第一种,调用Android的API完成,但是支持API17及以上
第二种,Github的方法,每个像素点去做模糊,链接:https://github.com/kikoso/android-stackblur/blob/master/StackBlur/src/com/enrique/stackblur/JavaBlurProcess.java
方法一:
public class RSBlurProcess implements BlurProcess { private Context context; public RSBlurProcess(Context context) { this.context = context; } /** * * @param original 需要模糊的bitmap * @param radius 模糊的半径,最大值25 * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @Override public Bitmap blur(Bitmap original, float radius) { long start = System.currentTimeMillis(); //Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888); //Instantiate a new Renderscript RenderScript rs = RenderScript.create(context); //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, original); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //Set the radius of the blur blurScript.setRadius(radius); //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 original.recycle(); //After finishing everything, we destroy the Renderscript. rs.destroy(); return outBitmap; }}
第二种方法代码较多,建议直接去github查看。
建议尽可能不要在客户端做高斯模糊,因为内存开销太大,如果不是特殊需求(如模糊需要动态变化),尽可能直接服务端做模糊。
更多相关文章
- MTK 添加宏控方法
- Android AsyncTask onProgressUpdate 方法的些许研究
- Android Studio中genymotion安装方法
- Android使用HttpClient方法和易错问题
- Android开机自启动程序设置及控制方法思路浅谈
- android获取当前运行Activity名字的方法
- android画任意曲线时,去除移动过程中出现莫名直线的方法
- Android开发包下载(包括开发所需所有安装包的下载方法与地址)
- Android获取系统时间方法的总结