自定义View(9)使用Renderscript 渲染特效。

1.渲染脚本官网

 https://developer.android.com/guide/topics/renderscript/compute

2.高斯模糊 ScriptIntrinsicBlur

2.1 添加api

在module 的 build.gradle 文件中添加选项,注意是 defaultConfig中。

 1 android {
 2     compileSdkVersion 28
 3     defaultConfig {
 4         applicationId "com.cnblogs.sjjg.gaussianblur"
 5         minSdkVersion 17
 6         targetSdkVersion 28
 7         versionCode 1
 8         versionName "1.0"
 9         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
10         renderscriptTargetApi 18
11         renderscriptSupportModeEnabled true
12     }
13     buildTypes {
14         release {
15             minifyEnabled false
16             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17         }
18     }
19 }
20 
21 dependencies {
22     implementation fileTree(dir: 'libs', include: ['*.jar'])
23     implementation 'androidx.appcompat:appcompat:1.0.2'
24     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
25     testImplementation 'junit:junit:4.12'
26     androidTestImplementation 'androidx.test:runner:1.2.0'
27     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
28 }

2.2 添加模糊工具类

 1 package com.cnblogs.sjjg.gaussianblur;
 2 
 3 import android.content.Context;
 4 import android.graphics.Bitmap;
 5 import android.renderscript.Allocation;
 6 import android.renderscript.Element;
 7 import android.renderscript.RenderScript;
 8 import android.renderscript.ScriptIntrinsicBlur;
 9 
10 import androidx.annotation.IntRange;
11 import androidx.annotation.NonNull;
12 
13 public class RenderScriptGaussianBlur {
14     private RenderScript renderScript;
15 
16     public RenderScriptGaussianBlur(@NonNull Context context) {
17         this.renderScript = RenderScript.create(context);
18     }
19 
20     public Bitmap gaussianBlur(@IntRange(from = 1, to = 25) int radius, Bitmap original) {
21         Allocation input = Allocation.createFromBitmap(renderScript, original);
22         Allocation output = Allocation.createTyped(renderScript, input.getType());
23         ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
24         scriptIntrinsicBlur.setRadius(radius);
25         scriptIntrinsicBlur.setInput(input);
26         scriptIntrinsicBlur.forEach(output);
27         output.copyTo(original);
28         return original;
29     }
30 }

2.3 使用模糊类

 1 package com.cnblogs.sjjg.gaussianblur;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.graphics.Bitmap;
 6 import android.graphics.BitmapFactory;
 7 import android.os.Bundle;
 8 import android.widget.ImageView;
 9 import android.widget.SeekBar;
10 
11 public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
12 
13 
14     private         SeekBar                     blurValue;
15     private         ImageView                   image;
16     private         RenderScriptGaussianBlur    blur;
17 
18 
19     @Override
20     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
21 
22     }
23 
24     @Override
25     public void onStartTrackingTouch(SeekBar seekBar) {
26 
27     }
28 
29     @Override
30     public void onStopTrackingTouch(SeekBar seekBar) {
31         int radius = seekBar.getProgress();
32         if (radius < 1) {
33             radius = 1;
34         }
35         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image);
36         bitmap = blur.gaussianBlur(radius,bitmap);
37         image.setImageBitmap(bitmap);
38     }
39 
40     @Override
41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.activity_main);
44         image = findViewById(R.id.imageView);
45         blurValue = findViewById(R.id.blur_value);
46         blurValue.setOnSeekBarChangeListener(this);
47         blur = new RenderScriptGaussianBlur(getBaseContext());
48     }
49 }

2.4 效果

2.5 下载

  https://github.com/f9q/gaussianblur

posted @ 2016-02-18 23:47  f9q  阅读(352)  评论(0编辑  收藏  举报