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

本文介绍了一种从屏幕整张图片中截取指定区域的方法,通过获取屏幕View的坐标和尺寸,确保裁剪后的图片不会超出原始Bitmap的边界,避免了因参数设置不当导致的错误。

【概】目的使用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();

        }
    }

 

FATAL EXCEPTION: main Process: com.mercusys.halo, PID: 31585 java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:1209) at android.graphics.Bitmap.createBitmap(Bitmap.java:1175) at android.graphics.Bitmap.createBitmap(Bitmap.java:1123) at android.graphics.Bitmap.createBitmap(Bitmap.java:1082) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate.initLine(EarthAnimationViewDelegate.kt:102) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate.access$initLine(EarthAnimationViewDelegate.kt:23) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate$initEarth$$inlined$addListener$default$1.onAnimationEnd(Animator.kt:141) at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:719) at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda1.call(Unknown Source:4) at android.animation.Animator.callOnList(Animator.java:677) at android.animation.Animator.notifyListeners(Animator.java:616) at android.animation.Animator.notifyEndListeners(Animator.java:641) at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1306) at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1585) at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:328) at android.animation.AnimationHandler.-$$Nest$mdoAnimationFrame(Unknown Source:0) at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:86) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1542) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1553) at android.view.Choreographer.doCallbacks(Choreographer.java:1109) at android.view.Choreographer.doFrame(Choreographer.java:984) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1527) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:257) at android.os.Looper.loop(Looper.java:368) at android.app.ActivityThread.main(ActivityThread.java:8839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
05-11
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智商不够_熬夜来凑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值