如何在android中顺时针和逆时针旋转图像?
问题描述:
我想知道如何用android中的onTouch()事件顺时针和逆时针旋转图像。以及如何通过onTouchListener找出它是顺时针还是逆时针旋转?由于如何在android中顺时针和逆时针旋转图像?
答
基本上你可以将你的形象自己的类,扩展视图,并实现OnClickListener像这里面:
public class CustomImageView extends View implements OnClickListener{
...
}
覆盖您CustomImageView类中的方法的onDraw。旋转可以通过旋转画布对象在onDraw内部实现。
第三,实现onClick以获得点击事件并根据您的需要进行轮换。
的基本布局可能看起来像:
public class CustomImageView extends View implements OnClickListener{
public void onClick (View v){
// process click here
// invalidate after click processing in order to redraw
this.invalidate();
}
protected void onDraw(Canvas canvas) {
// draw your image here, might be bitmap or other stuff
// rotate canvas now, your logic on clockwise or
// counterclockwise rotation goes here
canvas.rotate(-90.0f, centerx, centery);
}
}
答
您可以尝试使用此功能得到x和从MotionEvent对象的y值的旋转。 在那里,我仍然需要从给定的矢量x和矢量y的附加计算中找到方向。
private int calculateAngle(float GETX, float GETY) {
int direction = 1;
double tx = (int) GETX - object_center.x;
double ty = (int) GETY - object_center.y;
double angleInDegrees = Math.atan2(ty, tx) * 180/Math.PI;
int area = 0;
int ACTUAL_ANGLE = 270;
if (angleInDegrees < 0 && angleInDegrees < -90) {
// Need to add
// 270+angle degrees
// =================
ACTUAL_ANGLE += (int) (180 + angleInDegrees) * direction;
} else if (angleInDegrees < 0 && angleInDegrees > -90) {
// Need to add
// 90+angle degrees
// =================
ACTUAL_ANGLE = (int) (90 + angleInDegrees);
} else if (angleInDegrees > 0)
ACTUAL_ANGLE = 90 + (int) angleInDegrees;
return ACTUAL_ANGLE;
}
使用旋转动画,也有很多很好的例子在那里...... http://developer.android.com/reference/android/view/animation/RotateAnimation.html – testingtester 2012-04-22 08:54:46
谢谢,但我想知道在android中使用onTouch()事件顺时针和逆时针旋转图像的方式 – secretlm 2012-04-22 09:01:17