生活随笔
收集整理的這篇文章主要介紹了
ScaleGestureDetector使用注意事项
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:一定要通過view的onTouchEvent調用mScaleGestureDetector.onTouchEvent(ev);,只有這樣
才能調用回調函數:onScaleBegin
具體可以參加:android源碼:KenBurnsActivity.java
下面是轉載的文章:
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: Create an instance of the?ScaleGestureDetector?for your?View In the?(View's )onTouchEvent (MotionEvent)?method ensure you call?(ScaleGestureDetector's )onTouchEvent (MotionEvent). The methods defined in your callback will be executed when the events occur. 下面是測試的代碼:
public ?class ?TouchView?extends ?View ?{ ?private ?Context ?mContext; ?private ?Paint ?mPaint; ?private ?Rect mRect; ?private ?ScaleGestureDetector mScaleGestureDetector; ? ?public ?TouchView( Context ?context ) ?{ ??super ( context ) ; ??mContext?= ?context ; ??mPaint?= ?new ?Paint ( ) ; ??mPaint. setColor ( Color . BLUE) ; ??mPaint. setFlags( Paint . ANTI_ALIAS_FLAG) ; ??mRect?= ?new ?Rect( ) ; ??mRect. left?= ?10; ??mRect. top?= ?10; ??mRect. right?= ?300; ??mRect. bottom?= ?400; ?? ??mScaleGestureDetector?= ?new ?ScaleGestureDetector( context , ?listener) ; ?} ? ?OnScaleGestureListener listener?= ?new ?OnScaleGestureListener( ) ?{ ?? ??public ?void ?onScaleEnd( ScaleGestureDetector?detector ) ?{ ???// TODO Auto-generated method stub ???Log . i( "OnScaleGestureListener" , ?"onScaleEnd" ) ; ??} ?? ??public ?boolean ?onScaleBegin( ScaleGestureDetector?detector ) ?{ ???// TODO Auto-generated method stub ???Log . i( "OnScaleGestureListener" , ?"onScaleBegin?detector .getCurrentSpan() = " ?+ ?detector . getCurrentSpan( ) ) ; ???detector . getCurrentSpan( ) ; ???return ?true; ??} ?? ??public ?boolean ?onScale( ScaleGestureDetector?detector ) ?{ ???// TODO Auto-generated method stub ???float ?cur?= ?detector . getCurrentSpan( ) ; ???float ?pre?= ?detector . getPreviousSpan( ) ; ???float ?cp?= ?cur?- ?pre; ???Log . i( "OnScaleGestureListener" , ?"onScale?detector .getCurrentSpan() = " ?+ ?cur ?????+ ?"?detector .getPreviousSpan() = " ?+ ?pre? ?????+ ?" cur - pre = " ?+ ?( cur?- ?pre) ) ; ???if ?( cp?< ?- 100?& & ?! mScaled) ?{ ????mScaled?= ?true; ????mRect. left?= ?10+ ?50; ????mRect. top?= ?10?+ ?50; ????mRect. right?= ?300?- ?50; ????mRect. bottom?= ?400?- ?50; ????invalidate ( ) ; ???} ?else ?if ?( cp?> ?100?& & ?mScaled) ?{ ????mScaled?= ?false; ????mRect. left?= ?10; ????mRect. top?= ?10?; ????mRect. right?= ?300?; ????mRect. bottom?= ?400; ????invalidate ( ) ; ???} ???return ?false; ??} ?} ; ?boolean ?mScaled?= ?false; ?/*@Override ?protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ??int widthMode = MeasureSpec.getMode(widthMeasureSpec); ??int width = MeasureSpec.getSize(widthMeasureSpec); ??int heightMode = MeasureSpec.getMode(heightMeasureSpec); ??int height = MeasureSpec.getSize(heightMeasureSpec); ??super.onMeasure(widthMeasureSpec, heightMeasureSpec); ?}*/ ?@Override ?protected ?void ?onDraw( Canvas ?canvas ) ?{ ??canvas . drawRect ( mRect, ?mPaint) ; ?} ?@Override ?public ?boolean ?onTouchEvent ( MotionEvent?event ) ?{ ??System . out. println ( "event.getPointerCount() : " ?+ ?event . getPointerCount( ) ) ; ??mScaleGestureDetector. onTouchEvent ( event ) ; ??return ?true; ?} }
轉載于:https://www.cnblogs.com/tanqiantot/archive/2012/10/12/3126845.html
總結
以上是生活随笔 為你收集整理的ScaleGestureDetector使用注意事项 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。