I have a HorizontalScrollView that contains a LinearLayout to store my entire view. I am adding about 20 RelativeLayout which contains ImageView and TextView for LinearLayout. I would like to download only images if the ImageView is on the screen (when scrolling to ImageView).
I tried to use this post to use getHitRect() in thumbnail, however, Rect (bounds) for thumbnail is always 0, 0-0, 0, as a result my method returns false. What am I doing wrong?
thumbnailLayout is my LinearLayout inside a HorizontalScrollView
thumbnailScroll - my HorizontalScrollView
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); }
Edit I use TreeObserver and found that my method of checking the correct display of files on the screen is incorrect. It still captures all images and constantly loops (because I'm using 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; } });
heero source share