我正在使用imageView,用户可以通过一个手指触摸拖动此图像进行旋转,将箭头的尾部作为中心。说到实现,我发现图像确实按预期旋转。而不是那样,箭头在收缩箭头时旋转并向下或向右方向站立。到目前为止,这是一个奇怪的轮换。你能告诉我计算旋转中心偏移的正确方法吗?
以下是我的代码
私人ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView);
imageView.setScaleType(ScaleType.MATRIX);
imageView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
double tx = event.getX() - imageView.getWidth()/2;
double ty = event.getY() - imageView.getHeight()/2;
double r = Math.atan2(tx, ty);
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
Matrix matrix = new Matrix();
matrix.postRotate((float) rotation,
imageView.getWidth(), imageView.getHeight());
imageView.setImageMatrix(matrix);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}
});
}