Android 动画
帧动画
AnimationDrawable
属性
android:duration=“2000” 动画持续时间。即这个动画会持续多长时间,单位(ms)
android:fillAfter=“false” 动画播放完毕后,是否会停止在动画结束的状态,优先存在于fillBefore
android:fillBefore=“true” 动画播放完毕后,是否会恢复原始状态
android:fillEnabled=“true” 是否应用与fillBefore的值,默认:true
android:fromXScale=“1” X轴起始缩放倍数
android:fromYScale=“1” Y轴起始缩放倍数
android:pivotX=“50%” 缩放轴点X坐标
android:pivotY=“50%” 缩放轴点Y坐标
android:repeatCount=“0” 重复次数,值infinite为无限一直重复
android:repeatMode=“restart” 播放的动画模式restart表示正序播放,reverse代表倒序播放,默认是restart
android:startOffset=“0” 动画延迟开始时间(多长时间后开始执行动画)
android:toXScale=“2.5” 缩放轴点的X坐标
android:toYScale=“2.5” 缩放轴点的Y坐标
补间(Tween)动画
平移动画
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,200);
缩放动画
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f);
旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0,360,50,5);
透明动画
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
代码实现
package com.example.animationsdemo;
import android.animation.AnimatorSet;
import android.support.v7.app.AppCompatActivity;
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.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3,button4,button5;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.set_alpha);
button2 = findViewById(R.id.set_rotate);
button3 = findViewById(R.id.set_translate);
button4 = findViewById(R.id.set_scale);
button5 = findViewById(R.id.set_all);
imageView = findViewById(R.id.image);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alpha();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rotate();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
translate();
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scale();
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
all();
}
});
}
private void scale() {
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setRepeatCount(1);
imageView.startAnimation(scaleAnimation);
}
private void translate() {
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,200);
translateAnimation.setDuration(3000);
translateAnimation.setRepeatCount(1);
imageView.startAnimation(translateAnimation);
}
private void all() {
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,200);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,50,5);
AnimationSet animationSet = new AnimationSet(this,null);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(5000);
imageView.startAnimation(animationSet);
}
private void rotate() {
RotateAnimation rotateAnimation = new RotateAnimation(0,360,50,5);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatCount(2);
imageView.startAnimation(rotateAnimation);
}
private void alpha() {
Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);
imageView.startAnimation(animation);
// AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
// alphaAnimation.setRepeatCount(3);
// alphaAnimation.setRepeatMode(Animation.REVERSE);
}
}