自定义控件——视图动画

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43847987/article/details/100522228

android中的动画类型
View Animation视图动画——Tween Animation(补间动画),Frame Animation(逐帧动画)
Property Animation属性动画——ValueAnimator和ObjectAnimator

视图动画
视图动画由5中类型组成:alpha,scale,translate,rotate,set.

scale标签
scale用于缩放动画,可以实现动态调整控件尺寸的效果
该标签具有的属性
android:fromXScale:动画起始时,控件在X轴方向上的相对于自身的缩放比例;
android:toXScale:动画结束时,控件在X轴方向上的相对于自身的缩放比例;
android:fromYScale:动画起始时,控件在Y轴方向上的相对于自身的缩放比例;
android:toYScale:动画结束时,控件在Y轴方向上的相对于自身的缩放比例;
android:picotX:缩放起点的X坐标,即动画的起始点与坐标原点的相对位置;
android:picotY:缩放起点的Y坐标;

scale标签的使用:在res下的anim问价下创建xml文件
如下:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
		android:fromXScale="1.0"
		android:toXScale="0.3"
		android:fromXScale="1.0"
		android:toXScale="0.3"
		android:duration="500"/>

创建完成后,再调用动画时我们要以R.anim.XXX来调用这个动画。

Animation的继承属性
所有的动画都继承自Animation类
Animation所具有的属性的含义
android:duration用于设置完成一次的动画的持续时间,以毫秒为单位
android:fillAfter为true时,则控件动画结束时,将保持动画结束时的状态
android:fillBefore为true时,则空间动画结束时,将还原到初始状态
android:fillEnabled与android:fillBefore的效果相同
android:repeatCount用于指定动画的重复次数,取值为infinite时,表示无限循环
android:repeatMode用于设定重复的类型,由reverse和restart两个值。reverse表示倒序回放,restart表示重放,必须与repeatCount一起使用才可以看到效果
android:interpolator用于设定插值器,其实就是指定的动画效果

ScaleAnimation类对应的就是scale标签,ScaleAnimation不像Animation一样,他所有的属性都是通过ScaleAnimation类的构造函数来实现的。

ScaleAnimation(Context context , AttributeSet attrs) //用于从本地xml文件中加载动画
ScaleAnimation(float fromX , float toX, float fromY , float toY) 
ScaleAnimation(float fromX , float toX, float fromY , float toY , float pivotX, 
float votY)
ScaleAnimation(float fromX , float toX, float fromY , float toY, intpivotXType, 
float pivotXVa l ue , int pivotYType , float votYValue)

alpha标签
alpha标签用于实现渐变透明度动画效果,其具由Animation所有的属性除此之外,其自身的属性有:
android:fromAplha动画开始时的透明度
android:toAplha动画结束时的透明度

AlphaAnimation类对应alpha标签
构造函数如下:


AlphaAn mat on(Context context , AttributeSet attrs) 
AlphaAnimat on(float fromAlpha , float toAlpha)

totate标签
rotate用于实现动画的旋转效果,自身所拥有属性:
android:fromDegrees动画开始旋转时的角度位置
android;toDegrees动画结束时旋转到的角度位置
android:pivotX 旋转中心点X轴坐标,默认中心点为坐标原点
android:pivotY 旋转中心点Y轴坐标

RotateAnimation类对应rotate标签,与ScaleAnimation类相似。

transtate标签
transtate实现画面的平移变换,属性
android:fromXDelta起始点的X坐标
android:fromYDelta起始点的Y坐标
android:toXDelta终点的X坐标
android:toYDelta终点的Y坐标
TranstateAnimation的构造方法:

Tr anslateAnimation(Context context, Attr buteSet attrs) 
TranslateAn i mation (float fromXDelta , at toXDelta , float fromY Delt a , float 
toYDelta) 
TranslateAni mation(int fromXType , float fromXValue , int toXType , float 
toXValue , int fromYType , float fromYValue , int toYType , float toYValue )

