1.最常用的帧动画
xml实现:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/pic1"
android:duration="100"
/>
<item
android:drawable="@drawable/pic2"
android:duration="100"
/>
<item
android:drawable="@drawable/pic3"
android:duration="100"
/>
</animation-list>
调用:
//对于动画的调用
// AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
// animationDrawable.start();
代码实现:
AnimationDrawable animationDrawable1 = new AnimationDrawable();
for (int i = 1; i < 4; i++) {
int id = getResources().getIdentifier("pic" + i, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
animationDrawable1.addFrame(drawable, 300);
}
imageView.setBackgroundDrawable(animationDrawable1);
//是否只是播放一遍
animationDrawable1.setOneShot(false);
animationDrawable1.start();
2.补间动画--渐变,翻转,平移,缩放
//补间动画-渐变
AlphaAnimation animation=new AlphaAnimation(0,1);//0表示头像1表示不透明
animation.setDuration(5000);
animation.setFillAfter(true);//动画结束后保留最后的状态
imageView.setAnimation(animation);
animation.start();
//补间动画-平移
//Animation.RELATIVE_TO_PARENT//相对于父布局,这需要是个百分比的数值,0到1
//Animation.ABSOLUTE//是一个准确的数值
//Animation.RELATIVE_TO_SELF//相对于自己的尺寸,这里也是一个百分比的数值,0到1
//注意,不管怎样,左边原点都是自身view的左上角
//int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue
TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0.0f,Animation.RELATIVE_TO_PARENT,
0.8f,Animation.RELATIVE_TO_PARENT,0.0f,Animation.RELATIVE_TO_PARENT,0.6f);
//对于这个,注意,动画是从view自身的位置开始移动的,并不是从父布局的0,0坐标开始移动
translateAnimation.setDuration(5000);
translateAnimation.setFillAfter(true);
imageView.setAnimation(translateAnimation);
translateAnimation.start();
//补间动画-旋转,开始的角度,结束的角度,旋转中心在哪
//float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue
RotateAnimation rotateAnimation=new RotateAnimation(0f,270f,Animation.RELATIVE_TO_SELF,0.2f,Animation.RELATIVE_TO_SELF,0.2f);
rotateAnimation.setDuration(5000);
rotateAnimation.setFillAfter(true);
imageView.setAnimation(rotateAnimation);
rotateAnimation.start();
//补间动画-缩放
//和旋转类似,起始的xy尺寸比例,以及类似缩放中心的一个数据
//float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue
ScaleAnimation scaleAnimation=new ScaleAnimation(0,1.0f,0,1.0f,Animation.RELATIVE_TO_SELF,0.2f,Animation.RELATIVE_TO_SELF,0.2f);
scaleAnimation.setDuration(5000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setRepeatCount(100);
imageView.setAnimation(scaleAnimation);
scaleAnimation.start();