android动画:
分为三种:View Animation、Drawable Animation和Property Animation
View Animation只能用于View组件上,并且只能完成基本的动画效果(旋转/透明度渐变/尺寸渐变/移动),它无法做到背景颜色的改变等;并且这些动画效果也只是改变了draw时的绘制效果,并没有改变组件实际的尺寸和位置,例如Button使用这种效果时,放大,他的有效位置可点击区域仍是以前的大小,变化的效果也不会对点击事件起作用;
Drawable Animation帧动画,就是事先把准备好的图片弄好,按照一定的顺序播放图片,带到动画效果
Property Animation就是通过设置动画的实际动作属性,完成动画效果;
View Animation:
可以使用Java代码或者xml组件定义来完成;旋转/透明度/尺寸/移动依次依靠 RotateAnimation、AlphaAnimation、 ScaleAnimation和TranslateAnimation
JAVA使用方法:
//旋转
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//参数1: 开始角度
//参数2: 旋转到多少角度 (后四个定义旋转的中心)
//参数3: 值类型,三种Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT分别表示绝对/相对于自身/相对于父控件
//参数4: 值多少 0~1范围内
//透明度
AlphaAnimation(float fromAlpha, float toAlpha)
//这个好理解,不解释;0~1取值 0 完全透明 1完全不透明
//尺寸伸缩
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//参数1: X开始尺寸 < 1 表示收缩 >1表示放大 0 表示收缩到没有
//后面几个参数好理解
//移动
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
//根据类型确定值 移动前的位置 移动后的位置
1. 动画弄好后,调用image.startAnimation(animation)即可开始动画操作
2. 如果用于图像上有多个动画,可以把多个动画加入到AnimationSet里面去addAnimation(Animation a) ;该类也是继承Animation;所有也可以image.startAnimation(animationSet)
XML代码使用方法:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"> //放到这儿才有用处
<translate android:interpolator="@android:anim/decelerate_interpolator"
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="1000"
android:fillAfter="true"/> //保持移动后的效果图;但是放在此处不起作用
</set>
Java里面调用:
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.myanim);
image.startAnimation(anim);