【问题标题】:Android - Update Bitmap When the View Has Been MeasuredAndroid - 测量视图后更新位图
【发布时间】:2016-03-28 01:19:06
【问题描述】:

我正在尝试从图像视图中检索位图,然后对其进行缩放。但我收到以下错误:

"java.lang.IllegalArgumentException: width and height must be > 0"

我在网上搜索是因为在 imageView 完成测量并转换为位图之前,我尝试更新位图。任何人都可以帮我解决这个问题。我的代码如下:

profileImage.buildDrawingCache();
Bitmap bm = profileImage.getDrawingCache();

int widthPixel = bm.getWidth();
int heightPixel = bm.getHeight();
int basePixel;
float widthRatio;
float heightRatio;

if (widthPixel < heightPixel) {
  basePixel = widthPixel;
}
else {
   basePixel = heightPixel;
}

if (basePixel > 180) {
  widthRatio = 180/basePixel;
  heightRatio = 180/basePixel;
}
else {
   widthRatio = 1;
   heightRatio = 1;
}

Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();

编辑后的代码(现在出现“java.lang.IllegalArgumentException: value may not be null.”错误):

                                    profileImage.buildDrawingCache();
                                    bm = profileImage.getDrawingCache();



                                    profileImage.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
                                        public boolean onPreDraw() {
                                            profileImage.getViewTreeObserver().removeOnPreDrawListener(this);

                                            widthPixel = profileImage.getMeasuredWidth();
                                            heightPixel = profileImage.getMeasuredHeight();

                                            if (widthPixel < heightPixel) {

                                                basePixel = widthPixel;

                                            }

                                            else {

                                                basePixel = heightPixel;

                                            }


                                            if (basePixel > 180) {

                                                widthRatio = 180/basePixel;
                                                heightRatio = 180/basePixel;


                                            }

                                            else {

                                                widthRatio = 1;
                                                heightRatio = 1;

                                            }


                                            Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
                                            // bm.recycle();

                                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                            bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                            byteArray1 = stream.toByteArray();

                                            image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");

                                            return true;
                                        }
                                    });

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    使用OnPreDrawListenerOnPreDrawListener 允许您在 View 的度量操作之后但在布局阶段之前检索您的 View 维度。这意味着这是您可以获取视图尺寸的最早时间。

    view.getViewTreeObserver().adOnPreDrawListener(new OnPreDrawListener() {
        public boolean onPreDraw() {
            view.getViewTreeObserver().removeOnPreDrawListener(this);
    
    
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
    
            // TODO scale your bitmap here
    
            return true;
        }
    });
    

    【讨论】:

    • 你好 Reiner,我用“OnPreDrawListener”编辑了我的代码,但现在我得到了“java.lang.IllegalArgumentException: value may not be null”。错误。你可以检查我上面的代码吗?谢谢。
    • 每当您的应用程序因异常而崩溃时,请查找提到的行号。这会将您指向代码中发生错误的行,因此您可以调试问题。由于您提到抛出的错误是IllegalArgumentException 并且消息显示为value may not be null,我猜您正在尝试调整null 位图的大小。
    • @saner 您的方法存在一些问题。您正在尝试将视图的绘图缓存呈现为Bitmap。但是 View 还没有被绘制,所以 View 的绘图缓存是空的,使得view.getDrawingCache() 返回null。要么强制视图绘制(关于 SO 的大量示例),要么采取另一种方法。一种替代方法是调用view.getDrawable(),然后将Drawable 转换为位图,然后您可以调整其大小。
    • 您好 Reiner,在我上面编辑的代码中,将前两行放入我的 addPreDrawListener 部分后,它开始工作。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多