我使用自定义列表视图控件来显示图像和其他信息。当我滚动它以显示更多内容时,listview控件有点慢。
在测试控件之后,我发现函数SetControlsValue
可以做很多工作。我必须做一些工作来改进它。
方法1 是我的方式,是否正确?还有其他更好的方法吗?
方法1
private void SetControlsValue(ViewHolder holder,int position){
holder.mCheckBox.setChecked(mListCheckedStatus.get(position));
new BMPOperation().execute(position,holder);
new LongOperation().execute(position,holder);
}
private class BMPOperation extends AsyncTask<Object, Void, Bitmap> {
private ViewHolder myHolder;
@Override
protected Bitmap doInBackground(Object... params) {
int position=(Integer)params[0];
myHolder=(ViewHolder)params[1];
Bitmap mBitmap=mListVideo.get(position).GetBitmapOfVideo();
return mBitmap;
}
@Override
protected void onPostExecute(Bitmap mBitmap) {
try {
if (mBitmap != null) {
myHolder.imageViewthumb.setImageBitmap(mBitmap);
}
}catch (Exception e){
Utility.LogError(e.getMessage());
}
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
private class LongOperation extends AsyncTask<Object, Void, String> {
private ViewHolder myHolder;
@Override
protected String doInBackground(Object... params) {
int position=(Integer)params[0];
myHolder=(ViewHolder)params[1];
String s=mListVideo.get(position).fileName
+"\n"+mContext.getResources().getString(R.string.TitleResolution)+ mListVideo.get(position).GetResolution()
+"\n"+mContext.getResources().getString(R.string.TitleSize)+mListVideo.get(position).GetSize()
+"\n"+mContext.getResources().getString(R.string.TitleDuration)+mListVideo.get(position).GetDuration();
return s;
}
@Override
protected void onPostExecute(String result) {
myHolder.tvInfo.setText(result);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
功能SetControlsValue
private void SetControlsValue(ViewHolder holder,int position){
holder.mCheckBox.setChecked(mListCheckedStatus.get(position));
Bitmap mBitmap=mListVideo.get(position).GetBitmapOfVideo();
try {
if (mBitmap != null) {
holder.imageViewthumb.setImageBitmap(mBitmap);
}
}catch (Exception e){
Utility.LogError(e.getMessage());
}
String s=mListVideo.get(position).fileName
+"\n"+mContext.getResources().getString(R.string.TitleResolution)+ mListVideo.get(position).GetResolution()
+"\n"+mContext.getResources().getString(R.string.TitleSize)+mListVideo.get(position).GetSize()
+"\n"+mContext.getResources().getString(R.string.TitleDuration)+mListVideo.get(position).GetDuration();
holder.tvInfo.setText(s);
}
ListVideoAdapter.java
public class ListVideoAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<Video> mListVideo;
private List<Boolean> mListCheckedStatus;
public ListVideoAdapter (Context context){
mContext=context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mListVideo= VideoHelper.ListVideo(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListVideo.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.layout_adapter_video, null);
IniControls(holder,convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
SetControlsPosition(holder,position);
SetControlsValue(holder,position);
SetControlsEvent(holder);
return convertView;
}
private void IniControls(ViewHolder holder,View convertView){
holder.mCheckBox=(CheckBox) convertView.findViewById(R.id.chSelect);
holder.imageViewthumb=(ImageView)convertView.findViewById(R.id.imageViewVideo);
holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
holder.imageViewTrim=(ImageView)convertView.findViewById(R.id.imageViewTrim);
holder.imageViewShare=(ImageView)convertView.findViewById(R.id.imageViewShare);
holder.imageViewInfo=(ImageView)convertView.findViewById(R.id.imageViewInfo);
holder.imageViewDelete=(ImageView)convertView.findViewById(R.id.imageViewDelete);
}
private void SetControlsPosition(ViewHolder holder,int position){
holder.id=position;
holder.mCheckBox.setId(position);
holder.imageViewthumb.setId(position);
holder.tvInfo.setId(position);
holder.imageViewTrim.setId(position);
holder.imageViewShare.setId(position);
holder.imageViewInfo.setId(position);
holder.imageViewDelete.setId(position);
}
private void SetControlsValue(ViewHolder holder,int position){
holder.mCheckBox.setChecked(mListCheckedStatus.get(position));
Bitmap mBitmap=mListVideo.get(position).GetBitmapOfVideo();
try {
if (mBitmap != null) {
holder.imageViewthumb.setImageBitmap(mBitmap);
}
}catch (Exception e){
Utility.LogError(e.getMessage());
}
String s=mListVideo.get(position).fileName
+"\n"+mContext.getResources().getString(R.string.TitleResolution)+ mListVideo.get(position).GetResolution()
+"\n"+mContext.getResources().getString(R.string.TitleSize)+mListVideo.get(position).GetSize()
+"\n"+mContext.getResources().getString(R.string.TitleDuration)+mListVideo.get(position).GetDuration();
holder.tvInfo.setText(s);
}
private void SetControlsEvent(final ViewHolder holder) {
}
}
class ViewHolder {
int id;
CheckBox mCheckBox;
ImageView imageViewthumb;
TextView tvInfo;
ImageView imageViewTrim;
ImageView imageViewShare;
ImageView imageViewInfo;
ImageView imageViewDelete;
}
加
我已阅读UniversalImageLoader的示例代码。
我不认为UniversalImageLoader控件符合我的要求。
ImageLoader需要传递URL par
ImageLoader.getInstance().displayImage(IMAGE_URLS[position], imageView, options);
但在我的应用中,还有其他两个代码需要花费很多时间。
Bitmap mBitmap=mListVideo.get(position).GetBitmapOfVideo();
String s=mListVideo.get(position).fileName +"\n"+mContext.getResources().getString(R.string.TitleResolution)+ mListVideo.get(position).GetResolution() +"\n"+mContext.getResources().getString(R.string.TitleSize)+mListVideo.get(position).GetSize() +"\n"+mContext.getResources().getString(R.string.TitleDuration)+mListVideo.get(position).GetDuration();
所以我认为我需要另一种更好的方法来改进代码。
BTW ,在我的函数Bitmap mBitmap=mListVideo.get(position).GetBitmapOfVideo()
中不仅获取了视频的缩略图,而且我还需要将缩略图中的Play图标合并,所以我不认为我可以使用ImageLoader.getInstance().displayImage("file:///mnt/sdcard/myvideo.mp4", imageView)
。
答案 0 :(得分:0)
在Bitmaps
上显示UiThread
是一项懒惰的操作。因此,您应该UniversalImageLoader
在SetControlsValue
函数中显示视频的位图/缩略图。
希望这有帮助。
答案 1 :(得分:0)
以下是提高绩效的建议: