平移动画
1.xml方式,在res下新建anim的文件夹
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="80"
android:fromYDelta="0"
android:toYDelta="80"
android:fillAfter ="true"
android:duration="800">
2.代码
public void TransAnim(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.trans_anim);
mTextView.startAnimation(animation);
}
3.效果
toXDelta和toYDelta如果为数值80时表示向右和向下同时平移80px
![](https://img.haomeiwen.com/i5872156/520970658643f4ae.gif)
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="180%"
android:fromYDelta="0"
android:toYDelta="80%"
android:fillAfter ="true"
android:duration="800">
toXDelta和toYDelta如果数值后面加上%号表示移动的距离为该控件宽高的百分之多少,如180%为控件的1.8倍宽度的距离
![](https://img.haomeiwen.com/i5872156/170d516ed0830ef9.gif)
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="50%p"
android:fromYDelta="0"
android:toYDelta="50%p"
android:fillAfter ="true"
android:duration="800">
toXDelta和toYDelta如果数值后面加上%p表示移动的距离为父控件宽高的百分之多少,如50%p为父控件的0.5倍宽度的距离
![](https://img.haomeiwen.com/i5872156/30929a668ff904ae.gif)
透明度动画
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0.3"
android:fillAfter ="true"
android:duration="800">
</alpha>
public void TransAnim(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
mTextView.startAnimation(animation);
}
fromAlpha="1"和toAlpha="0.3"表示从透明度从100%到30%
![](https://img.haomeiwen.com/i5872156/5c6129f9843cd2b5.gif)
缩放动画
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.8"
android:fromYScale="0.0"
android:toYScale="1.8"
android:duration="600"
android:fillAfter="true">
</scale>
public void ScaleAnim(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
mTextView.startAnimation(animation);
}
缩放动画中2个属性pivotX和pivotY,为pivotX数值30时表示从该控件最左边加30px的位置开始缩放,pivotY的意义也一样
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.8"
android:fromYScale="0.0"
android:toYScale="1.8"
android:duration="600"
android:pivotX="30"
android:pivotY="30"
android:fillAfter="true">
</scale>
![](https://img.haomeiwen.com/i5872156/6cfdf3e892b496ef.gif)
pivotX数值为50%时表示从控件的宽度的50%的位置开始缩放,pivotY的意义也一样
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.8"
android:fromYScale="0.0"
android:toYScale="1.8"
android:duration="600"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true">
</scale>
![](https://img.haomeiwen.com/i5872156/ffb0d27fecbdedc3.gif)
pivotX数值为50%p时表示从该控件的最左边加上父控件宽度的50%的位置开始缩放,pivotY的意义也一样
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.8"
android:fromYScale="0.0"
android:toYScale="1.8"
android:duration="600"
android:pivotX="50%p"
android:pivotY="50%p"
android:fillAfter="true">
</scale>
![](https://img.haomeiwen.com/i5872156/0cecc977ae1d3f0f.gif)
旋转动画
public void RotateAnim(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
mTextView.startAnimation(animation);
}
缩放动画和旋转动画一样都可以设置pivotX和pivotY,意义一样
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50"
android:pivotY="50"
android:toDegrees="120">
</rotate>
![](https://img.haomeiwen.com/i5872156/83b5610197ecd666.gif)
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120">
</rotate>
![](https://img.haomeiwen.com/i5872156/3d31d9ea198e7853.gif)
Set
当我们希望几组动画一起播放时就需要set标签
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true">
<alpha android:fromAlpha="0" android:toAlpha="1">
</alpha>
<scale android:pivotX="50%" android:pivotY="50%"
android:fromXScale=".3" android:toXScale="1.5" android:fromYScale=".3" android:toYScale="1.5">
</scale>
<rotate android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0" android:toDegrees="540">
</rotate>
</set>
public void SetAnim(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.set_anim);
mTextView.startAnimation(animation);
}
![](https://img.haomeiwen.com/i5872156/c2e560ca1e630318.gif)
android:duration 动画持续时间,以毫秒为单位
android:fillAfter 如果设置为true,控件动画结束时,将保持动画最后时的状态
android:fillBefore 如果设置为true,控件动画结束时,还原到开始动画前的状态
android:fillEnabled 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
android:repeatCount 重复次数
android:repeatMode 重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
网友评论