android 动画 补间动画,渐变动画 Tween Animation

Animation常用属性

duration:动画时间                   

repeatCount:重复次数 infinite无限次

fillAfter:是否停止在最后一帧

repeatMode:重复模式     值:restart重新开始,reserve反复

startOffset:开始延迟时间

 

属性[类型]

功能

Duration[long]

属性为动画持续时间 时间以毫秒为单位

fillAfter [boolean]

当设置为true ,该动画转化在动画结束后被应用

fillBefore[boolean]

当设置为true ,该动画转化在动画开始前被应用

interpolator

指定一个动画的插入器,有一些常见的插入器。

accelerate_decelerate_interpolator:加速-减速 动画插入器;

accelerate_interpolator:加速-动画插入器;

decelerate_interpolator:减速- 动画插入器,其他的属于特定的动画效果

repeatCount[int]

动画的重复次数

repeatMode[String]

定义重复的行为

 1:"restart" 2:"reverse"   eg: android:repeatMode="reverse"

startOffset[long]

动画之间的时间间隔,从上次动画停多少时间开始执行下个动画

zAdjustment[int]

定义动画的Z Order的改变 

0:保持Z Order不变,1:保持在最上层,-1:保持在最下层

补间动画 Tween Animation

只能应用于View对象,只支持部分属性,View animation值改变了View绘制的位置,并没有改变对象本身的真实位置

可以使用XML定义也可以使用代码定义     XML定义的动画放在/res/anim/文件夹内

 

开始动画 通过view的startAnimation(Animationa)  参数定义的动画

 

1,继承dialog

public class IconDrawableDialog extends Dialog {
    private static final int CHANGE_TITLE_WHAT = 1;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';

    private ImageView iv_route;
    private TextView detail_tv;
    private TextView tv_point;
    private RotateAnimation mAnim;

    private Context contenxt;

    //设置点的改变
    private Handler handler = new Handler() {
        private int num = 0;

        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_TITLE_WHAT) {
                StringBuilder builder = new StringBuilder();
                if (num >= MAX_SUFFIX_NUMBER) {
                    num = 0;
                }
                num++;
                for (int i = 0; i < num; i++) {
                    builder.append(SUFFIX);
                }
                tv_point.setText(builder.toString());
                if (isShowing()) {
                    handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT,
                            CHNAGE_TITLE_DELAYMILLIS);
                } else {
                    num = 0;
                }
            }
        }
    };

    public IconDrawableDialog(Context context) {
        super(context, R.style.MyDialog);
        this.contenxt = context;
        //点击imageview外侧区域,动画不会消失
        setCanceledOnTouchOutside(false);
        init();
    }

    private void init() {
        setContentView(R.layout.icon_drawable_dialog_layout);
        iv_route = (ImageView) findViewById(R.id.iv_route);
        detail_tv = (TextView) findViewById(R.id.detail_tv);
        tv_point = (TextView) findViewById(R.id.tv_point);
        initAnim();
        getWindow().setWindowAnimations(R.anim.alpha_in);//设置dialog动画
    }

    private void initAnim() {
        mAnim = new RotateAnimation(0, 360, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim.setDuration(500);
        mAnim.setRepeatCount(Animation.INFINITE);
        mAnim.setRepeatMode(Animation.RESTART);
        mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
    }

    @Override
    public void show() {
    
    // 在要用到的地方调用这个方法
        iv_route.startAnimation(mAnim);
        handler.sendEmptyMessage(CHANGE_TITLE_WHAT);
        super.show();
    }

    @Override
    public void dismiss() {
        mAnim.cancel();
        super.dismiss();
    }

    @Override
    public void setTitle(CharSequence title) {
        if (TextUtils.isEmpty(title)) {
            detail_tv.setText("正在加载");
        } else {
            detail_tv.setText(title);
        }
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getString(titleId));
    }

    public static void dismissDialog(IconDrawableDialog loadingDialog) {
        if (null == loadingDialog) {
            return;
        }
        loadingDialog.dismiss();
    }

}

 

 

