博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android模糊效果总结
阅读量:4649 次
发布时间:2019-06-09

本文共 3370 字,大约阅读时间需要 11 分钟。

1. 软件模糊

用软件的方法。利用cpu计算,无sdk版本号要求。

效果图:
效果图

关键模糊代码

原文

译文
演示样例

本文地址 :

2. 使用RenderScript模糊,

ScriptIntrinsicBlur要求android sdk版本号最低17。

google的官方文档

具体使用代码例如以下:

/**     * 通过调用系统高斯模糊api的方法模糊     *     * @param bitmap    source bitmap     * @param outBitmap out bitmap     * @param radius    0 < radius <= 25     * @param context   context     * @return out bitmap     */ public static Bitmap blurBitmap(Bitmap bitmap, Bitmap outBitmap, float radius, Context context) {        //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(), 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, bitmap);        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//        bitmap.recycle();        //After finishing everything, we destroy the Renderscript.        rs.destroy();        return outBitmap;    }

3. bitmap缩放处理

以上两种模糊方法,bitmap尺寸越小,处理的越迅速。特别是方法一。为了达到须要的模糊效果,通常我们须要对源bitmap缩放的处理。缩放代码例如以下:

/**     * 比例压缩图片     *     * @param sourceBitmap 源bitmap     * @param scaleFactor  大于1。将bitmap缩小     * @return 缩小scaleFactor倍后的bitmap     */    public static Bitmap compressBitmap(Bitmap sourceBitmap, float scaleFactor) {        Bitmap overlay = Bitmap.createBitmap((int) (sourceBitmap.getWidth() / scaleFactor),                (int) (sourceBitmap.getHeight() / scaleFactor), Config.ARGB_8888);        Canvas canvas = new Canvas(overlay);        canvas.translate(0, 0);        canvas.scale(1 / scaleFactor, 1 / scaleFactor);        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);        canvas.drawBitmap(sourceBitmap, 0, 0, paint);        return overlay;    }

4. 改变图片的对照度/明暗度

具体说明见代码凝视,演示样例代码

/**     * 改变图片对照度,达到使图片明暗变化的效果     *     * @param srcBitmap source bitmap     * @param contrast  图片亮度。0:全黑。小于1,比原图暗;1.0f原图;大于1比原图亮     * @return bitmap     */    public static Bitmap darkBitmap(Bitmap srcBitmap, float contrast) {        float offset = (float) 0.0; //picture RGB offset        int imgHeight, imgWidth;        imgHeight = srcBitmap.getHeight();        imgWidth = srcBitmap.getWidth();        Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888);        ColorMatrix cMatrix = new ColorMatrix();        cMatrix.set(new float[]{contrast, 0, 0, 0, offset,                0, contrast, 0, 0, offset,                0, 0, contrast, 0, offset,                0, 0, 0, 1, 0});        Paint paint = new Paint();        paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));        Canvas canvas = new Canvas(bmp);        canvas.drawBitmap(srcBitmap, 0, 0, paint);        return bmp;    }

转载于:https://www.cnblogs.com/wzzkaifa/p/7229134.html

你可能感兴趣的文章
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>
lambda表达式树
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>
第一阶段冲刺3
查看>>
父类引用指向子类对象
查看>>
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>
创建 PSO
查看>>