set标签
set标签是一个容器标签,用于定义动画集。set标签可以将这些动画效果组合起来,共同完成一个动画。

AnimtionSet类对应set标签


AnimationSet (Context context , AttributeSet attrs) 
AnimationSet(boolean shareinterpolator)

shareInterpolator参数取值有两个,true和false,当取值为true时,用于在AnimationSet中设置一个插值器。
增加动画的函数为
public void addAnimation(Aniamation a)


TextView tv = TextView)find liewByid (R . id. tv) ; 
An mation alpha Anim = new AlphaAnimation (1. Of, 0 . lf); 
An mation scale Anim =new ScaleAnimation(O.Of, 1.4f, O.Of, 1.4f, Animation. 
RELATIVE TO SELF , 0 . Sf, Animation . RELATIVE TO SELF , 0 . Sf) ; 
Animation rotate Anim = new RotateAnimation (0 , 720 , Animation . RELATIVE TO SELF , 
O.Sf, Animation . RELATIVE TO SELF , O. Sf) ; 
AnimationSet setAnim=new AnimationSet(true) ; 
setAnim addAn mation(alpha Anim) ; 
setAnim . addAnimation(scale Anim); 
setAnim . addAnimation(rotate Anim) ; 
setAnim.setDurat on(3000)
setAnim . setFillAfter(true) ; 
tv . startAnimation(AnimationSet) ;

AnimationSet的使用就是先创建各种动画,然后利用AnimationSet.addAnimation(anim)方法添加进去。

Animation类
Animation的一些函数
void cancel()//取消动画
void reset()将控件重置到动画开始前状态
void setAnimationListener(Animation.AnimationListener listener)//设置动画监听
Animation.AnimationListener的回调函数如下:

abstract 1oid onAnimationEnd(Animation animation) //动画结束时,回调
abstract void onAnimat onRepeat(Animation animation) //动画重复时,回调
abstract 10id onAnimationStart (Animation animation)//动画开始时,回调

插值器
插值器的使用:
在xml文件中:

<alpha xmlns : android= ” http : //schemas . android.com/apk/res/android" 
android fromAlpha :1".
android : toAlpha="0 . 1 " 
android : duration=" 3000 "
android:fillBefore= " true " 
android:interpolator="@android : anim/accelerate_interpolator" >
</alpha>

在Java代码中实现:

TextView tv = Text Tiew) findViewByid (R . id. tv) ; 
AlphaAn mation alphaAnim =new AlphaAnimation(l.Of, 0 . lf); 
alphaAnim . setDuration(3000) ; 
alphaAnim .setFillBefore(true) ; 
alphaAnim . setinterpolator (new Linearinterpolator ()); 
tv . startAnimation(alphaAnim);

系统提供的插值器:
AccelerateDeceleratelnterpolator—— 加速减速插值器,在开始和结束时速度变化慢,中间时快。
AccelerateInterpolator——加速插值器,速度逐渐加快
DecelerateInterpolator——减速插值器,适度逐渐变慢
LinearInterpolator——线性插值器,保持匀速
BounceInterpolator——弹跳插值器,模拟自由落地回弹效果
Anticipatelnterpolator ——初始偏移插值器,在动画开始的时候向前偏移一段距离,然后应用于动画。
Overshootlnterpolator ——结束偏移插值器,在动画结束时,沿动画方向继续运动
一段距离后再结束动画。
AnticipateOvershootlnterpolator是Anticipatelnterpolator和Overshootlnterpolator的结合体。
Cyclelntorpolator ——循环插值器,表示动画循环播放特定的次数,速 沿正弦曲线改变。

逐帧动画
逐帧动画就是一帧挨着一帧的播放图片。逐帧动画既可以通过XML实现也可以通过java实现。

猜你喜欢

转载自blog.csdn.net/weixin_43847987/article/details/100522228