android ViewPager与图片放大移动冲突

本文探讨了在Android应用中,当使用ViewPager展示图片时遇到的放大和移动冲突问题。通过自定义ImageView,实现了平滑的图片浏览体验,同时支持缩放和滑动操作。针对这一常见问题,提供了有效的解决方案。
摘要由CSDN通过智能技术生成

只需要自定义ImageView

package com.ringing.phone.weight;


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.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

/**
 * Created by houruixiang on 2017/8/22.
 */

@SuppressLint("AppCompatCustomView")
public class ZoomImageView extends ImageView implements ViewTreeObserver.OnGlobalLayoutListener
        , View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
   

    private boolean mOnce;
    private float minScale;
    private float maxScale;
    private float mTouchScale;
    private Matrix mMatrix;
    /**捕获多指触摸的手势*/
    private ScaleGestureDetector mScaleGestureDetector;
    private Object rectF;



    /**------------------------------------------------
     * 自由移动*/
    private float mLastX;
    private float mLastY;

    private boolean isMoveX;
    private boolean isMoveY;

    private boolean isMove;

    private int mPointCount;
    private int mTouchslop;



    /**------------------------------------------------
     * 双击放大+缩小*/
    private GestureDetector mGes;
    private boolean isClick;

    public ZoomImageView(Context context) {
   
        this(context,null);
    }

    public ZoomImageView(Context context, @Nullable AttributeSet attrs) {
   
        this(context, attrs,0);
    }

    public ZoomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
   
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
   


        mMatrix = new Matrix();
        setScaleType(ScaleType.MATRIX);
        mScaleGestureDetector = new ScaleGestureDetector(getContext(), this);
        setOnTouchListener(this);
        mTouchslop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

        mGes = new GestureDetector(getContext(),new GestureDetector.SimpleOnGestureListener(){
   
            @Override
            public boolean onDoubleTap(MotionEvent e) {
   
                float x = e.getX();
                float y = e.getY();
                float scale = getScale();

                if (scale < mTouchScale){
   

                    Log.i("==currScale",String.valueOf(mTouchScale) + ":::" + String.valueOf(scale));
                    //放大的逻辑---注意要渐变
                    if (!isClick){
   
                        isClick = true;
                        postDelayed(new ChangeRunnable(mTouchScale,x,y),16);
                    }

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值