动画
帧动画
直接可以参考这个博客
http://blog.csdn.net/aminfo/article/details/7847761
补间动画(平移,旋转,缩放,透明度动画)
先来个布局吧
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:onClick="translate"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="平移"
/>
<Button
android:onClick="rotate"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="旋转"
/>
<Button
android:onClick="scale"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="缩放"
/>
<Button
android:onClick="alpha"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="透明"
/>
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
1.平移动画
public void translate(View v){
//平移动画
/*TranslateAnimation animation = new TranslateAnimation(
0, 100, //x方向移动
0, 0); //y方向移动
*/
/**
/这里是三种type类型
*Animation.ABSOLUTE : 后面指定的数值,代表的是像素点
*Animation.RELATIVE_TO_SELF : 后面的数值,代表的是自己的宽度或者高度的倍数
* Animation.RELATIVE_TO_PARENT : 后面的数值,代表的是控件的父元素宽度或者高度的倍数
* */
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.5f);
//指定动画播放多少时间
animation.setDuration(2000);
//设置播放的次数
animation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
animation.setRepeatMode(Animation.REVERSE);
//让控件播放这个动画
iv.startAnimation(animation);
}
2.旋转动画
public void rotate(View v){
/**
* 三种Type
* Animation.ABSOLUTE : 后面指定的数值,代表的是像素点
* Animation.RELATIVE_TO_SELF : 后面的数值,代表的是自己的宽度或者高度的倍数
* Animation.RELATIVE_TO_PARENT : 后面的数值,代表的是控件的父元素宽度或者高度的倍数
*
*/
RotateAnimation animation = new RotateAnimation(
0, 360, //从什么角度旋转到什么角度
Animation.RELATIVE_TO_SELF , 0.5f,
Animation.RELATIVE_TO_SELF , 0.5f);
//指定动画播放多少时间
animation.setDuration(2000);
//设置播放的次数
animation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
animation.setRepeatMode(Animation.REVERSE);
//让控件播放这个动画
iv.startAnimation(animation);
}
3.缩放动画
public void scale(View v){
/**
*Animation.ABSOLUTE : 后面指定的数值,代表的是像素点
*Animation.RELATIVE_TO_SELF : 后面的数值,代表的是自己的宽 度或者高度的倍数
* Animation.RELATIVE_TO_PARENT : 后面的数值,代表的是控件的父元素宽度或者高度的倍数
*
*/
ScaleAnimation animation = new ScaleAnimation(
1, -1, //x 方向缩放从什么倍数到什么倍数
1, 1, //y方向缩放从什么倍数到什么倍数
Animation.RELATIVE_TO_SELF , 0.5f, //缩放的中心点
Animation.RELATIVE_TO_SELF , 0.5f);
//指定动画播放多少时间
animation.setDuration(2000);
//设置播放的次数
animation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
animation.setRepeatMode(Animation.REVERSE);
//让控件播放这个动画
iv.startAnimation(animation);
}
4.透明度动画
public void alpha(View v){
/**
* Animation.ABSOLUTE : 后面指定的数值,代表的是像素点
* Animation.RELATIVE_TO_SELF : 后面的数值,代表的是自己的宽度或者高度的倍数
* Animation.RELATIVE_TO_PARENT : 后面的数值,代表的是控件的父元素宽度或者高度的倍数
*
*/
AlphaAnimation animation = new AlphaAnimation(0.2f,1);
//指定动画播放多少时间
animation.setDuration(2000);
//设置播放的次数
animation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
animation.setRepeatMode(Animation.REVERSE);
//让控件播放这个动画
iv.startAnimation(animation);
}
5.如何通过xml文件去生成动画(这里就以平移动画为列)
1.在res文件下新建anim文件夹存放动画xml文件
2.平移动画代码,其他都是类似的,这里就不一一去写了.
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
//1.这里你如果直接写具体数值,则相当于移动的具体数值像素
//2.如果写成 android:toXDelta="-50%",则说明相对于自己X移动自己的一半
//3.如果写成 android:toXDelta="-50%p" ,则说明相对于父容器距离的一半
android:toXDelta="-50%p"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse">
<!--
如果给的是数字: 移动的是绝对值的像素
如果给的是百分比(100%) 代表的是移动自己的宽度或者高度的倍数 、、
如果是百分比后面跟上个p 100%p 代表的是移动父元素的宽度或者高度倍数
-->
</translate>
3.xml文件写完,那么我们应该如何让他在代码中加载呢?我就直接贴代码了
public void translate(View v){
//1. 导入动画
Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate_demo);
//2. 播放动画
iv.startAnimation(anim);
}
6.基本的动画说完了,那么我们来说说动画的集合吧
1.首先你需要把基本的动画创建出来
AlphaAnimation animation = new AlphaAnimation(0.2f,1);
//指定动画播放多少时间
animation.setDuration(2000);
//设置播放的次数
animation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
animation.setRepeatMode(Animation.REVERSE);
//--------------------------
ScaleAnimation sanimation = new ScaleAnimation(
1, 3, //x 方向缩放从什么倍数到什么倍数
1, 3, //y方向缩放从什么倍数到什么倍数
Animation.RELATIVE_TO_SELF , 0.5f, //缩放的中心点
Animation.RELATIVE_TO_SELF , 0.5f);
//指定动画播放多少时间
sanimation.setDuration(2000);
//设置播放的次数
sanimation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
sanimation.setRepeatMode(Animation.REVERSE);
//--------------------------
RotateAnimation ranimation = new RotateAnimation(
0, 360, //从什么角度旋转到什么角度
Animation.RELATIVE_TO_SELF , 0.5f,
Animation.RELATIVE_TO_SELF , 0.5f);
//指定动画播放多少时间
ranimation.setDuration(2000);
//设置播放的次数
ranimation.setRepeatCount(Animation.INFINITE);
//设置播放的模式
ranimation.setRepeatMode(Animation.REVERSE);
2.创建动画集合
//shareInterpolator 是否共享插入器 ,
// true , 集合中的子动画都共用集合的插入器,
// false : 代表的是每个子动画使用自己的插入器
AnimationSet set = new AnimationSet(false);
//给集合加入3个动画
set.addAnimation(animation);
set.addAnimation(sanimation);
set.addAnimation(ranimation);
3.接下来你只需要给你需要设置动画的控件设置动画就ok了
//让控件播放集合中的动画,我这里的iv图片
iv.startAnimation(set);
7.那我们来说说补间动画的特点吧
补间动画动画只是我们看着动画在动,其实控件还是在原来的位置,就像那句话说的”树在动,不是你心在动”
属性动画
//平移
public void translate(View v) {
/**
* 参数一: 谁取播放这个动画 参数二:属性名字: 实现什么动画 参数三:动画的数值 , 0 - 1 -2 -3 -4 -5
*/
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", 0,20,100);
// 指定动画播放多少时间
animator.setDuration(2000);
// 设置播放的次数
animator.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animator.setRepeatMode(Animation.REVERSE);
animator.start();
}
--------------------华丽的分割线--------------------------------
public void rotate(View v) {
// iv.setRotation()
/**
* 参数一: 谁取播放这个动画 参数二:属性名字: 实现什么动画 参数三:动画的数值 , 0 - 1 -2 -3 -4 -5
*/
//旋转动画
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "rotationY", 0,360);
// 指定动画播放多少时间
animator.setDuration(2000);
// 设置播放的次数
animator.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animator.setRepeatMode(Animation.REVERSE);
animator.start();
}
--------------------华丽的分割线--------------------------------
//缩放动画
public void scale(View v) {
/**
* 参数一: 谁取播放这个动画 参数二:属性名字: 实现什么动画 参数三:动画的数值 , 0 - 1 -2 -3 -4 -5
*/
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "scaleX", 1,3);
// 指定动画播放多少时间
animator.setDuration(2000);
// 设置播放的次数
animator.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animator.setRepeatMode(Animation.REVERSE);
animator.start();
}
--------------------华丽的分割线--------------------------------
//透明度动画
public void alpha(View v) {
/**
* 参数一: 谁取播放这个动画 参数二:属性名字: 实现什么动画 参数三:动画的数值 , 0 - 1 -2 -3 -4 -5
*/
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 0.2f,1.0f);
// 指定动画播放多少时间
animator.setDuration(2000);
// 设置播放的次数
animator.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animator.setRepeatMode(Animation.REVERSE);
animator.start();
}
--------------------华丽的分割线--------------------------------
//动画集合
public void set(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", 0,100);
// 指定动画播放多少时间
animator.setDuration(2000);
/*// 设置播放的次数
animator.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animator.setRepeatMode(Animation.REVERSE);*/
ObjectAnimator animatorY = ObjectAnimator.ofFloat(iv, "translationY", 0,100);
// 指定动画播放多少时间
animatorY.setDuration(2000);
/* // 设置播放的次数
animatorY.setRepeatCount(Animation.INFINITE);
// 设置播放的模式
animatorY.setRepeatMode(Animation.REVERSE);*/
//1. 定义一个集合
AnimatorSet set = new AnimatorSet();
//2. 往集合中添加动画
// set.playTogether(animator , animatorY);
set.playSequentially(animatorY , animator );
//3. 让集合中的动画播放起来
set.start();
}
在xml文件中定义属性动画
1.在res中新建animator文件夹
//xml文件名字translate_demo
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="200"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse">
</objectAnimator>
2.代码中加载xml属性动画
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.translate_demo);
animator.setTarget(iv);
animator.start();