码迷,mamicode.com
首页 > 移动开发 > 详细

Android模糊效果总结

时间:2017-07-24 16:20:11      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:tty   sdk   sdn   awb   .text   rri   软件   lte   版本号   

1. 软件模糊

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


效果图:
技术分享

关键模糊代码 github链接

原文 链接
译文 链接
演示样例 代码

本文地址 :http://blog.csdn.net/csqingchen/article/details/43817975

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

Android模糊效果总结

标签:tty   sdk   sdn   awb   .text   rri   软件   lte   版本号   

原文地址:http://www.cnblogs.com/wzzkaifa/p/7229134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!