Fork me on GitHub

安卓第十八天笔记--简单动画


title:动画

动画

1.补间动画

  1. Animation.ABSOLUTE:用于指定后面的数值是绝对值的像素。
  2. Animation.RELATIVETOSELF: 用于指定后面的数值是自己宽高的倍数
  3. Animation.RELATIVETOPARENT : 用于指定后面的数值是自己的父控件宽高的倍数
  4. Animation.RESTART播放重新回到原点
  5. Animation.REVERSE播放回接着,反向播放

2.平移

fromXType  水平播放的类型
fromXValue 从哪开始,0表示现在的位置
toXType,   到什么位置的类型
toXValue 到什么坐标
fromYType 垂直播放的类型
fromYValue 从哪开始,0表示现在的位置
toYType 到什么位置的类型
toYValue 到什么坐标
TranslateAnimation translate = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, -2, 
            Animation.RELATIVE_TO_SELF, 2, 
            Animation.RELATIVE_TO_SELF, 0,
            Animation.RELATIVE_TO_SELF, 1);
    //设置显示时间
    translate.setDuration(3000);
    //播放资源为无限循环
    translate.setRepeatCount(Animation.INFINITE);
    //播放模式,反转播放,先顺着播完,就反着播放
    translate.setRepeatMode(Animation.REVERSE);
    //哪个组件在播放,设置
    imageView.setAnimation(translate);
    //开始播放
    translate.start();

 

3.旋转

/*
     * fromDegrees, 从什么角度开始 0 从现在的 toDegrees, 270度 pivotXType, X中心点类型
     * Animation.RELATIVE_TO_SELF自身 pivotXValue, 中心点值 0.5表示这个图片的一半置
     * pivotYType, Y中心点类型Animation.RELATIVE_TO_SELF自身
     *  pivotYValue 0.5f 
     */
    RotateAnimation rotate = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            3);
    //播放显示时间
    rotate.setDuration(5000);
    rotate.setRepeatCount(Animation.INFINITE);
    rotate.setRepeatMode(Animation.RESTART);

    imageView.setAnimation(rotate);
    rotate.start();

 

4.缩放

    /*
     * fromX, toX, 从坐标什么地址开始到什么坐标,0,表示当前 fromY, toY, 
     * pivotXType,  旋转的中心点
     * pivotXValue, 类型, pivotYType, pivotYValue
     */
    ScaleAnimation scale = new ScaleAnimation(1, 2, 1, -2,
            Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 3);

    // 播放显示时间
    scale.setDuration(5000);
    scale.setRepeatCount(Animation.INFINITE);
    scale.setRepeatMode(Animation.RESTART);

    imageView.setAnimation(scale);
    scale.start();

 

5.透明度

    /*
     * fromAlpha,  从什么透明度开始
     * toAlpha 到什么透明度结束
     */
    AlphaAnimation alpha = new AlphaAnimation(1, 0);

    // 播放显示时间
    alpha.setDuration(2000);
    alpha.setRepeatCount(Animation.INFINITE);
    alpha.setRepeatMode(Animation.REVERSE);

            imageView.setAnimation(alpha);
            alpha.start();
}

 

6.集合

就是建立一个
AnimationSet set = new AnimationSet();
set.addAnimation(scale);
    set.addAnimation(rotate);
    set.addAnimation(translate);
    set.start();

 


就可以播放多个动画

7. XML配置

在res/目录下建立anim文件目录
 <!-- 
        如果平移的数值是数字,那么表示的是绝对值
        如果是百分比,那么表明是自己的宽高的倍数
        如果是百分比加上 p,那么表明是自己的父元素的宽高的倍数
         -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate 
    android:fromXDelta="200%"
    android:toXDelta="200%"
    android:fromYDelta="-100%"
    android:toYDelta="200%"
    android:duration="4000"
    android:repeatCount="infinite"
    android:repeatMode="reverse"/>

    <rotate
       android:fromDegrees="0"
       android:toDegrees="360"
       android:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="4000" 
       android:repeatCount="infinite"
       android:repeatMode="reverse"/>

</set>

 

加载使用AnimationUtils.load方法加载

Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_set);

    imageView.startAnimation(animation);

 

8.属性动画

一般都用代码控制

/**
 * 水平
 * 
 * @param v
 */
public void translate(View v) {
    /**
     * 参数一: 谁去播放这个动画 参数二: 属性名字 想要改变x方向的移动 参数三:动画执行的数值
     */
    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,
            "translationX", -50, 50);
    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,
            "translationY", 0, 60);

    objectAnimatrX.setDuration(2000);

    objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);

    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);

    objectAnimatrX.start();




}

/**
 * 转换
 * 
 * @param v
 */
public void rotate(View v) {
    ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView, "rotationX", 0,360);
    ObjectAnimator rotateY = ObjectAnimator.ofFloat(imageView, "rotationY", 0,360);
    rotateX.setDuration(3000);
    rotateX.setRepeatMode(Animation.REVERSE);
    //rotateX.setRepeatCount(Animation.INFINITE);
    rotateY.setDuration(3000);
    rotateY.setRepeatMode(Animation.REVERSE);
    //rotateY.setRepeatCount(Animation.INFINITE);

    AnimatorSet set  = new AnimatorSet();
    set.playSequentially(rotateX,rotateY);
    set.start();
}

/**
 * 绽放
 * 
 * @param v
 */
public void scale(View v) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2.0f,5f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2.0f,5f);
    scaleX.setDuration(3000);
    scaleX.setRepeatMode(Animation.REVERSE);
    scaleX.setRepeatCount(Animation.INFINITE);
    scaleY.setDuration(3000);
    scaleY.setRepeatMode(Animation.REVERSE);
    scaleY.setRepeatCount(Animation.INFINITE);

    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX,scaleY);
    set.start();


}

/**
 * 透明
 * 
 * @param v
 */
public void alpha(View v) {

    ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f,0f);
    alpha.setDuration(3000);
    alpha.setRepeatMode(Animation.REVERSE);
    alpha.setRepeatCount(Animation.INFINITE);

    alpha.start();
}

/**
 * 集合
 * 
 * @param v
 */
public void set(View v) {
    /**
     * 参数一: 谁去播放这个动画 参数二: 属性名字 想要改变x方向的移动 参数三:动画执行的数值
     */
    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,
            "translationX", -50, 100);
    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,
            "translationY", -70, 100);

    objectAnimatrX.setDuration(2000);

    //objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);

    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);
    objectAnimatrY.setDuration(2000);

    //objectAnimatrY.setRepeatCount(ObjectAnimator.INFINITE);
    objectAnimatrY.setRepeatMode(ObjectAnimator.REVERSE);

    AnimatorSet set = new AnimatorSet();

    //set.playTogether(objectAnimatrX,objectAnimatrY);
    set.playSequentially(objectAnimatrX,objectAnimatrY);
    set.start();


}

 

如果需要 用XML控制可以在res\下建立 animator,即可, 在这个目录建立XML文件如果 下方法导入

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);
set.start();

 

posted @ 2016-03-21 17:13  森林森  阅读(555)  评论(0编辑  收藏  举报