注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

android 多点触摸 实现图片缩放 Image Zoom  

2012-01-15 19:46:42|  分类: Android的UI开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

将下面的代码单独放到一个JAVA类中,MulitPointTouchListener.java


import android.graphics.Matrix;   
import android.graphics.PointF;   
import android.util.FloatMath;   
import android.view.MotionEvent;   
import android.view.View;   
import android.view.View.OnTouchListener;   
import android.widget.ImageView;   
public class MulitPointTouchListener implements OnTouchListener {   
        Matrix matrix = new Matrix();   
        Matrix savedMatrix = new Matrix();   
        static final int NONE = 0;   
        static final int DRAG = 1;   
        static final int ZOOM = 2;   
        int mode = NONE;   
        PointF start = new PointF();   
        PointF mid = new PointF();   
        float oldDist = 1f;   
        @Override   
        public boolean onTouch(View v, MotionEvent event) {   
                ImageView view = (ImageView) v;   
                switch (event.getAction() & MotionEvent.ACTION_MASK) {   
                case MotionEvent.ACTION_DOWN:   
                        matrix.set(view.getImageMatrix());   
                        savedMatrix.set(matrix);   
                        start.set(event.getX(), event.getY());   
                        mode = DRAG;   
                         
                        break;   
                case MotionEvent.ACTION_POINTER_DOWN:   
                        oldDist = spacing(event);   
                        if (oldDist > 10f) {   
                                savedMatrix.set(matrix);   
                                midPoint(mid, event);   
                                mode = ZOOM;   
                        }   
                        break;   
                case MotionEvent.ACTION_UP:   
                case MotionEvent.ACTION_POINTER_UP:   
                        mode = NONE;   
                        break;   
                case MotionEvent.ACTION_MOVE:   
                        if (mode == DRAG) {   
                                matrix.set(savedMatrix);   
                                matrix.postTranslate(event.getX() - start.x, event.getY()   
                                                - start.y);   
                        } else if (mode == ZOOM) {   
                                float newDist = spacing(event);   
                                if (newDist > 10f) {   
                                        matrix.set(savedMatrix);   
                                        float scale = newDist / oldDist;   
                                        matrix.postScale(scale, scale, mid.x, mid.y);   
                                }   
                        }   
                        break;   
                }   
                view.setImageMatrix(matrix);   
                return true;   
        }   
          
         
        private float spacing(MotionEvent event) {   
                float x = event.getX(0) - event.getX(1);   
                float y = event.getY(0) - event.getY(1);   
                return FloatMath.sqrt(x * x + y * y);   
        }   
         
        private void midPoint(PointF point, MotionEvent event) {   
                float x = event.getX(0) + event.getX(1);   
                float y = event.getY(0) + event.getY(1);   
                point.set(x / 2, y / 2);   
        }   
}   

 

  1. imageView.setOnTouchListener(new MulitPointTouchListener ());在xml中要将图片的缩放格式改成Matrix   
  2. 注:android:scaleType="matrix"   
  3. 注:此文档为转载  http://blog.csdn.net/hundsong/article/details/6532292
  评论这张
 
阅读(1250)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017