createBitmap — java.lang.IllegalArgumentException: x must be < bitmap.width()

前端 未结 4 1822
天涯浪人
天涯浪人 2020-12-09 11:50

I am getting error while taking screenshot and create bitmap with cropping picture

below is my code

    View v1 = mKittyBGLayer.getRootView();
    v1         


        
4条回答
  •  抹茶落季
    2020-12-09 12:40

    No, not x must be < bitmap.width(). It says x + width must be <= bitmap.width().

    You are creating a Bitmap like so:

    Bitmap.createBitmap(source, 60, 0, 480, 260); // 320 - 60 = 260
    

    Basically, you are drawing from x = 60, y = 0 to x = 480 + 60, y = 260 on a Bitmap which is only 480x320. Obviously, this is not possible, since your x coordinate is off the Bitmap.

    It's hard to tell you how to fix this without knowing your exact use case. Basically, your source image has to fit within { x1: x, x2: x + width, y1: y, y2: y + height }.

    If you only want to draw from the 60th pixel onward, then you need to do this:

    Bitmap.createBitmap(source, vListView.getWidth(), 0, width - vListView.getWidth(), height - hListView.getHeight());
    

提交回复
热议问题