项目地址:https://github.com/chrisbanes/PhotoView
用开源项目有个好处,一是实现简单,二是bug少。那么我们就来说下这个项目能够实现的效果:
1.单个图片的双指缩放,双击放大缩小
PhotoViewAttacher mAttacher; ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Any implementation of ImageView can be used! mImageView = (ImageView) findViewById(R.id.iv_photo); // Set the Drawable displayed Drawable bitmap = getResources().getDrawable(R.drawable.ic_launcher); mImageView.setImageDrawable(bitmap); // Attach a PhotoViewAttacher, which takes care of all of the zooming // functionality. mAttacher = new PhotoViewAttacher(mImageView);
2.图片的旋转效果,可以精准指定旋转的度数
private PhotoView photo; private final Handler handler = new Handler(); private boolean rotating = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); photo = new PhotoView(this); photo.setImageResource(R.drawable.wallpaper); setContentView(photo); photo.setRotationBy(10); }
3.放入viewpager来做照片浏览,浏览中可以放大和缩小(这里的viewpager可以设置是否允许左右滑动,如果不用这功能,就直接用普通的viewpager)
private static final String ISLOCKED_ARG = "isLocked"; private ViewPager mViewPager; private MenuItem menuLockItem; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); mViewPager = (HackyViewPager) findViewById(R.id.view_pager); setContentView(mViewPager); mViewPager.setAdapter(new SamplePagerAdapter()); if (savedInstanceState != null) { boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false); ((HackyViewPager) mViewPager).setLocked(isLocked); } } static class SamplePagerAdapter extends PagerAdapter { private static final int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper }; @Override public int getCount() { return sDrawables.length; } @Override public View instantiateItem(ViewGroup container, int position) { //定义可以缩放的图片对象 PhotoView photoView = new PhotoView(container.getContext()); photoView.setImageResource(sDrawables[position]); // Now just add PhotoView to ViewPager and return it container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }
这是官方文档的例子
ImageView mImageView; PhotoViewAttacher mAttacher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Any implementation of ImageView can be used! mImageView = (ImageView) findViewById(R.id.iv_photo); // Set the Drawable displayed Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper); mImageView.setImageDrawable(bitmap); // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality. mAttacher = new PhotoViewAttacher(mImageView); } // If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call attacher.update();
下面给出的源码是我修改过的,我把原来的放大两次变成了放大一次,然后再双击的话就缩小到原图。至于单击直接退出的效果我给出一个思路,点击一个缩略图后开启一个activity,用渐变的效果显示。在这个弹出的activity放一个可以缩放的图片,然后给这个图片加个监听器(源码中我已经加了),点击后这个activity渐变消失。很简单吧~ 不过这里面需要注意的是缩略图的设置还有弹出activity时很可能要用进度条来等待网络图片