正确的在ViewPager中使用Bitmap图像
在实现image gallery的详情查看时使用swipe view模式是很好的选择,你可以使用ViewPager和对应的PagerAdapter来实现这个模式。不过对于adapter你还有更好的选择:FragmentStatePagerAdapter,当屏幕关闭的时候这个子类可以自动的销毁和保存ViewPager的Fragments状态,以节省内存。注意:如果你只需要使用很少的图片,并确定其不会超出程序内存的限制的话,使用PagerAdapter或者FragmentPagerAdapter对于你来说是最适合的。下面是一个ViewPager的实现,他里面包含了数个ImageView。在main activity中使用ViewPager和adapter:public class ImageDetailActivity extends FragmentActivity {public static final String EXTRA_IMAGE = "extra_image";private ImagePagerAdapter mAdapter;private ViewPager mPager;// A static dataset to back the ViewPager adapterpublic final static Integer[] imageResIds = new Integer[] {R.drawable.sample_image_1, R.drawable.sample_image_2, R.drawable.sample_image_3,R.drawable.sample_image_4, R.drawable.sample_image_5, R.drawable.sample_image_6,R.drawable.sample_image_7, R.drawable.sample_image_8, R.drawable.sample_image_9};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.image_detail_pager); // Contains just a ViewPagermAdapter = new ImagePagerAdapter(getSupportFragmentManager(), imageResIds.length);mPager = (ViewPager) findViewById(R.id.pager);mPager.setAdapter(mAdapter);}public static class ImagePagerAdapter extends FragmentStatePagerAdapter {private final int mSize;public ImagePagerAdapter(FragmentManager fm, int size) {super(fm);mSize = size;}@Overridepublic int getCount() {return mSize;}@Overridepublic Fragment getItem(int position) {return ImageDetailFragment.newInstance(position);}}}下面是使用Fragment和ImageView的一个实现,看上去是十分合理的,你能发现它的缺点吗?并且如何进行改进?public class ImageDetailFragment extends Fragment {private static final String IMAGE_DATA_EXTRA = "resId";private int mImageNum;private ImageView mImageView;static ImageDetailFragment newInstance(int imageNum) {final ImageDetailFragment f = new ImageDetailFragment();final Bundle args = new Bundle();args.putInt(IMAGE_DATA_EXTRA, imageNum);f.setArguments(args);return f;}// Empty constructor, required as per Fragment docspublic ImageDetailFragment() {}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mImageNum = getArguments() != null ? getArguments().getInt(IMAGE_DATA_EXTRA) : -1;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// image_detail_fragment.xml contains just an ImageViewfinal View v = inflater.inflate(R.layout.image_detail_fragment, container, false);mImageView = (ImageView) v.findViewById(R.id.imageView);return v;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);final int resId = ImageDetailActivity.imageResIds[mImageNum];mImageView.setImageResource(resId); // Load image into ImageView}}希望你已经注意到了这个问题:所有的图片都是在UI线程中处理的,这样将导致程序挂掉,强制退出。这时需要使用AsyncTask(在“Processing Bitmaps Off the UI Thread”这一篇文章中介绍过的)来处理,直接将图片的加载和处理放在后台线程中去做。public class ImageDetailActivity extends FragmentActivity {...public void loadBitmap(int resId, ImageView imageView) {mImageView.setImageResource(R.drawable.image_placeholder);BitmapWorkerTask task = new BitmapWorkerTask(mImageView);task.execute(resId);}... // include BitmapWorkerTask class}public class ImageDetailFragment extends Fragment {...@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);if (ImageDetailActivity.class.isInstance(getActivity())) {final int resId = ImageDetailActivity.imageResIds[mImageNum];// Call out to ImageDetailActivity to load the bitmap in a background thread((ImageDetailActivity) getActivity()).loadBitmap(resId, mImageView);}}}任何多余的处理(比如调整图像大小,从网络获取图片)都移到BitmapWorkerTask中,这样就不会影响到UI主线程。如果后台线程除了在硬盘中直接读取图片之外还有其他处理,在内存或硬盘中加入一个缓存是十分有用的(在“Caching Bitmaps”这篇文章中有介绍)。下面是一个内存缓存的实现:public class ImageDetailActivity extends FragmentActivity {...private LruCache<String, Bitmap> mMemoryCache;@Overridepublic void onCreate(Bundle savedInstanceState) {...// initialize LruCache as per Use a Memory Cache section}pu补充:移动开发 , Android ,
上一个:Android 驱动开发系列一
下一个:Android图像处理:怀旧效果