Android只下载在horizontalScrollView中可见的图像
我有一个水平滚动视图,其中包含一个线性布局来保存我的所有视图。我在LinearLayout中添加了大约20个RelativeLayout,其中包含ImageView和TextView。我只想在屏幕上显示ImageView时加载图像(当我滚动到ImageView时) 我尝试在缩略图上使用Android只下载在horizontalScrollView中可见的图像,android,performance,horizontalscrollview,Android,Performance,Horizontalscrollview,我有一个水平滚动视图,其中包含一个线性布局来保存我的所有视图。我在LinearLayout中添加了大约20个RelativeLayout,其中包含ImageView和TextView。我只想在屏幕上显示ImageView时加载图像(当我滚动到ImageView时) 我尝试在缩略图上使用getHitRect(),但是,缩略图的Rect(界限)始终为0,0-0,0,导致我的方法返回false。我做错了什么 thumbnailLayout是我在HorizontalScrollView中的线性布局 th
getHitRect()
,但是,缩略图的Rect(界限)始终为0,0-0,0,导致我的方法返回false。我做错了什么
thumbnailLayout
是我在HorizontalScrollView中的线性布局thumbnailScroll
是我的水平滚动视图
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);
}
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Log.e(标签“运行”);
对于(int i=0;i
编辑
我正在使用TreeObserver进行tred,发现我检查屏幕上是否有拇指尾的方法是错误的。它仍然捕获所有图像,并不断循环(因为我使用的是onPreDrawListener?)
thumbnailLayout.getViewTreeObserver().addOnPreDrawListener(新的OnPreDrawListener()){
@凌驾
公共布尔onPreDraw(){
Log.e(标签“运行”);
对于(int i=0;i
您需要一个ListView,但android中没有原生的HorizontallListView。你可以试试。它的工作原理类似于本机列表视图,如果需要,可以使用适配器和自定义视图
我希望这能帮助你。你有没有想过使用ViewPager?
如果这不符合您的设计,您需要覆盖ScrollChanged上的ScrollView->函数,然后检查屏幕中的图像是否与滚动位置相比,检查图像(左?)位置。您从哪里调用该可运行位置?它在滚动侦听器中吗?我在将ImageView添加到LinearLayout后运行它。我在想,在将ImageView添加到LinearLayout之后,必须加载一些图像。然后我会使用滚动列表器。你可以使用带适配器的多媒体资料吗?我不想让我的项目居中,这就是为什么我不使用多媒体资料的原因。而且,自从API 16以来,多媒体资料已被弃用,因此你不应该使用它。
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;
}
});