我在每一行都有一個帶有ImageView的ListView。有什麼方法可以提高從資產加載圖像的性能?
我有一個customAdapter爲getView函數中的每一行加載圖像。
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_row, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.listImage = (ImageView) row
.findViewById(R.id.list_image);
viewHolder.title = (TextView) row.findViewById(R.id.title);
viewHolder.description = (TextView) row
.findViewById(R.id.description);
row.setTag(viewHolder);
}
ItemDescription des = getItemDescriptionFromFile(items.get(position)
+ "/description.txt");
ViewHolder holder = (ViewHolder) row.getTag();
holder.title.setText(des.title);
holder.description.setText(des.description+(position)%5);
holder.listImage.setImageBitmap(getBitmapFromAsset(items.get(position));
return row;
}
private Bitmap getBitmapFromAsset(String strName) {
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
但是我注意到列表的滾動不夠光滑。
我GOOGLE了我的問題,而這個鏈接似乎是解決方案: http://developer.android.com/training/improving-layouts/smooth-scrolling.html
但也存在一些問題,與此代碼:
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
return mFakeImageLoader.getImage();
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.progress.setVisibility(View.GONE);
v.icon.setVisibility(View.VISIBLE);
v.icon.setImageBitmap(result);
}
}
}.execute(holder);
我不能訪問位置內部類的AsyncTask因爲裏面位置不是最終的。
有什麼方法可以訪問位置?
什麼是使我的清單順利滾動的最佳方式是什麼?
在此先感謝。
你可以看看[這](http://www.youtube.com/watch?v=KHTi28zT7rI) –