欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 视图动画(视图动画代码实现)

程序员文章站 2022-02-05 19:29:29
...

概述
这些标签,都对应一个类,但他们都是派生自Animation类。

scale ScaleAnimation
alpha AlphaAnimation
rotate RotateAnimation
translate TranslateAnimation
set AetAnimation

ScaleAnimation

ScaleAnimation(Context context, AttributeSet attrs)
 ScaleAnimation(float fromX, float toX, float fromY, float toY) 
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
ScaleAnimation(
float fromX, //初始x轴的缩放
float toX,    //目标缩放
float fromY, //初始y轴缩放
float toY,    //目标y轴缩放
int pivotXType, float pivotXValue,  //以原坐标为原点,x轴的起始点
int pivotYType, float pivotYValue) //以原坐标为原点,y轴的起始点
pivotXType的类型有:
RELATIVE_TO_SELF  百分比,相对自身百分比 50%
RELATIVE_TO_PARENT 百分比,相对父控件百分比 50%p
ABSOLUTE 具体数值 比如 50

AlphaAnimation

AlphaAnimation(Context context, AttributeSet attrs)
AlphaAnimation(float fromAlpha, float toAlpha)
AlphaAnimation(
float fromAlpha,  //初始透明度
float toAlpha)    //目标透明度

RotateAnimation

RotateAnimation(Context context, AttributeSet attrs) 
RotateAnimation(float fromDegrees, float toDegrees)
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) 
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
RotateAnimation(
float fromDegrees,  //初始角度
float toDegrees,      //目标角度
int pivotXType, float pivotXValue,        
int pivotYType, float pivotYValue)

TranslateAnimation

TranslateAnimation(Context context, AttributeSet attrs) 
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
TranslateAnimation(
int fromXType, float fromXValue,  //起始x轴坐标
int toXType, float toXValue,           //目标x轴坐标
int fromYType, float fromYValue,    //起始y轴坐标
int toYType, float toYValue)            //目标y轴坐标

AnimationSet

AnimationSet(Context context, AttributeSet attrs) 
AnimationSet(boolean shareInterpolator) //true 共用一个插值器,false 各自定义插值器
都是继承自Animation,有这些共同属性
animationSet1.setDuration(3000); //动画时长 
animationSet1.scaleCurrentDuration(4.0f); //当前动画时间 
animationSet1.setFillAfter(true); //true动画结束,保持控件结束时的状态 
animationSet1.setFillBefore(true); //true动画结束,保持控件最初始状态 animationSet1.setRepeatMode(Animation.RESTART); //循环模式 
animationSet1.setStartOffset(1000); //在什么时间停止动画 
animationSet1.setStartTime(500); //在什么时间开始动画 
animationSet1.setRepeatCount(5); //循环次数 
animationSet1.setFillEnabled(true); //true动画结束,保持控件结束时的状态
装载所有动画效果
ScaleAnimation scaleAnimation1 = new ScaleAnimation( 
0.0f, 
1.4f, 
0.0f, 
1.4f, 
Animation.RELATIVE_TO_SELF, 0.5f, 
Animation.RELATIVE_TO_SELF, 0.5f); 
AlphaAnimation alphaAnimation1=new AlphaAnimation(0.1f,0.5f); 
RotateAnimation rotateAnimation1=new RotateAnimation(0.1f,0.1f); 
TranslateAnimation translateAnimation1=new TranslateAnimation(
 Animation.ABSOLUTE,
 50, 
Animation.ABSOLUTE,
 50); 
AnimationSet animationSet1=new AnimationSet(true); //装载 
animationSet1.addAnimation(scaleAnimation1); 
animationSet1.addAnimation(alphaAnimation1); 
animationSet1.addAnimation(rotateAnimation1); 
animationSet1.addAnimation(translateAnimation1); 
animationSet1.setDuration(3000); 
animationSet1.scaleCurrentDuration(4.0f); 
animationSet1.setFillAfter(true); 
animationSet1.setFillBefore(true); 
animationSet1.setRepeatMode(Animation.RESTART); 
animationSet1.setStartOffset(1000); 
animationSet1.setStartTime(500); 
animationSet1.setRepeatCount(5); 
animationSet1.setFillEnabled(true); 

Animation

animationSet1.cancel(); //取消动画 
animationSet1.reset(); //将控件重置到初始化状态 
animationSet1.setAnimationListener(AnimationListener listener); //设置监听
监听,利用监听可以实现动画的连续效果,比如,先实现缩放,再实现位移
animationSet1.setAnimationListener(new Animation.AnimationListener() { 
@Override 
public void onAnimationStart(Animation animation) { 
//动画开始 
} 
@Override 
public void onAnimationEnd(Animation animation) { 
//动画结束 
} 
@Override 
public void onAnimationRepeat(Animation animation) { 
//动画结束 
} 
});
相关标签: animation

上一篇:

下一篇: 动画animation