支持任意手势动作的图片

效果预览

支持图片的放大缩小,旋转操作。

效果预览

原理实现

先认识了解处理触摸事件的处理。

  • 以双击的位置为中心进行放大缩小操作;
  • 根据两指之间的距离与初识距离距离比较,进行放大和缩小操作;
  • 根据两指之间的arctan值,计算角度进行旋转。

源码实现

缩放:
    @Override
        public void setScale(float scale, float focalX, float focalY,
                             boolean animate) {
            ImageView imageView = getImageView();
    
            if (null != imageView) {
                // Check to see if the scale is within bounds
                if (scale < mMinScale || scale > mMaxScale) {
                    LogManager
                            .getLogger()
                            .i(LOG_TAG,
                                    "Scale must be within the range of minScale and maxScale");
                    return;
                }
    
                if (animate) {
                    imageView.post(new AnimatedZoomRunnable(getScale(), scale,
                            focalX, focalY));
                } else {
                    mSuppMatrix.setScale(scale, scale, focalX, focalY);
                    checkAndDisplayMatrix();
                }
            }
        }
旋转:
 private boolean doRotate(MotionEvent ev) {
        //Calculate the angle between the two fingers
        float deltaX = ev.getX(0) - ev.getX(1);
        float deltaY = ev.getY(0) - ev.getY(1);
        double radians = Math.atan(deltaY / deltaX);
        //Convert to degrees
        int degrees = (int) (radians * 180 / Math.PI);
        /*
         * Must use getActionMasked() for switching to pick up pointer events.
         * These events have the pointer index encoded in them so the return
         * from getAction() won't match the exact action constant.
         */
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                mLastAngle = degrees;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                mLastAngle = degrees;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                upRotate();
                mLastAngle = degrees;
                break;
            case MotionEvent.ACTION_MOVE:
                int degreesValue = degrees - mLastAngle;
                if (degreesValue > 45) {
                    //Going CCW across the boundary
                    rotate(-5);
                } else if (degreesValue < -45) {
                    //Going CW across the boundary
                    rotate(5);
                } else {
                    //Normal rotation, rotate the difference
                    rotate(degreesValue);
                }
                //Save the current angle
                mLastAngle = degrees;
                break;
        }
        return true;
    }

只贴了部分关键部分代码。

完整代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,515评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 1、都说,女人如花。然而这花所拥有的智慧,在会欣赏它的人面前,它就是掌上珍宝,胜过雄兵百万;反之,亦不如糊在地上的...
    炳暢阅读 453评论 0 1
  • 引言: 在iOS中音频按照播放形式可以分为音效播放和音乐播放。音效主要指的是一些短音频,通常作为点缀音频,如游戏中...
    sxyxsp123阅读 10,382评论 2 13
  • 又到一年公考季,再过两天即将迎来2017年国家公务员考试,千万刚毕业的莘莘学子,千军万马过独木河的场景又将一一呈现...
    河对岸的窗阅读 465评论 0 1