我是一名Android学生,我想要一些帮助。我正努力在ImageView上进行捏缩放和拖拽/平移。我做了一些研究,但我感到很困惑。我可以将两个事件分开工作,但我尝试将它们一起使用并且捏缩放不起作用。任何帮助我都会感恩。这是我的代码:
...
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);
imageView =(ImageView)findViewById(R.id.imageView);
SGD = new ScaleGestureDetector(this,new ScaleListener());
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float currentX, currentY;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
preX = event.getX();
preY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
currentX = event.getX();
currentY = event.getY();
imageView.scrollBy((int) (preX - currentX), (int) (preY - currentY));
preX = currentX;
preY = currentY;
break;
case MotionEvent.ACTION_UP:
currentX = event.getX();
currentY = event.getY();
imageView.scrollBy((int) (preX - currentX), (int) (preY - currentY));
break;
}
return true;
}
});
}
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(scale, scale);
imageView.setImageMatrix(matrix);
return true;
}
}
}