版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30241709/article/details/79771309
Animations使用步骤:
- 创建一个AnimationSet对象
- 根据需要创建相应的Animation对象
- 为Animation设置相应的数据
- 将Animation动画放入AnimationSet中
- 使用控件对动画执行AnimationSet
package com.example.birdguan.exercise;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button b_alpha, b_scale, b_rotate, b_translate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_alpha = findViewById(R.id.btn_alpha);
b_scale = findViewById(R.id.btn_scale);
b_rotate = findViewById(R.id.btn_rotate);
b_translate = findViewById(R.id.btn_translate);
b_alpha.setOnClickListener(this);
b_rotate.setOnClickListener(this);
b_scale.setOnClickListener(this);
b_translate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_alpha:
AnimationSet animationSet_alpha = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet_alpha.addAnimation(alphaAnimation);
b_alpha.setAnimation(animationSet_alpha);
Log.d("debug", "alpha");
break;
case R.id.btn_scale:
AnimationSet animationSet_scale = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0,4,0,4);
scaleAnimation.setDuration(2000);
animationSet_scale.addAnimation(scaleAnimation);
b_scale.setAnimation(animationSet_scale);
Log.d("debug", "scale");
break;
case R.id.btn_rotate:
AnimationSet animationSet_rotate = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0.0f,90.0f);
rotateAnimation.setDuration(2000);
animationSet_rotate.addAnimation(rotateAnimation);
b_rotate.setAnimation(animationSet_rotate);
Log.d("debug", "rotate");
break;
case R.id.btn_translate:
AnimationSet animationSet_translate = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0,1.0f,0.0f,1.0f);
translateAnimation.setDuration(2000);
animationSet_translate.addAnimation(translateAnimation);
b_translate.setAnimation(animationSet_translate);
Log.d("debug", "translate");
break;
default:
break;
}
}
}