【问题标题】:How could I deal with the imageView when I am loading an Image加载图像时如何处理 imageView
【发布时间】:2016-06-16 07:17:24
【问题描述】:

如果某些ImageViews与GridViewListView相关,并且我们想在屏幕上显示一些图像,通常会发生图像的宽度和高度值为零(0),所以它发生了当我们传递这个参数来计算压缩位图时出现异常,我回顾了一些框架,但这对我来说仍然是一个问题。我想获得一些内核解决方案来解决这个问题。当ImageView 的宽度和高度为 0 时我该怎么办。非常感谢任何帮助。

public void loadImage(final ImageView imageView, final String imageUrl){
    mExecutor.execute(new PriorityRunnable(mThreadsCount) {

        @Override
        public void doSomething() {
            while(!mExecutor.isPause()){
                if(imageView != null && imageView.getTag().equals(imageUrl)){
                    mImageCache.loadImage(imageView, imageUrl);
                }
            }
        }
    });
}

public void loadImage(ImageView imageView, String imageUrl){

    //readFromMemoryCache
    Bitmap bitmap = getBitmap(imageUrl);
    if(bitmap != null){
        mImageView = imageView;
        mBitmap = bitmap;
        mHandler.sendEmptyMessage(DISPLAY_IMAGE);
        return;
    }

    int width = imageView.getWidth();
    int height = imageView.getHeight();
    //because width = 0, so it doesn't do anything, only return
    if(width == 0 || height == 0){
        return;
    }

    //load from http or disk
    bitmap = getBitmap(imageUrl, width, height);
    if(bitmap != null){
        mImageView = imageView;
        mBitmap = bitmap;
        mHandler.sendEmptyMessage(DISPLAY_IMAGE);
    }
}

【问题讨论】:

  • 您可以延迟图像的加载,直到布局被测量。请参阅this so entry 了解如何操作。
  • 你从哪里得到图像的大小?在 onCreate() 中?
  • 我创建一个新线程,并计算图像的大小@Alex Hong

标签: android imageview


【解决方案1】:

您需要使用全局布局侦听器。 比如:

    yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (yourView.getHeight() > 0 && yourView.getWidth() > 0) {
                CompatUtil.removeOnGlobalLayoutListener(yourView, this);
                // do your stuff
            }
        }
    });

【讨论】:

  • 但是这个操作不能保证我的视图的宽度和高度不等于“0”,还有什么我应该做的吗? @金斯顿
  • 在布局视图时,onGlobalLayout 将被多次调用。最后,大小将不再为零,您将调用 removeOnGlobalLayoutListener 不再被调用,然后您将显示图像
  • 谢谢@kingston,我仍然对这个问题感到困惑,起初我已经显示了我的 ListView ,它需要一些时间来加载这些图像,首先宽度和高度都是0,我必须过滤这种情况,否则会发生异常。所以由于这个原因,我的屏幕无法显示图像。我发现当我滚动这个列表视图时,会显示一些图像。我已经添加了我的代码。
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多