亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android模糊圖片技術

標簽:
Android

RenderScript是android的计算图形框架。android的动态墙纸就是用了这一技术。本文学习一下把一张图片进行模糊显示处理。

集成Renderscript Support支持库

使用Gradle配置

在项目的build.gradle添加以下代码

[代码]xml代码:

?

1

2

3

4

5

6

android {

    defaultConfig {

        renderscriptTargetApi   19

        renderscriptSupportModeEnabled   true

    }

}

 

添加成功后,会发现android.support.v8.renderscript.*的类可以使用

使用RenderScript模糊图片

以下是模糊的核心代码

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

public class BlurBuilder   {

    private static final float BITMAP_SCALE = 0.4f;

    private static final float BLUR_RADIUS = 7.5f;

 

    public static Bitmap blur(Context context, Bitmap image) {

        int width = Math.round(image.getWidth() *   BITMAP_SCALE);

        int height = Math.round(image.getHeight() *   BITMAP_SCALE);

 

        Bitmap   inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);

        Bitmap   outputBitmap = Bitmap.createBitmap(inputBitmap);

 

        RenderScript   rs = RenderScript.create(context);

        ScriptIntrinsicBlur   theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        Allocation   tmpIn = Allocation.createFromBitmap(rs, inputBitmap);

        Allocation   tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

        theIntrinsic.setRadius(BLUR_RADIUS);

        theIntrinsic.setInput(tmpIn);

        theIntrinsic.forEach(tmpOut);

        tmpOut.copyTo(outputBitmap);

 

        return outputBitmap;

    }

}

 

只需要适当地调整BITMAP_SCALEBLUR_RADIUS这两个参数就可以了。
注意一点的是。导入的包一定要是android.support.v8.renderscript.*,不然就只能在Api 17(Android 4.3)的版本上使用。

如果要对一张Bitmap图片进行模糊,只需要调用上面的blur方法,生成一个新的Bitmap就可以了。

[代码]java代码:

?

1

2

Bitmap blurredBitmap = BlurBuilder.blur(   getActivity(), originalBitmap );

view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap   ) );

 

这是原图
https://img1.sycdn.imooc.com//5bd41219000134fc06640498.jpg

压缩后的图片
https://img1.sycdn.imooc.com//5bd4122700014e6b06620495.jpg

 原文链接:http://www.apkbus.com/blog-705730-60536.html


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消