我正在尝试从它的中心点旋转图像单轮,但我无法在所需位置停止,因为我可以旋转但我想在 360'(1 轮)
后停止旋转.
public class RotateRoundActivity extends Activity implements OnTouchListener
{
private ImageView dialer;
//private float y=0;
private float x=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialer = (ImageView) findViewById(R.id.big_button);
dialer.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
x=event.getX();
// y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}
旋转方式@
private void updateRotation(double rot){
float newRot=new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Matrix matrix=new Matrix();
matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
Log.i("demo===>", "matrix==>" + matrix);
// Log.i("demo===", "y===>" + y);
Log.i("demo===", "x===>" + x);
if(x>250){
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
else{
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
}
}
您的建议很重要。
最佳答案
您必须保存以前的rot
值。并在 updateRotation
方法中添加检查,如果 previousRot
位于 360' 度的左侧并且 rot
位于 360' 度的右侧,那么我们制作了1轮需要停止旋转。
顺时针示例代码
if (previousRot >= 300 && previousRot <= 360 && rot >= 0 && rot <= 60) {
rot = 359.99; // or here can be 360'
}
对于逆时针的情况,它几乎是一样的,但是交换了值
if (previousRot >= 0 && previousRot <= 60 && rot >= 300 && rot <= 360) {
rot = 0;
}
此代码将停止旋转。从一开始,previousRot
顺时针应为 0,逆时针应为 359.99
另一种方法是再添加一个变量来存储总行进角度。从一开始,traveledAngle
必须等于 0。如果您按顺时针方向旋转,则必须将其增加 rot
和 previousRot< 之间的差值
。逆时针旋转时减少相同的值。
traveledAngle += rot - previousRot;
当traveledAngle
大于360'时需要停止顺时针旋转,小于0时需要停止逆时针旋转。
关于android - 图像旋转 360 度后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341135/