Android学习------使用RenderScript ,ScriptIntrinsicBlur实现高斯模糊
2017-04-17 10:49
393 查看
android新增了android.support.v8.renderscript支持库,提供了高斯模糊的实现。
参考自:
https://developer.android.google.cn/guide/topics/renderscript/compute.html#using-rs-from-java
http://stackoverflow.com/questions/2067955/fast-bitmap-blur-for-android-sdk
Java code
注意在解码图片的时候可能会发生OOM
xml code
demo演示:
参考自:
https://developer.android.google.cn/guide/topics/renderscript/compute.html#using-rs-from-java
http://stackoverflow.com/questions/2067955/fast-bitmap-blur-for-android-sdk
Setup 1
在你的build.gradle中添加defaultConfig { (...) renderscriptTargetApi 18 renderscriptSupportModeEnabled true (...) }
Java code
public class MainActivity extends AppCompatActivity { private final static String TAG = "MainActivity"; private ImageView img; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.img); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap blurTemplate = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, options); RenderScript rs = RenderScript.create(MainActivity.this); final Allocation input = Allocation.createFromBitmap(rs, blurTemplate); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(8f); script.setInput(input); script.forEach(output); output.copyTo(blurTemplate); img.setImageBitmap(blurTemplate); } }); } }
注意在解码图片的时候可能会发生OOM
xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img" android:layout_width="200dp" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/img" android:text="start bulr" android:textAllCaps="false" /> </RelativeLayout>
demo演示:
相关文章推荐
- 使用 ScriptIntrinsicBlur 进行高斯模糊 出现图片花屏 解决
- Android学习-使用单例模式实现一键退出APP
- Android学习-使用单例模式实现一键…
- Android 学习笔记之SurfaceView的使用+如何实现视频播放...
- Android 学习笔记之使用多线程实现断点下载...
- 一起学习android使用一个回调函数onCreateDialog实现负载对话(23)
- Android学习备忘004——使用ViewFlipper实现滑动翻页
- Android NDK 学习(5) 使用CDT特性 实现自动化编译
- Android动画学习(三)之使用ValueAnimator和ObjectAnimator实现动画实例
- Android使用局和数据实现天气项目-android学习之旅(十二)
- Android学习之使用SQLite实现简单的(CRUD)增删改查
- Android之UI学习篇九:使用TabHost实现卡片选项菜单
- Android实现监听手机通话--仅供学习使用--
- Android学习之 使用ContentProvider实现数据共享
- 【android控件学习笔记】使用Spinner实现下拉列表
- Android学习-使用单例模式实现一键退出APP
- Android之UI学习篇十:使用TabHost实现微博客户端界面
- Android开发学习之使用百度语音识别SDK实现语音识别(下)
- Android动画学习(三)之使用ValueAnimator和ObjectAnimator实现动画实例
- android学习之使用AIDL实现进程间的通讯