验证中...
码云 IDEA 插件最新版本发布,支持 Pull Request
gistfile1.txt
原始数据 复制代码
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private Bitmap createBlurBitmap(Bitmap srcBmp) {
Bitmap inputBitmap = Bitmap.createScaledBitmap(srcBmp, 200, 200, false);
Bitmap bitmap = Bitmap.createBitmap(inputBitmap);
//创建RenderScript,ScriptIntrinsicBlur固定写法
RenderScript renderScript = RenderScript.create(this);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
//根据inputBitmap,outputBitmap分别分配内存
Allocation tmpIn = Allocation.createFromBitmap(renderScript, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(renderScript, bitmap);
blurScript.setRadius(15);
blurScript.setInput(tmpIn);
blurScript.forEach(tmpOut);
tmpOut.copyTo(bitmap);
return bitmap;
}
/**
* 对View截图转成Bitmap
* <p>
* 有两种方法:
* 1:
* DrawingCache方法:
* 如果使用DrawingCache,对要截图的View有一个要求:View本身已经显示在界面上.如果View没有添加到界面上或者没有显示绘制过,则buildDrawingCache会失败。这种方式比较适合对应用界面或者某一部分的截图.
* 2:
* View.draw方法:view必须有高和宽
*/
private Bitmap view2Bitmap(View view, int type, int width, int height) {
Bitmap outputBitmap = null;
switch (type) {
case 1:
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();//启用DrawingCache并创建位图
outputBitmap = Bitmap.createBitmap(view.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
view.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
break;
case 2:
outputBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.WHITE); /** 如果不设置canvas画布为白色,则生成透明 */
view.layout(0, 0, view.getWidth(), view.getHeight());
view.draw(canvas);
break;
case 3: // 需要给view确定高宽
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(0);
float alpha = view.getAlpha();
view.setAlpha(1.0f);
if (color != 0) {
view.destroyDrawingCache();
}
int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
view.layout(0, 0, width, height);
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + view + ")",
new RuntimeException());
return null;
}
outputBitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
view.setAlpha(alpha);
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
break;
default:
break;
}
return outputBitmap;
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助