【问题标题】:Android only download images that are visible in horizontalScrollViewAndroid 仅下载在 Horizo​​ntalScrollView 中可见的图像
【发布时间】:2013-04-12 02:21:01
【问题描述】:

我有一个 Horizo​​ntalScrollView,其中包含一个 LinearLayout 来保存我的所有视图。我在 LinearLayout 中添加了大约 20 个包含 ImageView 和 TextView 的 RelativeLayout。如果 ImageView 在屏幕上(当我滚动到 ImageView 时),我只想加载图像。

我尝试按照this post 在缩略图上使用getHitRect(),但是,缩略图的 Rect(边界)始终为 0、0-0、0,导致我的方法返回 false。我做错了什么?

thumbnailLayout 是我在 Horizo​​ntalScrollView 中的 LinearLayout
thumbnailScroll 是我的 Horizo​​ntalScrollView

runOnUiThread(new Runnable() {

    @Override
    public void run() {
           Log.e(TAG, "running");
           for (int i = 0; i < thumbnailLayout.getChildCount(); i++) {
                RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i);
                ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
                if (thumbnailIsOnScreen(thumbnail)) {
                    Log.e(TAG, items.get(i).getTitle() + " has downloaded");
                    app.setImage(items.get(i).getThumbnailSmall(), thumbnail);
                }
           }


       }
   });

private boolean thumbnailIsOnScreen(ImageView thumbnail) {
        Rect bounds = new Rect();
        thumbnail.getHitRect(bounds);

        Rect scrollBounds = new Rect(thumbnailScroll.getScrollX(), thumbnailScroll.getScrollY(),
                thumbnailScroll.getScrollX() + thumbnailScroll.getWidth(), thumbnailScroll.getScrollY()
                        + thumbnailScroll.getHeight());
        return Rect.intersects(scrollBounds, bounds);
    }

编辑 我正在使用 TreeObserver 并发现我检查缩略图是否在屏幕上的方法是错误的。它仍然抓取所有图像,并不断循环(因为我使用的是 onPreDrawListener?)

thumbnailLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                Log.e(TAG, "running");

                for (int i = 0; i < thumbnailLayout.getChildCount(); i++) {
                    RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i);
                    ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
                    if (thumbnailIsOnScreen(thumbnail)) {
                        Log.e(TAG, items.get(i).getTitle() + " has downloaded");
                        app.setImage(items.get(i).getThumbnailSmall(), thumbnail);
                    }
                }
                return true;
            }
        });

【问题讨论】:

  • 你从哪里调用 Runnable?是否在滚动监听器中?
  • 我在将 ImageViews 添加到 LinearLayout 后运行它。我在想在将 imageViews 添加到 LinearLayout 之后,必须加载一些图像。然后我会使用滚动监听器
  • 您可以使用带适配器的图库吗?
  • 我不希望我的项目居中,这就是我不使用画廊的原因
  • 此外,自 API 16 起,Gallery 现已弃用,因此您不应使用它。

标签: android performance horizontalscrollview


【解决方案1】:

您需要一个 ListView,但 android 中没有原生的 Horizo​​ntallListView。你可以试试HorizontallListView by DevSmart。它的工作方式类似于原生列表视图,如果需要,可以使用适配器和自定义视图。

Yo lo estoy utilizando en mi proyecto por la misma razón que tu, para cargar solo las imagenes visibles,希望对您有所帮助。

【讨论】:

    【解决方案2】:

    您是否考虑过使用 ViewPager? http://developer.android.com/reference/android/support/v4/view/ViewPager.html

    如果这不适合您的设计,您需要覆盖 ScrollView -> 函数 onScrollChanged 并检查屏幕中的图像是否检查图像(左侧?)位置与滚动位置的比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多