Pau*_*aul 1 android android-listview android-asynctask
我之前发过另一篇关于这个主题的帖子,但我已经基于建议改变了我的代码,但存在同样的问题.如果我点击屏幕上的其他元素,我的图像会不断移动.
这是我的代码,我称之为:
new Thumbnailer(image_main,image_table).execute(image);
image_main是我的imageView,image_table是保存它的表.
private class Thumbnailer extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private TableLayout imageTable;
public Thumbnailer(ImageView imageView, TableLayout imageTable) {
this.imageView = imageView;
this.imageTable = imageTable;
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
imageView.setVisibility(View.VISIBLE);
imageTable.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected Bitmap doInBackground(String... params) {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(params[0], o);
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeFile(params[0], o2);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我遇到了类似的问题.我会通过AsyncTask(可能通过HTTP获取图像)将图像加载到ListView中,并且在加载正确的图像之前,它似乎加载错误的图像一瞬间.就好像Android在HTTP请求完成之前重用该行,并且AsyncTasks有点碰撞.
上面的评论证实了正在发生的事情.
我通过在AsyncTask启动之前将图像路径放在ImageView的标记中来修复它,然后在AsyncTask完成之后但在它调用setImageBitmap之前检查该标记.
添加以下行:
final String somethingUnique = "put the image path or some other identifier here";
image_main.setTag(somethingUnique);
Run Code Online (Sandbox Code Playgroud)
在此之前:
new Thumbnailer(image_main, image_table).execute(image);
Run Code Online (Sandbox Code Playgroud)
现在,在onPostExecute中,检查标记:
// if this does not match, then the view has probably been reused
if (((String)imageView.getTag()).equals(somethingUnique)) {
imageView.setImageBitmap(result);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4230 次 |
| 最近记录: |