2,dialog样式(R.style.MyDialog

<resources>

    <!-- 自定义dialog背景全透明无边框 -->
    <style name="MyDialog" parent="android:style/Theme.Dialog">
        <!-- 背景颜色及和透明程度 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 是否去除标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否去除边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否模糊 -->
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>

 

3,布局文件(R.layout.icon_drawable_dialog_layout)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="100dp"
              android:layout_gravity="center"
              android:paddingRight="30dp"
              android:background="@mipmap/dialog_background"
              android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_route"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@mipmap/dialog"></ImageView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/detail_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:gravity="center"
            android:singleLine="true"
            android:text="正在加载"
            android:textColor="#000000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_point"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/detail_tv"
            android:text="..."
            android:textColor="#000000"
            android:textSize="20sp" />
    </RelativeLayout>

</LinearLayout>

 

4,dialog动画xml(R.anim.alpha_in

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:toDegrees="360" />
</set>

 

 

5,代码设置动画的几种样式

//调用amin资源文件
private void initAnimation() {
    AlphaAnimation alphaAnimation = null;
    //加载XML中的动画XML文件
    alphaAnimation = (AlphaAnimation) AnimationUtils
            .loadAnimation(contenxt, R.anim.anim_alpha);
    //常用属性设置  各种动画通用
    alphaAnimation.setRepeatCount(3);//执行动画效果结束后重复执行3次  一共4次
    alphaAnimation.setRepeatMode(Animation.REVERSE);//重复模式
    alphaAnimation.setFillAfter(true);//动画结束是否停止在最后一帧
    alphaAnimation.setFillBefore(false);//动画结束是否停止在第一帧
    //设置插值器 动画执行速度  变速 加减速。。
    //AccelerateInterpolator减速
    //DecelerateInterpolator加速
    alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
}

//Alpha透明度
private void initAlphaAnimation() {
    //AnimationAlphaAnimation(
    // float fromAlpha,//第一个参数fromAlpha为 动画开始时候透明度
    // float toAlpha)//第二个参数toAlpha为 动画结束时候透明度
    /*说明:
            0.0表示完全透明
            1.0表示完全不透明
    */
    AlphaAnimation myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
    myAnimation_Alpha.setDuration(5000);//设置时间持续时间为 5000毫秒
}

//Scale缩放
private void initScaleAnimation() {
    // ScaleAnimation(
    // float fromX,//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
    // float toX,//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
    // float fromY,//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
    // float toY,//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
    // int pivotXType,//第五个参数pivotXType为动画在X轴相对于物件位置类型
    // float pivotXValue,//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
    // int pivotYType,//第七个参数pivotXType为动画在Y轴相对于物件位置类型
    // float pivotYValue)//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
    /*说明:
            前四种属性值
            0.0表示收缩到没有
            1.0表示正常无伸缩
            值小于1.0表示收缩
            值大于1.0表示放大
    */
    ScaleAnimation myAnimation_Scale = new ScaleAnimation(
            0.0f,
            1.4f,
            0.0f,
            1.4f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f);
    myAnimation_Scale.setDuration(700); //设置时间持续时间为 700毫秒
}

//Translate平移
private void initTranslateAnimation() {
    //TranslateAnimation(
    // float fromXDelta,//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
    // float toXDelta,//第二个参数toXDelta为动画结束时 X坐标上的移动位置
    // float fromYDelta,//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
    // float toYDelta)//第四个参数toYDelta为动画结束时Y坐标上的移动位置
}

//Rotate旋转
private void initRotateAnimation() {
    //RotateAnimation(
    // float fromDegrees,//第一个参数fromDegrees为动画起始时的旋转角度
    // float toDegrees,//第二个参数toDegrees为动画旋转到的角度
    // int pivotXType,//第三个参数pivotXType为动画在X轴相对于物件位置类型
    // float pivotXValue,//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
    // int pivotYType,//第五个参数pivotXType为动画在Y轴相对于物件位置类型
    // float pivotYValue)//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
    RotateAnimation myAnimation_Rotate = new RotateAnimation(
            0.0f,
            +350.0f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f);
}

 

6,amin文件设置动画的几种样式

XML文件中必须有一个根元素,可以是<alpha>、<scale>、<translate>、<rotate>中的任意一个,也可以是<set>来管理一个由前面几个元素组成的动画集合。

<!--缩放动画-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="2000"
       android:fromXScale="0.1"
       android:fromYScale="0.1"
       android:pivotX="0%"
       android:pivotY="0%"
       android:toXScale="1"
       android:toYScale="1">
    <!--
            浮点值 表示倍数 自身几倍
            fromXScale 动画在X轴以自身几倍伸缩开始
            toXScale   动画在X轴以自身几倍伸缩结束
            fromYScale 动画在Y轴以自身几倍伸缩开始
            toYScale   动画在Y轴以自身几倍伸缩结束
            pivotX  动画相对于控件自身的X坐标的开始位置
            pivotY  动画相对于控件自身的Y坐标的开始位置
            0% 0%  表示控件左上角 为00原点坐标
    -->
</scale>

 

<!--平移动画-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="2000"
           android:fromXDelta="-100%p"
           android:fromYDelta="0"
           android:toXDelta="100%p"
           android:toYDelta="0">
    <!--
            fromXDelta x轴起始位置
            toXDelta   X轴结束位置
            fromYDelta y轴起始位置
            toYDelta   y轴结束位置
            100%p 表示相对于父级
            100%相对于自身
    -->
</translate>

 

<!--透明度动画-->
<alpha  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="2000">
    <!--
        fromAlpha 起始透明度 0为完全透明 1为不透明 0~1之间的浮点值
        toAlpha 结束透明度
        duration 动画运行时间 单位毫秒
    -->
</alpha>

 

<!--旋转动画-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360">
    <!--
        interpolator 指定动画的插值器
         accelerate_decelerate_interpolator   加速-减速
         accelerate_interpolator               加速
          decelerate_interpolator               减速
          fromDegrees 动画起始角度
          toDegrees   动画结束旋转的角度 可以大于360度
          负数表示逆时针旋转  正数表示顺时针旋转
          pivotX相对于viewX坐标的开始位置
          pivotY相对于viewY坐标的开始位置
          100 绝对尺寸 100px
          50% 相对尺寸 相对于自身的50%
          50%p 相对尺寸 相对于父容器的50%
          50%为物件的XY方向坐标上的中点位置
          duration  动画播放时间 单位毫秒
    -->
</rotate>

 

7,插值器

Interpolator对象

资源ID

功能作用

AccelerateDecelerateInterpolator

@android:anim/

accelerate_decelerate_interpolator

先加速再减速

AccelerateInterpolator

@android:anim/

accelerate_interpolator

加速

AnticipateInterpolator

@android:anim/

anticipate_interpolator

先回退一小步然后加速前进

AnticipateOvershootInterpolator

@android:anim/

anticipate_overshoot_interpolator

在上一个基础上超出

终点一小步再回到终点

BounceInterpolator

@android:anim/

bounce_interpolator

最后阶段弹球效果

CycleInterpolator

@android:anim/

cycle_interpolator

周期运动

DecelerateInterpolator

@android:anim/

decelerate_interpolator

减速

LinearInterpolator

@android:anim/

linear_interpolator

匀速

OvershootInterpolator

@android:anim/

overshoot_interpolator

快速到达终点并超出

一小步最后回到终点


8,set组合动画效果 

xml方式

AnimationSet animationSet = (AnimationSet) AnimationUtils.
        loadAnimation(this, R.anim.anim_set);
imageView.startAnimation(animationSet);
 
java方式
AnimationSet animationSet1 = new AnimationSet(true);
animationSet1.addAnimation(alphaAnimation);
animationSet1.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet1);
 

9,动画监听器Animation.AnimationListener:

 有时可能我们要在动画的每个周期里面做不同的操作,这时候就要借助动画监听器了

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        //动画开始时调用
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //动画结束时调用
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        //动画重复时调用
    }
});

 

10,效果

猜你喜欢

转载自blog.csdn.net/l331258747/article/details/70856958