多点触控,双指缩放大集合

3,874 阅读2分钟

观看代码指南

        建议从简单的MultiTouchGestureDetector入手,看下原理。然后,观看PhotoView,看下结构怎么优雅点。再跑一下功能比较完善的demo PinchImageView ,修修改改可自己实现一个简单的。最后要集成比较完善的功能的话,直接用Stfalcon ImageViewer。

1.PhotoView(star 17.2k)

很多库都是基于他改的,代码逻辑清晰

PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView.

  • Out of the box zooming, using multi-touch and double-tap.
  • Scrolling, with smooth scrolling fling.
  • Works perfectly when used in a scrolling parent (such as ViewPager).
  • Allows the application to be notified when the displayed Matrix has changed. Useful for when you need to update your UI based on the current zoom/scroll position.
  • Allows the application to be notified when the user taps on the Photo.

2.Stfalcon ImageViewer(star 1.4k )

代码风格是kt,集成度比较高

A simple and customizable full-screen image viewer with shared image transition support, "pinch to zoom" and "swipe to dismiss" gestures. Compatible with all of the most popular image processing libraries such as Picasso, Glide etc. Based on PhotoView by chrisbanes.

alt tag alt tag

3.PinchImageView(star 1.8k )

只有一个类,功能完善,代码便于修改集成。

这是一个手势体验极棒但使用简单的ImageView控件,实现了手势放大缩小,平移等功能。PinchImageView继承于ImageView,可以在所有ImageView可以使用的情况下使用。全部的程序仅一个类文件,没有依赖任何特殊的外部库,非常易于集成

demo

  1. 手势控件与ViewPlayer结合,当图片不在当前页时加载一张缩略图,切换到当前页后更新成大图。
  2. 点击缩略图切换到大图浏览使用放大动画过渡;当返回时从大图到缩略图使用动画过渡。无论大图到状态和小图的位置都保证过渡平滑。
  3. 浏览超大尺寸图片时使用分片加载策略保证不OOM。

4.GestureViews (2.1k)

代码有点多,核心代码GestureController.java,加杂了各种动画,得花功夫看。

ImageView and FrameLayout with gestures control and position animation. Main goal of this library is to make images viewing process as smooth as possible and to make it easier for developers to integrate it into their apps.

Demo video

5.MultiTouchGestureDetector (star 214)

代码简洁,与特定的view解耦开了,便于修改和集成。

MultiTouchGestureDetector 通过提供的MotionEvent 统一处理缩放、旋转、移动手势变换。 当任何一个可察觉的手势发生变化,OnMultiTouchGestureListener 回调将会被触发, 此外SimpleOnMultiTouchGestureListener 是一个辅助类, 如果你不需要关心所有回调事件的话,可以优先继承它。 这个类一定要注入View的触摸事件。