package com.crpcg.domain_hchain.view; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.ScaleGestureDetector.OnScaleGestureListener; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewConfiguration; import android.view.ViewTreeObserver; import android.widget.ImageView; /** * */ public class ZoomImageView extends ImageView implements ViewTreeObserver.OnGlobalLayoutListener, ScaleGestureDetector.OnScaleGestureListener,View.OnTouchListener { @SuppressWarnings("unused") private static final String TAG = "ZoomImageView"; /** * 最大放大倍数 */ public static final float mMaxScale = 4.0f; /** * 默认缩放 */ private float mInitScale = 1.0f; /** * 双击放大比例 */ private float mMidScale=2.0f; /** * 检测缩放手势 多点触控手势识别 独立的类不是GestureDetector的子类 */ ScaleGestureDetector mScaleGestureDetector = null;//检测缩放的手势 /** *检测类似长按啊 轻按啊 拖动 快速滑动 双击啊等等 OnTouch方法虽然也可以 * 但是对于一些复杂的手势需求自己去通过轨迹时间等等判断很复杂,因此我们采用系统 * 提供的手势类进行处理 */ private GestureDetector mGestureDetector; /** * 如果正在缩放中就不向下执行,防止多次双击 */ private boolean mIsAutoScaling; /** * Matrix的对图像的处理 * Translate 平移变换 * Rotate 旋转变换 * Scale 缩放变换 * Skew 错切变换 */ Matrix mScaleMatrix = new Matrix(); /** * 处理矩阵的9个值 */ float[] mMartixValue = new float[9]; public ZoomImageView(Context context) { this(context, null); } public ZoomImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ZoomImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setScaleType(ScaleType.MATRIX); mScaleGestureDetector = new ScaleGestureDetector(context, this); this.setOnTouchListener(this); //缩放的捕获要建立在setOnTouchListener上 //符合滑动的距离 它获得的是触发移动事件的最短距离,如果小于这个距离就不触发移动控件, //如viewpager就是用这个距离来判断用户是否翻页 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); //监听双击事件 SimpleOnGestureListener是OnGestureListener接口实现类, //使用这个复写需要的方法就可以不用复写所有的方法 mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDoubleTap(MotionEvent e) { //如果正在缩放中就不向下执行,防止多次双击 if (mIsAutoScaling) { return true; } //缩放的中心点 float x = e.getX(); float y = e.getY(); //如果当前缩放值小于这个临界值 则进行放大 if (getScale() < mMidScale) { mIsAutoScaling = true; //view中的方法 已x,y为坐标点放大到mMidScale 延时10ms postDelayed(new AutoScaleRunble(mMidScale, x, y), 16); } else { //如果当前缩放值大于这个临界值 则进行缩小操作 缩小到
android 图片 上下左右滑动,能放大放小
最新推荐文章于 2023-02-02 23:41:26 发布
本文介绍了一个Android自定义组件ZoomImageView,实现了图片上下左右滑动及放大缩小的功能。通过ScaleGestureDetector检测缩放手势,GestureDetector处理滑动和双击事件,结合Matrix矩阵变换实现图片的平移和缩放。同时,代码中包含双击放大、缩小的逻辑以及防止多次双击的处理。
摘要由CSDN通过智能技术生成