2016-11-30 50 views
-2

我们可以在Android中使用手指沿着其中心旋转具有源代码(附加图像)的imageview吗? enter image description here像转向一样旋转Imageview?

+0

结帐 - > https://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868 – AndiGeeky

+0

检查这个网址作为参考: - HTTP:// andygeeks.blogspot.in/2014/05/how-to-animate-image-to-rotate-with.html – Bhavnik

+0

感谢您回复AndiGeeky。但它并没有围绕它的中心散去。请给你宝贵的建议,以纠正这个问题 – EED

回答

1
public class MainActivity extends Activity implements OnTouchListener{ 
private ImageView wheel; 
private double mCurrAngle = 0; 
private double mPrevAngle = 0; 
ImageView bask; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

wheel=(ImageView)findViewById(R.id.imageView1); 
wheel.setOnTouchListener(this); 
} 

@Override 
public boolean onTouch(final View v, MotionEvent event) { 
final float xc = wheel.getWidth()/2; 
final float yc = wheel.getHeight()/2; 
final float x = event.getX(); 
final float y = event.getY(); 
switch (event.getAction()) { 
case MotionEvent.ACTION_DOWN: { 
wheel.clearAnimation(); 
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y)); 
break; 
} 
case MotionEvent.ACTION_MOVE: { 
mPrevAngle = mCurrAngle; 
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y)); 
animate(mPrevAngle, mCurrAngle, 0); 
System.out.println(mCurrAngle); 
break; 
} 
case MotionEvent.ACTION_UP : { 
mPrevAngle = mCurrAngle = 0; 
break; 
} 
} 
return true; 
} 
private void animate(double fromDegrees, double toDegrees, long durationMillis) { 
final RotateAnimation rotate = new RotateAnimation((float) fromDegrees, (float) toDegrees, 
RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
rotate.setDuration(durationMillis); 
rotate.setFillEnabled(true); 
rotate.setFillAfter(true); 
wheel.startAnimation(rotate); 
System.out.println(mCurrAngle); 
} 
} 
+0

参考andygeeks.blogspot.in。 – EED