package com.example.myappanimator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_alpha;
private Button btn_rotation;
private Button btn_scale;
private Button btn_translation;
private ImageButton btn_img;
private Button btn_translat;
private ImageView tv_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_rotation = (Button) findViewById(R.id.btn_rotation);
btn_scale = (Button) findViewById(R.id.btn_scale);
btn_translation = (Button) findViewById(R.id.btn_translation);
btn_img = (ImageButton) findViewById(R.id.btn_img);
btn_alpha.setOnClickListener(this);
btn_rotation.setOnClickListener(this);
btn_scale.setOnClickListener(this);
btn_translation.setOnClickListener(this);
btn_img.setOnClickListener(this);
btn_translat = (Button) findViewById(R.id.btn_translat);
btn_translat.setOnClickListener(this);
tv_img = (ImageView) findViewById(R.id.tv_img);
tv_img.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
///透明度动画
case R.id.btn_alpha:
ObjectAnimator alpha = ObjectAnimator.ofFloat(btn_img, "alpha", new float[]{0.0f, 0.2f, 0.5f, 0.8f, 1.0f});
alpha.setDuration(10000);//设置动画时长
alpha.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
alpha.setRepeatCount(2);//动画执行的次数
alpha.start();//开启动画
break;
//旋转动画
case R.id.btn_rotation:
ObjectAnimator rotationY = ObjectAnimator.ofFloat(btn_img, "rotationY", 0f, 360f);
rotationY.setDuration(5000);//设置动画时长
rotationY.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
rotationY.setRepeatCount(2);//动画执行的次数
rotationY.start();//开启动画
break;
//缩放动画
case R.id.btn_scale:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(btn_img, "scaleX", 1f, 3f, 1f);
scaleX.setDuration(5000);//设置动画时长
scaleX.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
scaleX.setRepeatCount(2);//动画执行的次数
scaleX.start();//开启动画
break;
//平移动画
case R.id.btn_translation:
float curTranslationX = btn_img.getTranslationX();
ObjectAnimator translationX = ObjectAnimator.ofFloat(btn_img, "translationX", curTranslationX, -500, curTranslationX);
translationX.setDuration(5000);//设置动画时长
translationX.setRepeatMode(ObjectAnimator.RESTART);//动画执行的模式
translationX.setRepeatCount(2);//动画执行的次数
translationX.start();//开启动画
break;
//组合动画
case R.id.btn_img:
ObjectAnimator moveIn = ObjectAnimator.ofFloat(btn_img, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(btn_img, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(btn_img, "alpha", 1f, 0f, 1f);
AnimatorSet animaSet = new AnimatorSet();
animaSet.play(rotate).with(fadeInOut).after(moveIn);
animaSet.setDuration(5000);
animaSet.start();
Toast.makeText(this, "您点击了动画!!!", Toast.LENGTH_SHORT).show();
break;
//补间动画
case R.id.btn_translat:
TranslateAnimation translateAnimation = new TranslateAnimation(0,300,0,300);
//指定动画的时长
translateAnimation.setDuration(3000);
//设置重复的次数....总共运行了重复次数+1
translateAnimation.setRepeatCount(1);
//设置动画结束以后保持最后的状态
translateAnimation.setFillAfter(true);
tv_img.setAnimation(translateAnimation);
tv_img.startAnimation(translateAnimation);
break;
case R.id.tv_img:
Toast.makeText(this, "您点击了动画!!!", Toast.LENGTH_SHORT).show();
break;
}
}
}
//
LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>
Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:text=”透明度动画”
android:id=”@+id/btn_alpha”/>
Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:text=”旋转动画”
android:id=”@+id/btn_rotation”/>
Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:text=”缩放动画”
android:id=”@+id/btn_scale”/>
Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:text=”平移动画”
android:id=”@+id/btn_translation”/>
Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:text=”补间动画”
android:id=”@+id/btn_translat”/>
/LinearLayout>
ImageButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/btn_img”
android:src=”@drawable/f”
android:layout_gravity=”center”
android:layout_marginTop=”90dp”
/>
ImageView
android:layout_width=”90dp”
android:layout_height=”90dp”
android:src=”@drawable/hy3”
android:id=”@+id/tv_img”/>