import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
}
public void rtest(View v) {
// 引入定义的动画文件xml
Animation anim = AnimationUtils.loadAnimation(this, R.anim.r_test);
tv.startAnimation(anim);
// Animation.INFINITE
}
public void settest(View v){
Animation anim = AnimationUtils.loadAnimation(this, R.anim.test);
tv.startAnimation(anim);
}
public void stest(View v) {
// 引入定义的动画文件xml
Animation anim = AnimationUtils.loadAnimation(this, R.anim.s_test);
tv.startAnimation(anim);
}
public void alphatest(View v) {
// 引入定义的动画文件xml
Animation anim = AnimationUtils.loadAnimation(this, R.anim.a_test);
tv.startAnimation(anim);
// 添加动画监听事件
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "动画开始播放", 0).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "动画重复播放了", 0).show();
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "动画结束了", 0).show();
}
});
}
public void transtest(View v) {
// 引入定义的动画文件xml
Animation anim = AnimationUtils.loadAnimation(this, R.anim.t_test);
tv.startAnimation(anim);
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="透明度"
android:onClick="alphatest" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="平移"
android:onClick="transtest" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="缩放"
android:onClick="stest" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="旋转"
android:onClick="rtest" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignParentRight="true"
android:text="test"
android:onClick="settest"/>
</RelativeLayout>