RenderScript是Android系統(tǒng)中能高效處理大量計算任務(wù)的框架,特別適用一些需要處理圖片和加載圖片以及計算機視覺的方面應(yīng)用。它是Android平臺的一種類C腳本語言,文件名為xxx.rs。后期google 會不斷擴展這個引擎讓其支持更多的精密計算。
引入方式:
defaultConfig {
applicationId "com.example.renderscript.renderscriptuse"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}
- renderscriptTargetApi - 指定生成的字節(jié)碼版本。
- renderscriptSupportModeEnabled - 如果運行的設(shè)備不支持該目標(biāo)版本,那么可以指定生成的字節(jié)碼回落到一個兼容的版本。
- 考慮到兼容性,在使用RenderScript的類中,要引入:import android.support.v8.renderscript.*;
高斯模糊功能:
效果:

我的女神李若彤
public static Bitmap handleGlassblur(Context context, Bitmap originBitmap, int radius){
RenderScript renderScript = RenderScript.create(context);
Allocation input = Allocation.createFromBitmap(renderScript,originBitmap);
Allocation output = Allocation.createTyped(renderScript,input.getType());
ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
scriptIntrinsicBlur.setRadius(radius);
scriptIntrinsicBlur.setInput(input);
scriptIntrinsicBlur.forEach(output);
output.copyTo(originBitmap);
renderScript.destroy();
input.destroy();
output.destroy();
return originBitmap;
}
radius設(shè)置模糊度,范圍1-25
- 創(chuàng)建一個原圖Allcation(配置,相當(dāng)于容器),創(chuàng)建一個新圖Allcation
- 創(chuàng)建ScriptIntrinsicBlur處理類,設(shè)置模糊度radius,將Allocation中的產(chǎn)品輸出
- 釋放資源
調(diào)用:
private void blur() {
imageviewBlur = (ImageView) findViewById(R.id.imageview);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.test);
Bitmap blurBitmap = HandleUtils.handleGlassblur(getApplicationContext(),bitmap,15);
imageviewBlur.setImageBitmap(blurBitmap);
}
最早我使用的是fastblur,那計算速度明顯感覺到倒數(shù)了3秒才出來的。rendenscript處理速度是fastblur的8倍。
加強版高斯模糊
默認(rèn)使用的模糊度不夠,可以通過縮小原圖的像素值,達到更大的模糊度。調(diào)用handleGlassblur()方法之前生成更小比例的bitmap。scaleRatio為縮小比例
調(diào)用:
private void scale(){
imageviewScale = (ImageView) findViewById(R.id.imageview2);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.test);
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 8, bitmap.getHeight() / 8, false);
Bitmap blurBitmap = HandleUtils.handleGlassblur(getApplicationContext(),newBitmap,15);
imageviewScale.setImageBitmap(blurBitmap);
}
效果:

我的女神李若彤
放大鏡效果

giphy.gif
我們編寫完rs文件之后,就會在gen目錄下,生成一個ScriptC_xxx.java文件,如下

rs文件位置
簡單自定義Imageview,點擊放大區(qū)域,也可以改成ACTION_MOVE事件,拖動實時放大查看。
public class MirrorScaleImageView extends AppCompatImageView{
private Bitmap bitmap;
public MirrorScaleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.test);
setImageBitmap(bitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.test);
Bitmap newBitmap = scaleMirror(bitmap,x,y,200,2,getContext());
setImageBitmap(newBitmap);
break;
}
return true;
}
/**
*
* @param bitmap
* @param x 圓心x
* @param y 圓心y
* @param radius 圓半徑
* @param scale 放大倍數(shù)
* @param context
* @return
*/
private Bitmap scaleMirror(Bitmap bitmap, int x, int y, int radius, int scale, Context context){
RenderScript rs = RenderScript.create(context);
Allocation in = Allocation.createFromBitmap(rs, bitmap);
Allocation out = Allocation.createTyped(rs,in.getType());
ScriptC_magnifier magnifier = new ScriptC_magnifier(rs);
magnifier.set_inputAllocation(in);
magnifier.set_atX(x);
magnifier.set_atY(y);
magnifier.set_radius(radius);
magnifier.set_scale(scale);
magnifier.forEach_magnify(in,out);
out.copyTo(bitmap);
rs.destroy();
magnifier.destroy();
in.destroy();
out.destroy();
return bitmap;
}
}
素描效果:

image.png
public static Bitmap blackHandle(Bitmap bitmap, Context context){
RenderScript renderScript = RenderScript.create(context);
ScriptC_sketch sketchScript = new ScriptC_sketch(renderScript);
Allocation in = Allocation.createFromBitmap(renderScript,bitmap);
Allocation out = Allocation.createTyped(renderScript,in.getType());
sketchScript.forEach_invert(in,out);
out.copyTo(bitmap);
renderScript.destroy();
sketchScript.destroy();
in.destroy();
out.destroy();
return bitmap;
}
RenderScript功能會