zoukankan      html  css  js  c++  java
  • Android 图片高级绘图效果---高斯模糊

    高斯模糊就是将指定像素变换为其与周边像素加权平均后的值,权重就是高斯分布函数计算出来的值。高斯模糊能够将图片制作成类似磨砂的图片效果,一般这些图片都用来作为背景。
    目前使用到的是RenderScript ,其特点是使用起来比较方便,虽然效率不是很高,但是也能够满足目前的使用。使用流程如下:

    1. 添加混淆配置

    -keepclasseswithmembernames class * {
        native <methods>;
    }
    -keep class android.support.v8.renderscript.** { *; }

     2.使用高斯模糊

    private void blur(Bitmap bkg, View view) {
    
            float radius = 25;
    
            Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()), (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
    
            float scaleW = (float) view.getMeasuredWidth() / (float) bkg.getWidth();
            float scaleH = (float) view.getMeasuredHeight() / (float) bkg.getHeight();
    
            Matrix matrix = new Matrix();
            matrix.postScale(scaleW, scaleH);
            Bitmap resizeBmp = Bitmap.createBitmap(bkg, 0, 0, bkg.getWidth(), bkg.getHeight(), matrix, true);
    
            Canvas canvas = new Canvas(overlay);
    
            canvas.translate(-view.getLeft(), -view.getTop());
            canvas.drawBitmap(resizeBmp, 0, 0, null);
    
            RenderScript rs = RenderScript.create(this);
    
            Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
    
            ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    
            blur.setInput(overlayAlloc);
    
            blur.setRadius(radius);
    
            blur.forEach(overlayAlloc);
    
            overlayAlloc.copyTo(overlay);
            view.setBackground(new BitmapDrawable(getResources(), overlay));
            rs.destroy();
        } 

  • 相关阅读:
    [D3] 4. d3.max
    [D3] 3. Scaling Basics
    [D3] 2. Basics of SVG
    [PHP] find ascii code in string
    [PHP] csv to xml
    [AngularJS] angular-formly: Extending Types
    [R] Draw a wordcloud
    [AngularJS] Error: $location:nobase
    [Whole Web] [Node.js, PM2] Controlling runaway apps using pm2
    Runoob-Java-高级教程-实例-环境设置实例:3.Java 实例
  • 原文地址:https://www.cnblogs.com/renhui/p/9528316.html
Copyright © 2011-2022 走看看