动画

View动画

2019-04-23  本文已影响0人  FlyClound

一.效果图

image

二.使用 xml 文件实现方式

通用属性:

  1. 渐变

    自身属性:

示例:

anim 文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>

    <alpha android:fromAlpha="1.0"
           android:toAlpha="0.0"
           android:duration="2000"/>
</set>

调用:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_alpha);
        animation.setFillAfter(true);//设置保持动画后的状态
        mIvIcon.startAnimation(animation);
  1. 平移
    自身属性:

示例:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate android:fromXDelta="0"
               android:toXDelta="100%"
               android:fromYDelta="0"
               android:toYDelta="100%"
               android:duration="2000"
    />
</set>
  1. 缩放
    自身属性:

示例:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000">

    <scale android:fromXScale="1.0"
    android:toXScale="0.5"
    android:fromYScale="1.0"
    android:toYScale="0.5"
           android:pivotX="100%p"
           android:pivotY="100%p"/>

    <!--默认缩放中心在左上角-->
</set>
  1. 旋转

    自身属性:

示例:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:pivotX="50%"
            android:pivotY="50%"
            android:fromDegrees="0"
            android:toDegrees="180"
    android:duration="2000"/>
</set>
  1. 动画集合

    示例:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true">


    <alpha android:fromAlpha="1"
           android:toAlpha="0.5"/>

    <rotate android:fromDegrees="0"
            android:toDegrees="180"
            android:pivotX="50%"
            android:pivotY="50%"
    />
</set>

三.使用代码实现

  1. 渐变
//声明一个渐变动画, 参数1代表起始透明度,参数2代表结束透明度  //1代表完全不透明,0是完全透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
        //设置持续时间
        alphaAnimation.setDuration(2000);
        //设置动画后的状态
        alphaAnimation.setFillAfter(true);
        //设置重复次数
        alphaAnimation.setRepeatCount(2);
        //设置重复模式
        alphaAnimation.setRepeatMode(Animation.REVERSE);
         //设置动画监听
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mIvIcon.startAnimation(alphaAnimation);
  1. 平移
//      (int fromXType,    表示X的起始值类型,该类型指定相对长度的类型
//      Animation.RELATIVE_TO_PARENT   相对于父亲的长度
//      Animation.RELATIVE_TO_SELF     相对于自身的长度
//      Animation.ABSOLUTE             表示绝对类型,传入长度的绝对值
        
//      float fromXValue,  表示的是动画开始的时候,X的起始位置  (值由类型确定)
        
//      
//      int toXType, 表示X的结束值类型,该类型指定相对长度的类型
//      float toXValue,表示的是动画结束的时候,X的结束位置  (值由类型确定)
//      
//       y的起始类型,       y的起始值                              y的结束类型,       y的结束值  
//      int fromYType, float fromYValue, int toYType, float toYValue

TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f
                , Animation.RELATIVE_TO_PARENT, 1f,
                Animation.RELATIVE_TO_PARENT, 0f,
                Animation.RELATIVE_TO_PARENT, 1f);
        translateAnimation.setDuration(2000);
        translateAnimation.setFillAfter(true);
        mIvIcon.startAnimation(translateAnimation);
  1. 缩放
//      float fromX, 起始X的大小,  值为自身长度的倍数
//      
//      float toX, 结束X的大小,  值为自身长度的倍数
        
//      float fromY,
//      float toY,
//      int pivotXType,  缩放中心点的X位置的类型, 相对长度类型
//      float pivotXValue,   圆心的X的值 (值由类型确定) ,相对长度类型
//      int pivotYType, float pivotYValue   
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setDuration(2000);
        mIvIcon.startAnimation(scaleAnimation);
  1. 旋转
//申明一个旋转动画    参数1:起始角度    参数2:旋转的度数               起始值小于结束值 顺时针,  大于 逆时针
//      RotateAnimation rotateAnimation = new  RotateAnimation(-180, -90);
//      RotateAnimation rotateAnimation = new RotateAnimation(0, 180, 150, 0);
//      float fromDegrees,起始角度  
//      float toDegrees, 结束角度               起始值小于结束值 顺时针旋转,  大于 逆时针旋转
//      int pivotXType, 表示圆心X位置的相对类型, 相对长度类型
//      
//      float pivotXValue, 圆心的X的值 (值由类型确定)
//      
//      int pivotYType, float pivotYValue
      //设置旋转中心在图片的中心
        RotateAnimation rotateAnimation = new RotateAnimation(0, 180
                ,Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(2000);
        rotateAnimation.setFillAfter(true);
        mIvIcon.startAnimation(rotateAnimation);
  1. 集合
//先声明一个动画集合
    AnimationSet animationSet = new AnimationSet(true);
    //声明需要的动画
        RotateAnimation rotateAnimation = new RotateAnimation(0, 180
                ,Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);

        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
        //将每个动画添加进动画集合
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(3000);
        animationSet.setFillAfter(true);
        mIvIcon.startAnimation(animationSet);
上一篇 下一篇

猜你喜欢

热点阅读