一、帧动画(图片轮播)
1.新建Drawable Resource File(banner.xml),将根标签改为animation-list,设置图片和播放时间
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pic1"
android:duration="2000"/>
<item android:drawable="@drawable/pic2"
android:duration="2000"/>
<item android:drawable="@drawable/pic3"
android:duration="2000"/>
</animation-list>
2.在java文件中,将这个xml文件设置为ImageView的图片资源,获取ImageView的Drawable后强制转化为动画类型并启动
iv.setImageResource(R.drawable.banner);
AnimationDrawable anim = (AnimationDrawable) iv.getDrawable();
anim.start();
二、补间动画-淡入淡出
Animation anim = new AlphaAnimation(1f,0f);
iv.startAnimation(anim);
构造函数表示透明的的变化,(1f,0f)表示淡出,(0f,1f)表示淡入
三、补间动画-旋转
//Animation anim = new RotateAnimation(0,180);
//Animation anim = new RotateAnimation(0,180,50,50);
Animation anim = new RotateAnimation(0,180,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
iv.startAnimation(anim);
构造函数1:(fromDegrees,toDegrees)
旋转开始的角度、旋转结束的角度
以左上顶点(0,0)为旋转中心
构造函数2:(fromDegrees,toDegrees,pivotX,pivotY)
旋转开始的角度、旋转结束的角度、(pivotX,pivotY)旋转中心
构造函数3:旋转开始的角度、旋转结束的角度、(pivotXType,pivotX,pivotYType,pivotY)旋转中心
从fromDegrees到toDegrees的变化为正表示顺时针旋转,为负表示逆时针旋转,旋转角度可以大于360度
pivotX为到左侧的距离,pivotY为到顶部的距离
pivotXType、pivotYType可为Animation.ABSOLUTE(绝对位置)、Animation.RELATIVE_TO_PARENT(相对于父控件)、Animation.RELATIVE_TO_SELF(相对于自身)。第三种构造函数使用相对位置时,pivotX、pivotY可为0.5f…等表示倍数
四、补间动画-缩放
//Animation anim = new ScaleAnimation(1f,0.5f,1f,0.5f);
//Animation anim = new ScaleAnimation(1f,0.5f,1f,0.5f,50,50);
Animation anim = new ScaleAnimation(1f,0.5f,1f,0.5f,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
iv.startAnimation(anim);
构造函数1:(fromX,toX,fromY,toY)
长从原来的fromX倍变为toX倍,高从原来的fromY倍变为toY倍
以左上顶点(0,0)为缩放中心
构造函数2:(fromX,toX,fromY,toY,pivotX,pivotY)
长从原来的fromX倍变为toX倍,高从原来的fromY倍变为toY倍,(pivotX,pivotY)缩放中心
构造函数3:(fromX,toX,fromY,toY,pivotXType,pivotX,pivotYType,pivotY)
长从原来的fromX倍变为toX倍,高从原来的fromY倍变为toY倍,(pivotXType,pivotX,pivotYType,pivotY)缩放中心
从fromX(Y)到toX(Y)增大为图片长(宽)放大,减小为图片长(宽)缩小
其余参数与旋转动画相同
五、补间动画-移动
//Animation anim = new TranslateAnimation(0,50,0,50);
Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,1f,Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,1f);
iv.startAnimation(anim);
构造函数1:(fromXDelta,toXDelta,fromYDelta,toYDelta)
以原来左上顶点为(0,0),左上顶点从(fromXDelta,fromYDelta)移动到(toXDelta,toYDelta),是绝对位置
构造函数2:(fromXType,fromXValue,toXType,toXValue,fromYType,fromYValue,toYType,toYValue)
Type为Animation.ABSOLUTE(绝对位置)、Animation.RELATIVE_TO_PARENT(相对于父控件)或Animation.RELATIVE_TO_SELF(相对于自身);Value为0.5f…等表示倍数
四个动画的常用属性:
Animation anim = new AlphaAnimation(1f,0f);//定义
anim.setDuration(3000);//设置动画时间3000ms
anim.setRepeatCount(3);//设置动画重复3次(加上startAnimation(anim)一共4次)
anim.setFillAfter(true);//true动画结束后停留,false动画结束后恢复原来状态
anim.setStartOffset(2000);//设置延迟2000ms开始动画
iv.startAnimation(anim);//开始动画
六、使用动画切换Activity
startActivity(new Intent(LoadingActivity.this,MainActivity.class));
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
第一个参数:进入的Activity显示的动画资源
第二个参数:退出的Activity显示的动画资源