【android学习笔记】之入坑Bitmap.createBitmap:y+height must <=source height

【概】目的使用bitmap截取屏幕上部分内容,顺利入坑:y+height must <=source height

【注】开始还没明白意思,log出y始终<bitmap.getHeight();

          百度了以下才明了:

//源码
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
        throw new RuntimeException("Stub!");
    }
//y就是中间的参数y,height为参数height,必须满足:y+height<=bitmap.getHeight();bitmap为传入的bitmap

【最终目的】从屏幕整张图片中截取出指定区域

 private void getBitmap() throws Exception {
        View screenView = getWindow().getDecorView();
        screenView.setDrawingCacheEnabled(true);
        screenView.buildDrawingCache();

        //获取屏幕整张图片
        Bitmap bitmap = screenView.getDrawingCache();
        if (bitmap != null&&srcName!=null) {
            optionBox.setVisibility(View.GONE);
            //获取需要截图部分的在屏幕上的坐标(view的左上角坐标)
            int[] viewLocationArray = new int[2];
            view.getLocationOnScreen(viewLocationArray);

            //需要截取的长和宽
            int outWidth = screenView.getWidth();
            int outHeight = view.getHeight();
            if(viewLocationArray[1]+outHeight>bitmap.getHeight()&&viewLocationArray[1]+outHeight!=bitmap.getHeight()){
                outHeight=outHeight-(viewLocationArray[1]+outHeight-bitmap.getHeight());
                //Log.e("messIT","超出高度!outheight="+outHeight+",bitmapHeight="+bitmap.getHeight());
            }
            if(viewLocationArray[0]+outWidth>bitmap.getWidth()){
                outWidth=bitmap.getWidth()-viewLocationArray[0];
            }

            //从屏幕整张图片中截取指定区域
           //Log.e("messIT","bitmapHeight="+bitmap.getHeight()+"bitmapWidth="+bitmap.getWidth()+"--"+viewLocationArray[0]+","+viewLocationArray[1]+";"+outWidth+","+outHeight);
            bitmap = Bitmap.createBitmap(bitmap, viewLocationArray[0], viewLocationArray[1], outWidth, outHeight);

           File mFile=uriToFile(Uri.parse(srcName),this);
            FileOutputStream outputStream = new FileOutputStream(mFile);     //构建输出流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 30, outputStream);  //compress到输出outputStream
            Toast.makeText(getApplicationContext(),"保存成功!",Toast.LENGTH_SHORT).show();

            Intent intent=new Intent();
            intent.putExtra("imgSrc",srcName);
            intent.putExtra("id",id);
            setResult(0001,intent);
            finish();

        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智商不够_熬夜来凑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值