package com.imooc.viewanim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnAlpha(View view) {
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
view.startAnimation(aa);
}
public void btnRotate(View view) {
RotateAnimation ra = new RotateAnimation(90, 240, 50, 50);
ra.setDuration(1000);
view.startAnimation(ra);
}
public void btnRotateSelf(View view) {
RotateAnimation ra = new RotateAnimation(0, 360,
RotateAnimation.RELATIVE_TO_SELF, 0.5F,
RotateAnimation.RELATIVE_TO_SELF, 0.5F);
ra.setDuration(1000);
view.startAnimation(ra);
}
public void btnTranslate(View view) {
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
view.startAnimation(ta);
}
public void btnScale(View view) {
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
sa.setDuration(1000);
view.startAnimation(sa);
}
public void btnScaleSelf(View view) {
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5F,
Animation.RELATIVE_TO_SELF, 0.5F);
sa.setDuration(1000);
view.startAnimation(sa);
}
public void btnSet(View view) {
AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
as.addAnimation(aa);
TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
ta.setDuration(1000);
as.addAnimation(ta);
view.startAnimation(as);
}
}
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alpha"
android:layout_margin="10dp"
android:onClick="btnAlpha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:layout_margin="10dp"
android:onClick="btnRotate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate_self"
android:layout_margin="10dp"
android:onClick="btnRotateSelf" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate"
android:layout_margin="10dp"
android:onClick="btnTranslate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale"
android:layout_margin="10dp"
android:onClick="btnScale" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale_Self"
android:layout_margin="10dp"
android:onClick="btnScaleSelf" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Anim Set"
android:layout_margin="10dp"
android:onClick="btnSet" />
</LinearLayout>
