尝试这种方法,希望能帮助您改进代码...
- 您需要从此引用中获取“Aquery(AndroidQuery)”jar包:
https://code.google.com/p/android-query/downloads/detail?name=android-query-full.0.24.3.jar
2.现在将此jar包添加到您的项目lib文件夹中,并将其添加到构建路径或作为库。
3.现在是使用“Aquery(AndroidQuery)”从服务器下载图像的代码时间(这是我的演示代码,您可以根据自己的要求进行修改)。
"activity_main.xml"
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgFromServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<ProgressBar
android:id="@+id/pbrImageLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>
"MyActivity.java"
public class MyActivity extends Activity{
"我的活动.java"
public class 我的活动 extends 活动{
private ImageView imgFromServer;
private ProgressBar pbrImageLoader;
private AQuery aQuery;
private int currentIndex;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFromServer = (ImageView) findViewById(R.id.imgFromServer);
pbrImageLoader = (ProgressBar) findViewById(R.id.pbrImageLoader);
aQuery = new AQuery(this);
currentIndex=0;
final ArrayList<String> imageUrlListFromServer = new ArrayList<String>();
imageUrlListFromServer.add("http://www.mayoff.com/5-01cablecarDCP01934.jpg");
imageUrlListFromServer.add("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
imageUrlListFromServer.add("http://www.hdwallshub.com/files/submissions/cookie_monster_hd_wallpaper_1405239014.jpg");
imageUrlListFromServer.add("http://images4.fanpop.com/image/photos/17200000/Tangled-offical-wallpapers-tangled-17286338-1680-1050.jpg");
imageUrlListFromServer.add("http://wakpaper.com/large/Moons_wallpapers_4.jpg");
final Timer timer = new Timer();
downloadImagesFromServer(imageUrlListFromServer, 0, new ImageDownloadedListener() {
@Override
public void onDownloadFinish() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
imgFromServer.setImageBitmap(aQuery.getCachedImage(imageUrlListFromServer.get(currentIndex)));
if(currentIndex == imageUrlListFromServer.size()-1){
currentIndex=0;
}else{
currentIndex++;
}
}
});
}
},0,2000);
}
});
}
private void downloadImagesFromServer(final ArrayList<String> imageUrlList,final int index,final ImageDownloadedListener listener){
aQuery.progress(pbrImageLoader).ajax(imageUrlList.get(index), Bitmap.class, 0, new AjaxCallback<Bitmap>() {
@Override
public void callback(String url, Bitmap object, AjaxStatus status) {
super.callback(url, object, status);
if ((imageUrlList.size() - 1) == index) {
listener.onDownloadFinish();
} else {
downloadImagesFromServer(imageUrlList, index + 1, listener);
}
}
});
}
interface ImageDownloadedListener{
public void onDownloadFinish();
}
}
Note : "Aquery(AndroidQuery)" also cache images on local so it not get same images from server if it already downloaded.
downloader.download(item.imageurl(), bgImage);
,item.imageurl()
是一个字符串,我该如何逐个传递索引值? - Abid Khan