Android截屏并做模糊处理

点击事件中调用
View view;


view = MainActivity.this.getWindow().getDecorView();

initPopuptWindow(view);
//释放资源,防止多次截屏后显示的是同一张图片
view.destroyDrawingCache();


private void initPopuptWindow(View layout) {
//  private void initPopuptWindow(Bitmap screen) {
        // TODO Auto-generated method stub
        //对当前页面进行截屏
        layout.setDrawingCacheEnabled(true);
        layout.buildDrawingCache();  //启用DrawingCache并创建位图
//      Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
        Bitmap screen = Bitmap.createBitmap(getViewBitmap(layout)); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
//      layout.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
//      Log.e("Alex", "转换前bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight());
        screen = scaleBitmap(screen, screen.getWidth()/2, screen.getHeight()/2);//压缩bitmap到指定大小
//      Log.e("Alex", "转换后bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight());
        //将截屏进行模糊
        screen = Blur.fastblur(MainActivity.this, screen, 10);
        Log.e("alex","screen是否为空"+screen);



        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        final View popupWindow_view = getLayoutInflater().inflate(R.layout.home_add_popuwind, null, false);
        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
        background.setImageBitmap(screen);
        background.setVisibility(View.VISIBLE);
        final int screenWidth = getScreenWidth(this);
        WindowManager wm1 = MainActivity.this.getWindowManager();
        int width1 = wm1.getDefaultDisplay().getWidth();
//      popupWindow = new PopupWindow(popupWindow_view, width1 - 200, LinearLayout.LayoutParams.WRAP_CONTENT); //设置popwindow的大小
        popupWindow = new PopupWindow(popupWindow_view, width1, LinearLayout.LayoutParams.WRAP_CONTENT); //设置popwindow的大小
        popupWindow_view.findViewById(R.id.a5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                background.setVisibility(View.GONE);
            }
        });
        popupWindow_view.findViewById(R.id.a1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "写问卷", Toast.LENGTH_SHORT).show();
            }
        });
        popupWindow_view.findViewById(R.id.a2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "写调查", Toast.LENGTH_SHORT).show();
            }
        });
        popupWindow_view.findViewById(R.id.a3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "写报表", Toast.LENGTH_SHORT).show();
            }
        });
        popupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    /**
     * Get the screen width.
     *
     * @param context
     * @return the screen width
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static int getScreenWidth(Activity context) {

        Display display = context.getWindowManager().getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point size = new Point();
            display.getSize(size);
            return size.x;
        }
        return display.getWidth();
    }



    /**
     * 把一个bitmap压缩,压缩到指定大小
     *
     * @param bm
     * @param width
     * @param height
     * @return
     */
    private static Bitmap scaleBitmap(Bitmap bm, float width, float height) {
        if (bm == null) {
            return null;
        }
        int bmWidth = bm.getWidth();
        int bmHeight = bm.getHeight();
        float scaleWidth = width / bmWidth;
        float scaleHeight = height / bmHeight;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        if (scaleWidth == 1 && scaleHeight == 1) {
            return bm;
        } else {
            Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, bmWidth,
                    bmHeight, matrix, false);
            bm.recycle();//回收图片内存
            bm.setDensity(240);
            return resizeBitmap;
        }
    }


    public static Bitmap getViewBitmap(View view) {
//      Log.e("宽",view.getWidth()+"");
//      Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//      Canvas canvas = new Canvas(bitmap);
//      view.draw(canvas);

//      view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        return bitmap;
    }
多次运行截屏显示同一张图片

问题原因是因为View的缓存机制造成的,解决方法清除缓存即可

view.destroyDrawingCache();执行这个方法,释放view的缓存资源
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值