我试图从它的中心点旋转图像单轮,但我无法停在欲望位置,因为我可以旋转,但我想在360'(1 round)
之后停止旋转。
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);
}
}
}
你的建议很明显。
答案 0 :(得分:3)
您必须保存以前的rot
值。如果updateRotation
位于360'度左侧且previousRot
位于360度右侧,则我会进行{1}}方法,然后我们进行1轮并需要停止旋转。{ p>
顺时针方案的示例代码
rot
对于逆时针的情况,它几乎相同,但值交换
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;
}
对于顺时针方式应为0,对于逆时针方向应为359.99
另一种方法是添加一个变量来存储总行进角度。从一开始previousRot
必须等于0.如果您按顺时针方向旋转,则必须将它增加traveledAngle
和rot
之间的差值。逆时针旋转时,将其减小相同的值。
previousRot
当traveledAngle += rot - previousRot;
大于360'时,您需要停止顺时针旋转,当它小于0时,您需要停止逆时针方向旋转。
答案 1 :(得分:2)
我已经使用了你的演示并添加了一些逻辑,新的演示如下:
public class RotateRoundActivity extends Activity implements OnTouchListener {
float rot1=0.0F, rot2=0.0F;
boolean clockwise, rotationDone = false, halfrotated = false;
int rotcall=0;
private ImageView dialer;
//private float y=0;
private int x=0;
//private int y=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=(int) event.getX();
//y=(int) event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}
private void updateRotation(double rot){
float newRot = new Float(rot);
rotcall++;
if(rotcall == 1)
rot1 = new Float(rot);
if(rotcall == 2)
rot2 = new Float(rot);
if(rot1 != 0.0F && rot2 != 0.0F)
if(rot1 < rot2)
clockwise = true;
else
clockwise = false;
System.out.println("Rotate :: "+newRot);
if(clockwise && rot1>=0 ) {
if(newRot < 0)
halfrotated = true;
if(halfrotated && newRot > 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(clockwise && rot1<0) {
if(newRot > 0)
halfrotated = true;
if(halfrotated && newRot < 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(!clockwise && rot1<0) {
if(newRot > 0)
halfrotated = true;
if(halfrotated && newRot < 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(!clockwise && rot1>=0) {
if(newRot < 0)
halfrotated = true;
if(halfrotated && newRot > 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
System.out.println("Rotation Done :: "+rotationDone);
if(!rotationDone) {
//BitmapDrawable bitmapDrawable = (BitmapDrawable) dialer.getDrawable();
//Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable. YOUR_DRBL );
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(newRot, width, height);
System.out.println("x===>" + x);
//System.out.println("y===>" + y);
//if (x > 250) {
Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
dialer.setImageBitmap(reDrawnBitmap);
/*} else {
Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);
dialer.setImageBitmap(reDrawnBitmap);
}*/
}
}
}