android tween动画主要有4种,分别为 透明 比例缩放 移动 和转动
两种创建方式如下:
1. 代码里面创建 动画:
透明:
(初次使用,主要明白其大致流程和参数的含义)具体参见SDK文档
//透明动画
Animation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(2000);
// -1 表示无限重复
alphaAnimation.setRepeatCount(-1);
// 设置动画
imageView.setAnimation(alphaAnimation);
alphaAnimation.start();
比例:
//相当于放大了 3 倍
//关键是参数的含义需要理解
Animation scaleAnimation = new ScaleAnimation(1,3,1,3);
//Animation scaleAnimation = new ScaleAnimation(1, 3, 1, 3,Animation.ZORDER_BOTTOM, 1,Animation.ZORDER_NORMAL, 1);
scaleAnimation.setDuration(3000);
scaleAnimation.setRepeatCount(-1);
imageView.setAnimation(scaleAnimation);
scaleAnimation.startNow();
移动:
// 移动动画
Animation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
translateAnimation.setDuration(2000);
translateAnimation.setRepeatCount(-1);
imageView.setAnimation(translateAnimation);
translateAnimation.startNow();
旋转:
//旋转动画
/*
* 后两个参数可以理解为 旋转的 中心点的坐标
*/
Animation rorateAnimation = new RotateAnimation(0, 360, 100,100);
rorateAnimation.setDuration(3000);
rorateAnimation.setRepeatCount(-1);
imageView.setAnimation(rorateAnimation);
rorateAnimation.startNow();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
在xml文件里面一般的创建方式:
以 缩放为例子:(在res/anim下面建立scale.xml)
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%" //X轴缩放的位置
android:pivotY="50%"
android:duration="2000">
</scale>
代码里面:
Animation animation = AnimationUtils.loadAnimation(this,R.anim.scale);
imageView.starttAnimation(animation);
** 注意 这代码里面 和上面用代码创建的区别
其余三种 动画的创建方式类似.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
如果动画中的图像变换比较有规律时,可以才用自动生成中间图像的方式生成动画,例如图像的移动、旋转、缩放、透明度渐变、正方形变圆形等,这些图像变化过程中的图像都可以根据一定的算法自动生成,我们只需要指定动画的第一帧和最后一帧图像即可,这种自动生成中间图像的动画就是补间动画。
interpolator:表示动画渲染器(通用)。有三个值accelerate_interpolator(动画加速器)、decelerate_interpolator(动画减速器)、accelerate_decelerate_interpolator(动画加速减速器)。加速器是动画开始速度慢,越到后来速度越快,其它加速器类似理解。
fromXDelta:起始X坐标
fromYDelta:起始Y坐标
toXDelta:目标X坐标
toYDelta:目标Y坐标
duration:动画持续时间(通用)
repeatCount:动画重复次数(通用)
repeatMode:动画重复模式(通用)
2.旋转动画
<?xml version="1.0" encoding="utf-8"?><!-- 旋转动画 -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
3.缩放动画
<?xml version="1.0" encoding="utf-8"?><!-- 缩放动画 -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
</set>
fromXScale:起始X方向缩放比例 0.0最小 1.0原大小 2.0两倍大小
fromYScale:起始Y方向缩放比例
toXScale:结束X方向缩放比例
toYScale:结束Y方向缩放比例
4.透明度动画
<set xmlns:android="http://schemas.android.com/apk/res/android"
以上四种动画可以自由组合成需要的动画,如下即图像缩放同时透明度也改变
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
</set>
装载补间动画文件需要使用AnimationUtils.loadAnimation()方法,如下:
将补间动画应用到控件的方法有两种:
通过代码设置补间动画:
/---------------------------------------------------- 华丽的分割线————————————————————————————————/
1. 先在anim下面 或者 drawable下面 建立 frame_xxx.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/ic_launcher"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan1"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan3"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan4"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan5"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan6"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan7"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan8"
android:duration="500"/>
<item
android:drawable="@drawable/ic_sys_scan9"
android:duration="500"/>
</animation-list>
2. 设置imageview的属性:
<ImageView
android:id="@+id/imageView"
android:layout_width="100dip "
android:layout_height="100dip "
android:src="@drawable/frame_xxx"
/>
这里 前面如果是放在anim文件夹下,直接换成
android:src="@anim/frame_xxx"
3.代码中进行使用:
imageView = (ImageView) findViewById(R.id.imageView);
mAnimationDrawable = (AnimationDrawable) imageView.getDrawable();
然后调用
mAnimationDrawable.start()即可
注意: 在oncreate里面直接调用start()是看不见效果的
如果非要 ,则可以:
其次使用View.post(Runnable)的方式:
imageV.post(new Runnable(){
@Override
public void run() {
frameAnim.start();
}
});
实现开始就动画