在我的應用程序中,我有一個listView
它使用LazyLoading來顯示圖像。自定義ImageView與progressBar作爲默認圖像
但是,直到實際的圖像下載並顯示,我想ImageView
顯示加載動畫,而不是一些默認的圖像。
我想創建一個自定義imageView
它有一個默認圖像ListDrawable
看起來像一個動畫。但是有沒有更簡單/常見的方法來實現這一目標?
謝謝。
在我的應用程序中,我有一個listView
它使用LazyLoading來顯示圖像。自定義ImageView與progressBar作爲默認圖像
但是,直到實際的圖像下載並顯示,我想ImageView
顯示加載動畫,而不是一些默認的圖像。
我想創建一個自定義imageView
它有一個默認圖像ListDrawable
看起來像一個動畫。但是有沒有更簡單/常見的方法來實現這一目標?
謝謝。
在每個列表項中都有一個進度條。每當你下載圖片時,隱藏這個進度條[set visibility GONE]。你的xml應該看起來像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgToBeSet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/img_cont_desc_common" />
<ProgressBar
android:id="@+id/imgProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imgToBeSet"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:text="@string/msg_download_failed" />
</RelativeLayout>
這是一個相當普遍的問題,可以通過使用其中一個可用的庫來解決。當我需要類似的東西時,圖書館感覺有點沉重。
一個非常簡單的解決方案是使用默認加載微調器創建ImageView。現在你有一個ListView充滿了spinners。接下來創建一個新的AsyncTask類,它接受一個URL並返回一個位圖。異步任務完成後,使用myImageView.setImageBitmap(...);
將結果分配給ImageView。例如。 (沒有合理的錯誤檢查等)
public class MyLazyBitmapLoader extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
public MylazyBitmapLoader(final ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap result = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
result = BitmapFactory.decodeStream(in);
}
catch (Exception e) {
...
}
return result;
}
protected void onPostExecute(Bitmap result) {
//handle null
imageView.setImageBitmap(result);
}
}
這可以很容易地擴展到包括其他功能,如高速緩存,其他資源加載等
如果你有再一次嘗試這個做一個透明的活動,然後使用在它的加載gif圖像,搜索谷歌,你會發現一種方式來加載Android的gif圖像:) –
不要重新發明輪子,只需使用[通用圖像加載器](https://github.com/nostra13/ Android的通用 - 圖像下載器) – Androiderson