IllegalArgumentException: width and height must be > 0 while loading Bitmap from View

后端 未结 5 1612
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 22:17

I\'m trying to draw on an ImageViewTouch, a library which enables pinch zooming. I\'m able to draw over the image using Canvas, but when I zoom the image, the drawing disapp

相关标签:
5条回答
  • 2020-11-27 22:33
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loadBitmapFromView(mImage);
            }
        }, 1000);
    
    0 讨论(0)
  • 2020-11-27 22:37

    In my case, I forgot to set orientation to vertical for parent LinearLayout and the default value of horizontal has been set, so I set it and my problem gone...

    android:orientation="vertical"
    
    0 讨论(0)
  • 2020-11-27 22:48

    If everything else is correct you are executing your code too early.

    The view's layout has not yet been measured by Android. Try executing in OnResume just to see if this is your problem.

    Cheers!

    0 讨论(0)
  • 2020-11-27 22:49

    I finally figured out a solution for my problem.

    I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.

    Here's the code sample:

    mImage.post(new Runnable() {
        @Override
        public void run() {
            mImage.setImageBitmap(loadBitmapFromView(mImage));
        }
    });
    

    Hope it also helps someone else :)

    0 讨论(0)
  • 2020-11-27 22:50

    java.lang.IllegalArgumentException: width and height must be > 0 is because in your Bitmap.createBitmap() call v.getLayoutParams().width or v.getLayoutParams().height is 0. Probably the LayoutParams are wrongly set in mImage.

    Update: setLayoutParams() for the mImage before using createBitmap(). Something like this:

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
    mImage.setLayoutParams(layoutParams);
    
    0 讨论(0)
提交回复
热议问题