只需移动一根手指而不是两根手指即可调整 ImageView 的大小

Resize ImageView with only one finger movement instead of two

我正在尝试仅用一根手指移动(向上或向下)来更改 ImageView 的高度。下面的代码用两根手指改变高度(比如捏合或反向)。我怎样才能修改这段代码来做我想做的事?谢谢

public class MainActivity extends Activity {


    private  ImageView iv;
    private  Matrix matrix = new Matrix();
    private float scale = 1f;
    private ScaleGestureDetector SGD;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = iv=(ImageView)findViewById(R.id.imageView);
        SGD = new ScaleGestureDetector(this, new ScaleListener());

    }

    public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return true;
    }

    private class  ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
    {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.1f, Math.min(scale, 5.0f));

            matrix.setScale(1, scale);
            iv.setImageMatrix(matrix);
            return true;
        }
    }
}

此代码段应该可以帮助您检测上下

 private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call 
                // computeCurrentVelocity(). Then call getXVelocity() 
                // and getYVelocity() to retrieve the velocity for each pointer ID. 
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                Log.d("", "X velocity: " + 
                        VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
                        pointerId));
                Log.d("", "Y velocity: " + 
                        VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                        pointerId));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }

改变

public boolean onTouchEvent(MotionEvent ev) {
    SGD.onTouchEvent(ev);
    return true;
}

public boolean onTouchEvent(MotionEvent ev) {
    switch(ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN, case MotionEvent.ACTION_UP:
           SGD.onTouchEvent(ev);
           break;
    return true;
}

使用 GestureDetector 进行滚动:

@Bind(R.id.container)
ViewGroup container;

@Bind(R.id.image)
ImageView image;
private GestureDetectorCompat detector;
private float yscale = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    ButterKnife.bind(this);
    image.setScaleType(ImageView.ScaleType.MATRIX);

    detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Matrix m = new Matrix();
            yscale += distanceY / container.getHeight();
            m.setScale(1, yscale, 0, 0);
            image.setImageMatrix(m);
            return true;
        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return detector.onTouchEvent(event);
} detector.onTouchEvent(event);
}