动态高斯模糊适配不同尺寸

本文介绍了如何在不同尺寸的UI中适配动态高斯模糊效果。首先,通过截图接口获取屏幕图片,然后根据UI计算需要模糊的区域,进行剪切,接着对剪切后的图片应用高斯模糊处理,最后将模糊后的图片设为背景。详细实现包括截图、剪切、模糊等步骤,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

不同UI大小尺寸不同需要在特定区域进行高斯模糊适配,例如system ui下拉高度不同背景高斯模糊不同,或弹框背景区域做高斯模糊处理。

 

原理

1.截取当前屏幕图片(使用截图接口)

2.根据当前UI计算位置,大小

3.在截图bitmap上截取图片

4.高斯模糊处理截取后的图片

5.将图片设置为背景

 

具体实现

1.截取当前屏幕图片(使用截图接口)

 public static String screenShotByShell(Context context){
        // 获取内置当前应用路径
        String sdCardPath = context.getCacheDir().getAbsolutePath()+screenShotPath;
        File dir = new File(sdCardPath);
        if(!dir.exists()){
            dir.mkdirs();
        }else{
            delteDirectoryFiles(sdCardPath);
        }
        // 图片文件路径
        String filePath = sdCardPath + File.separator +"screenshot-"+System.currentTimeMillis()+".png";
        String shotCmd = "screencap -p " + filePath + " \n";
        try {
            Runtime.getRuntime().exec(shotCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }

2.计算位置剪切图片

public static Bitmap cropShotBitmap(String path,View view){
        int outWidth = view.getWidth();
        int outHeight = view.getHeight();
        int[] result = new int[2];
        view.getLocationOnScreen(result);
        int x = result[0];
        int y = result[1];
        Bitmap bitmap = ScreenShotUtil.cropBitmap(path,outWidth,outHeight,x,y);
        Log.d(TAG,"screenShotByShell path = "+path+" x = "+x+" y = "+y+" outWidth = "+outWidth+" outHeight = "+outHeight);
        return bitmap;
    }

public static Bitmap cropBitmap(String screenShotPath,int outWidth, int outHeight, int x, int y){
        Bitmap bitmap = BitmapFactory.decodeFile(screenShotPath);
        if(bitmap != null){
            //从屏幕整张图片中截取指定区域
            try {
                if(outHeight > bitmap.getHeight()){
                    outHeight = bitmap.getHeight();
                }
                if(outWidth > bitmap.getWidth()){
                    outWidth = bitmap.getWidth();
                }
                if(x < 0){
                    x = 0;
                }
                if(y < 0){
                    y = 0;
                }
                bitmap = Bitmap.createBitmap(bitmap, x, y, outWidth, outHeight);
                //File file = new File(screenShotPath);
                //file.delete();
               // String path = screenShotPath.replace(".png",System.currentTimeMillis()+".png");
                //onSaveBitmap(bitmap,screenShotPath);
                return bitmap;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        if(bitmap != null){
            Log.d(TAG,"cropBitmap sucess");
        }else{
            Log.d(TAG,"cropBitmap fail");
        }
        return null;
    }

3.高斯模糊处理

/**
     * 使用RenderScript实现高斯模糊的算法
     * @param bitmap
     * @return
     */
    public static Bitmap blur(Context context,Bitmap bitmap,int radius){
        //Let's create an empty bitmap with the same size of
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值