我使用ViewPager
全屏显示我的图像。基本上你点击图片缩略图,一个新的活动开始,它全屏显示图片。从系统的文件路径中创建位图显示图像。由于位图处理是资源密集型的,我将其移动到AsyncTask
线程中。然而,尽管这样做了,我还是没有看到性能的提高。
我的活动转换仍然延迟,我没有看到任何进展对话框。
public class ZoomImageAdapter extends PagerAdapter {
private Context mContext;
private List<String> imagePathData = new ArrayList<String>();
private LayoutInflater layoutInflater;
private View itemView;
private TouchImageView imageView;
private ViewGroup container;
public ZoomImageAdapter(Context context, List<String> data){
this.mContext = context;
this.imagePathData = data;
}
@Override
public int getCount() {
return this.imagePathData.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
this.container = container;
BitmapHandling bitmapHandling = new BitmapHandling();
bitmapHandling.execute(imagePathData.get(position));
View result = null;
try {
result = bitmapHandling.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return result;
}
private class BitmapHandling extends AsyncTask<String, View, View>{
ProgressDialog progressDialog;
String path;
Bitmap bitmap;
@Override
protected void onPreExecute(){
progressDialog = ProgressDialog.show(mContext,"","");
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = layoutInflater.inflate(R.layout.item_zoomimage, container, false);
}
@Override
protected View doInBackground(String... params) {
path = params[0];
bitmap = Utility.compressBitmap(BitmapFactory.decodeFile(path), 720);
return itemView;
}
@Override
protected void onPostExecute(View result){
progressDialog.dismiss();
imageView = (TouchImageView) result.findViewById(R.id.iv_zoomImage);
imageView.setImageBitmap(bitmap);
((ViewPager) container).addView(result, 0);
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
你的代码甚至不会编译,只是看看doInBackground()
方法:)
无论如何-它不工作,因为你调用AsyncTask.get()
这是阻塞。它等待后台任务完成并返回其结果。
你必须立即从你的instantiateItem()
方法返回一些东西(可能是自定义视图),并从那里开始BitmapHandling
任务。