【android开发】截取屏幕,高斯模糊后显示出来

Android开发:屏幕截图与高斯模糊实现
本文介绍了如何在Android应用中实现屏幕截图,并对截取的图片进行高斯模糊处理。首先,通过代码截取当前屏幕内容,然后利用RenderScript技术进行高效的高斯模糊操作,最终展示模糊后的效果。

先上效果。如下:
效果图

1、截取当前屏幕的内容(截屏)

/**
 *获取当前Activity的截图
 * 
 * @param activity
 *            需要被截取的Activity
 * @return 截图后的Bitmap对象
 */
public static Bitmap getScreenShot(Activity activity) {
    View decorView = activity.getWindow().getDecorView();
    View contentView = decorView.findViewById(android.R.id.content);
    contentView.setDrawingCacheEnabled(true);
    int tryNumber = 3;
    Bitmap cache = null;
    Bitmap bitmap = null;
    while (tryNumber > 0) {
        cache = contentView.getDrawingCache();
        if (cache != null) {
            bitmap = Bitmap.createBitmap(cache);
            break;
        }
        tryNumber--;
    }
    contentView.setDrawingCacheEnabled(false);
    return bitmap;
}

2、对图片进行高斯模糊处理(高斯模糊处理在我RenderScript实现高斯模糊中有写)

/**
 * 对Bitmap进行高斯模糊处理
 * 
 * @param bitmap
 *            需要处理的Bitmap对象(处理完毕后将会覆盖之)
 * @return 处理后的Bitmap对象
 */
public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
    RenderScript mRS = RenderScript.create(context);
    ScriptIntrinsicBlur mS = ScriptIntrinsicBlur.create(mRS,
            Element.U8_4(mRS));
    Allocation inAllocation = Allocation.createFromBitmap(mRS, bitmap);
    Allocation outAllocation = Allocation.createFromBitmap(mRS, bitmap);
    mS.setRadius(18);// 模糊程度:0~25之间,越大越模糊
    mS.setInput(inAllocation);
    mS.forEach(outAllocation);
    outAllocation.copyTo(bitmap);
    mRS.destroy();
    mRS = null;
    return bitmap;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值