版权声明
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16481829.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
前言
ScaleGestureDetector是实现多指操作缩放
GestureDetector 是实现单指追踪
这2个类的使用都很简单,下面的代码中有注释,另外其实也可以实现在TextView或者其他View中
代码
import android.content.Context; import android.graphics.Canvas; import android.graphics.Matrix; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; import com.facebook.drawee.view.SimpleDraweeView; public class ZoomableDraweeView4 extends ImageView { private ScaleGestureDetector mScaleGestureDetector; private GestureDetector mGestureDetector; private Matrix mCurrentMatrix; private float mMidX; private float mMidY; public ZoomableDraweeView4(Context context) { super(context); initScaleGestureDetector(); } public ZoomableDraweeView4(Context context, AttributeSet attrs) { super(context, attrs); initScaleGestureDetector(); } public ZoomableDraweeView4(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initScaleGestureDetector(); } private void initScaleGestureDetector() { mCurrentMatrix = new Matrix(); mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() { @Override public boolean onScaleBegin(ScaleGestureDetector detector) { //缩放开始 return true; } @Override public boolean onScale(ScaleGestureDetector detector) { float scaleFactor = detector.getScaleFactor(); if (mMidX == 0f) { //最小X轴缩放 mMidX = getWidth() / 2f; } if (mMidY == 0f) { //最小Y轴缩放 mMidY = getHeight() / 2f; } mCurrentMatrix.postScale(scaleFactor, scaleFactor, mMidX, mMidY); //让View失效重绘 invalidate(); return true; } @Override public void onScaleEnd(ScaleGestureDetector detector) { //缩放结束 } }); GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapConfirmed(MotionEvent e) { //单击确认 return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.e("zh", "onScroll: distanceX = " + distanceX); Log.e("zh", "onScroll: distanceY = " + distanceY); mCurrentMatrix.postTranslate(-distanceX, -distanceY); invalidate(); return true; } }; mGestureDetector = new GestureDetector(getContext(), gestureListener); } @Override protected void onDraw(Canvas canvas) { int saveCount = canvas.save(); canvas.concat(mCurrentMatrix); super.onDraw(canvas); canvas.restoreToCount(saveCount); } @Override public boolean onTouchEvent(MotionEvent event) { //这里将触摸事件转接给ScaleGestureDetector与GestureDetector mScaleGestureDetector.onTouchEvent(event); //mScaleGestureDetector.isInProgress() 如果缩放功能没有正在处理,就说明是单指操作中 if (!mScaleGestureDetector.isInProgress()) { mGestureDetector.onTouchEvent(event); } return true; } }
End
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16481829.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。