-2
我們可以在Android中使用手指沿着其中心旋轉具有源代碼(附加圖像)的imageview嗎? 像轉向一樣旋轉Imageview?
我們可以在Android中使用手指沿着其中心旋轉具有源代碼(附加圖像)的imageview嗎? 像轉向一樣旋轉Imageview?
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);
}
}
參考andygeeks.blogspot.in。 – EED
結帳 - > https://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868 – AndiGeeky
檢查這個網址作爲參考: - HTTP:// andygeeks.blogspot.in/2014/05/how-to-animate-image-to-rotate-with.html – Bhavnik
感謝您回覆AndiGeeky。但它並沒有圍繞它的中心散去。請給你寶貴的建議,以糾正這個問題 – EED