• 用开源项目PhotoView实现图片的双指缩放和双击放大缩小


    项目地址: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时很可能要用进度条来等待网络图片

    源码下载:http://download.csdn.net/detail/shark0017/7710153

  • 相关阅读:
    js如何实现上拉加载更多...
    基于epoll的TP传输层实现
    ActiveMQ简介与安装
    序列化和反序列化实现
    NoSQL之Redis数据库初探
    分布式缓存之memcached--命令测试
    Java正则表达式测试用例
    Java之正则表达式
    Java反射机制练习(增强可扩展性)
    Java反射机制(获取Class对象的三种方式+获取Class中的构造函数进行对象的初始化+获取反射类的字段+获取反射类的一般方法)
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3888664.html
Copyright © 2020-2023  润新知