美文网首页app开发
Android图片模糊效果

Android图片模糊效果

作者: i冰点 | 来源:发表于2016-11-27 23:32 被阅读219次

    在进行模糊的时候,可以先对原始图片进行压缩,然后选择一个合适的方式进行模糊。
    效果如下:

    模糊

    1、处理图片

    缩放、旋转图片

        private Bitmap getBitmap(Bitmap source) {
            //scaleFactor:压缩比
            Bitmap tempBitmap= Bitmap.createBitmap((int) (source.getWidth() / scaleFactor), (int) blurHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas=new Canvas(tempBitmap);
            canvas.scale(1/scaleFactor,-1*blurHeight/source.getHeight());
            canvas.translate(0, -source.getHeight());
            Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    //        ColorMatrix colorMatrix=new ColorMatrix();
    //        colorMatrix.setScale(0.8f,0.8f,0.8f,1);
    //        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
            canvas.drawBitmap(source, 0, 0, paint);
            return tempBitmap;
        }
    

    2、模糊图片

    1、fastblur
    stackblur的java实现,这里

    • 使用
    Blur.fastblur(context,tempBitmap,25);
    

    2、rs-stackblur
    stackblur的Renderscript实现,这里

    • 添加依赖
    compile 'com.commit451:NativeStackBlur:1.0.2'
    
    • 使用
    NativeStackBlur.process(tempBitmap, 60);
    

    其中,模糊半径越大,处理后的图片越模糊

    参考:NativeStackBlur 高斯模糊

    **3、RenderScript ** ScriptIntrinsicBlur
    RenderScript,一个强大的图片处理框架,可以使用ScriptIntrinsicBlur 实现高斯模糊的效果(API 17)。
    需要进行如下配置:

    defaultConfig {
        ......
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled  true
    }
    

    代码如下:

    public Bitmap blurBitmap(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(), Config.ARGB_8888);
            
        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(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: 0 < radius <= 25
        blurScript.setRadius(25.0f);
            
        //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;   
            
    }
    

    引用自:Android RenderScript 简单高效实现图片的高斯模糊效果

    栗子下载

    相关文章

      网友评论

        本文标题:Android图片模糊效果

        本文链接:https://www.haomeiwen.com/subject/tzknettx.html