捏缩放仅放大到中心

时间:2018-08-15 14:31:42

标签: java android gesture android-gesture

我正在尝试使应用程序中的图像可缩放。它应该能够放大图像的不同部分。就像现在的代码一样,无论我在哪里做缩放手势,它都只会放大到中心,因此我没有机会移动图像或放大其他区域。 什么需要使缩放偏心并移动缩放的图像?

public class MainActivity extends AppCompatActivity {

private ScaleGestureDetector mScaleGestureDetector;
private float mScaleFactor = 1.0f;
private ImageView mImageView;

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

    mImageView = (ImageView)findViewById(R.id.imageView);
    mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
    mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());

    mImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            mScaleGestureDetector.onTouchEvent(event);

            return true;
        }

    });
}


private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector){
        mScaleFactor *= scaleGestureDetector.getScaleFactor();
        mScaleFactor = Math.max(1.0f,
                Math.min(mScaleFactor, 1.7f));
        mImageView.setScaleX(mScaleFactor);
        mImageView.setScaleY(mScaleFactor);

        return true;
    }
}

}

1 个答案:

答案 0 :(得分:1)

出于您的目的,最好的将添加到该库中:

https://github.com/chrisbanes/PhotoView

当然,或者将该库添加为本地模块,并根据需要自定义它。