Chinaunix首页 | 论坛 | 博客
  • 博客访问: 861045
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: 嵌入式

2011-08-17 15:45:40

Detects transformation gestures involving more than one pointer ("multitouch") using the supplied MotionEvents.
 TheScaleGestureDetector.OnScaleGestureListener callback will notify users when a particular gesture event has occurred. This class should only be used with MotionEvents reported via touch. To use this class:
下面是测试的代码:
  1. public class TouchView extends View {

  2.  private Context mContext;
  3.  private Paint mPaint;
  4.  private Rect mRect;
  5.  private ScaleGestureDetector mScaleGestureDetector;
  6.  
  7.  public TouchView(Context context) {
  8.   super(context);
  9.   mContext = context;
  10.   mPaint = new Paint();
  11.   mPaint.setColor(Color.BLUE);
  12.   mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  13.   mRect = new Rect();
  14.   mRect.left = 10;
  15.   mRect.top = 10;
  16.   mRect.right = 300;
  17.   mRect.bottom = 400;
  18.   
  19.   mScaleGestureDetector = new ScaleGestureDetector(context, listener);
  20.  }
  21.  
  22.  OnScaleGestureListener listener = new OnScaleGestureListener() {
  23.   
  24.   public void onScaleEnd(ScaleGestureDetector detector) {
  25.    // TODO Auto-generated method stub
  26.    Log.i("OnScaleGestureListener", "onScaleEnd");
  27.   }
  28.   
  29.   public boolean onScaleBegin(ScaleGestureDetector detector) {
  30.    // TODO Auto-generated method stub
  31.    Log.i("OnScaleGestureListener", "onScaleBegin detector.getCurrentSpan() = " + detector.getCurrentSpan());
  32.    detector.getCurrentSpan();
  33.    return true;
  34.   }
  35.   
  36.   public boolean onScale(ScaleGestureDetector detector) {
  37.    // TODO Auto-generated method stub
  38.    float cur = detector.getCurrentSpan();
  39.    float pre = detector.getPreviousSpan();
  40.    float cp = cur - pre;
  41.    Log.i("OnScaleGestureListener", "onScale detector.getCurrentSpan() = " + cur
  42.      + " detector.getPreviousSpan() = " + pre
  43.      + " cur - pre = " + (cur - pre));
  44.    if (cp < -100 && !mScaled) {
  45.     mScaled = true;
  46.     mRect.left = 10+ 50;
  47.     mRect.top = 10 + 50;
  48.     mRect.right = 300 - 50;
  49.     mRect.bottom = 400 - 50;
  50.     invalidate();
  51.    } else if (cp > 100 && mScaled) {
  52.     mScaled = false;
  53.     mRect.left = 10;
  54.     mRect.top = 10 ;
  55.     mRect.right = 300 ;
  56.     mRect.bottom = 400;
  57.     invalidate();
  58.    }
  59.    return false;
  60.   }
  61.  };

  62.  boolean mScaled = false;
  63.  /*@Override
  64.  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  65.   int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  66.   int width = MeasureSpec.getSize(widthMeasureSpec);
  67.   int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  68.   int height = MeasureSpec.getSize(heightMeasureSpec);
  69.   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  70.  }*/



  71.  @Override
  72.  protected void onDraw(Canvas canvas) {
  73.   canvas.drawRect(mRect, mPaint);
  74.  }



  75.  @Override
  76.  public boolean onTouchEvent(MotionEvent event) {
  77.   System.out.println("event.getPointerCount() : " + event.getPointerCount());
  78.   mScaleGestureDetector.onTouchEvent(event);
  79.   return true;
  80.  }
  81. }
阅读(4527) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~