Android旋转动画
问题描述:
我有一个三角形,每次用户点击屏幕时我都想旋转120度。我有三个旋转动画,从0-120,120-240和240-360度。Android旋转动画
旋转1:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<rotate
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:startOffset="0"
android:fromDegrees="0"
android:toDegrees="120"
/>
</set>
在我的MainActivity的网页我有:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
public class StartPage2 extends Activity {
Animation rotate1;
Animation rotate2;
Animation rotate3;
View triangle2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page_two);
rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate1);
rotate2 = AnimationUtils.loadAnimation(this, R.anim.rotate2);
rotate3 = AnimationUtils.loadAnimation(this, R.anim.rotate3);
triangle2 = (View) findViewById(R.id.triangle2);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
triangle2.startAnimation(rotate2);
return true;
}
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
答
使用此
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
public class StartPage2 extends Activity {
Animation rotate1;
Animation rotate2;
Animation rotate3;
View triangle2;
int currentAnimation = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page_two);
rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate1);
rotate2 = AnimationUtils.loadAnimation(this, R.anim.rotate2);
rotate3 = AnimationUtils.loadAnimation(this, R.anim.rotate3);
triangle2 = (View) findViewById(R.id.triangle2);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
switch(++currentAnimation){
case 1:
triagnle2.startAnimation(rotate1);
break;
case 2:
triagnle2.startAnimation(rotate2);
break;
case 3:
currentAnimation = 0;
triagnle2.startAnimation(rotate3);
break;
}
return true;
}
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
,考虑你的变量前使用private
和那么,你的问题是什么? –