Android 开发笔记 ListView异步加载图片
當ListView需要在線獲取數(shù)據(jù),并且列表中需要顯示圖片時,友好的處理方式是使用異步加載圖片的方式。
這是因為LIstView中顯示的內(nèi)容是分為兩部加載的,第一次加載文本信息(包含圖片的uri地址),第二次使用圖片的uri下載圖片到本地顯示。
如果一次性獲取文本信息與圖片,將要較長時間加載,用戶等待太久。
?以下為代碼實現(xiàn):
xml文件,list.xml
?<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"?>
<ListView
? ? ? ? android:id="@+id/list"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@+id/img_your_circle"
? ? ? ? android:layout_marginBottom="50dp"
? ? ? ? android:layout_marginLeft="13dp"
? ? ? ? android:layout_marginRight="13dp"
? ? ? ? android:divider="@null"
? ? ? ? android:dividerHeight="5dp"
? ? ? ? android:fadingEdge="none"
? ? ? ? android:listSelector="@android:color/transparent"
? ? ? ? android:scrollbars="none" >
? ? </ListView>
</RelativeLayout>
?list_item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:background="@drawable/layout_background"
? ? android:padding="10dp" >
? ? <RelativeLayout
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignBottom="@+id/txt_icon"
? ? ? ? android:layout_alignLeft="@+id/txt_icon"
? ? ? ? android:layout_alignRight="@+id/txt_icon"
? ? ? ? android:layout_alignTop="@+id/txt_icon"
? ? ? ? android:layout_centerVertical="true" >
? ? ? ? <ProgressBar
? ? ? ? ? ? android:id="@+id/progressBar"
? ? ? ? ? ? style="?android:attr/progressBarStyleSmall"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_centerInParent="true" />
? ? </RelativeLayout>
? ? <ImageView
? ? ? ? android:id="@+id/txt_icon"
? ? ? ? android:layout_width="90dp"
? ? ? ? android:layout_height="90dp"
? ? ? ? android:scaleType="fitXY"
? ? ? ? android:visibility="invisible" />
? ? <TextView
? ? ? ? android:id="@+id/txt_title"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentTop="true"
? ? ? ? android:layout_alignTop="@+id/txt_icon"
? ? ? ? android:layout_marginLeft="5dp"
? ? ? ? android:layout_marginRight="5dp"
? ? ? ? android:layout_toLeftOf="@+id/imageView1"
? ? ? ? android:layout_toRightOf="@+id/txt_icon"
? ? ? ? android:ellipsize="end"
? ? ? ? android:maxLines="2"
? ? ? ? android:minLines="2"
? ? ? ? android:textColor="#000000" />
? ? <TextView
? ? ? ? android:id="@+id/txt_content"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignBottom="@+id/txt_icon"
? ? ? ? android:layout_alignLeft="@+id/txt_title"
? ? ? ? android:layout_alignRight="@+id/txt_title"
? ? ? ? android:ellipsize="end"
? ? ? ? android:maxLines="2"
? ? ? ? android:minLines="2" />
? ? <ImageView
? ? ? ? android:id="@+id/imageView1"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentRight="true"
? ? ? ? android:layout_centerVertical="true"
? ? ? ? android:src="@drawable/list_arrow" />
</RelativeLayout>?
?
java文件,ListActivity.java
?public class ListActivity extends Activity {
?
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.list);
super.onCreate(savedInstanceState);
?list = (ListView) findViewById(R.id.list);
?
getResource();?
}?
?
class AppsAdapter extends BaseAdapter {@Overridepublic int getCount() {return myList.size();}@Overridepublic Object getItem(int position) {return myList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ArticleResponse app = myList.get(position);ImageView del = null;if (convertView == null) {LayoutInflater inflater = getLayoutInflater();convertView = inflater.inflate(R.layout.goldencircle_innercircle_list_item, parent,false);}TextView txt_title = (TextView) convertView.findViewById(R.id.txt_title);txt_title.setText(app.pagetitle);TextView txt_content = (TextView) convertView.findViewById(R.id.txt_content);String teaser = app.teaser;if (teaser != null && teaser.trim().length() > CONTENT_LENGHT)teaser = teaser.substring(0, CONTENT_LENGHT) + "...";txt_content.setText(teaser);ImageView icon = (ImageView) convertView.findViewById(R.id.txt_icon);icon.setTag(position);icon.setVisibility(View.INVISIBLE);if (app.bitmap != null) {icon.setVisibility(View.VISIBLE);icon.setImageBitmap(app.bitmap);} else if (!app.flag) {new ImageLoadTask(GoldenCircleInnerCircleListActivity.this,position, icon).execute();app.flag = true;}return convertView;}}public class ImageLoadTask extends AsyncTask<Void, Void, Void> {private int position;private ImageView image;public ImageLoadTask(Context context, int position, ImageView image) {this.position = position;this.image = image;}@Overrideprotected Void doInBackground(Void... params) {ArticleResponse app = myList.get(position);if (app.thumb != null && app.thumb.trim().length() > 0) {Drawable dr = AsyncImageLoader.loadCacheImageFromUrl(app.thumb);Bitmap bitmap = null;if (dr != null) {bitmap = new WeakReference<Bitmap>(((BitmapDrawable) dr).getBitmap()).get();}app.flag = dr != null;app.bitmap = bitmap;publishProgress();}return null;}public void onProgressUpdate(Void... voids) {if (isCancelled())return;int p = Integer.valueOf(image.getTag().toString());if (p != position)return;ArticleResponse app = myList.get(position);image.setImageBitmap(app.bitmap);image.setVisibility(app.bitmap != null ? View.VISIBLE: View.INVISIBLE);}}}?
?
list項中加入圓形進度條,在圖片還未加載完時,進度條一直在滾動,當圖片顯示時,將遮住它。?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/linvaluable/archive/2012/08/13/2637271.html
總結(jié)
以上是生活随笔為你收集整理的Android 开发笔记 ListView异步加载图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将csv文件导入到数据库中
- 下一篇: windows server 2008