Android的帧动画、补间动画、属性动画

xiaoxiao2021-02-27  536

Animation:

ScaleAnimation:

android:fromXScale 起始的 X 方向上相对自身的缩放比例,浮点值,比如 1.0 代表自身无变化,0.5 代表起始时缩小一倍,2.0 代表放大一倍; android:toXScale 结尾的 X 方向上相对自身的缩放比例,浮点值; android:fromYScale 起始的 Y 方向上相对自身的缩放比例,浮点值, android:toYScale 结尾的 Y 方向上相对自身的缩放比例,浮点值; android:pivotX 缩放起点 X 轴坐标,可以是数值、百分数、百分数 p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前 View 的左上角,即原点处加上 50px,做为起始缩放点;如果是 50%,表示在当前控件的左上角加上自己宽度的 50%做为起始点;如果是 50%p,那么就是表示在当前的左上角加上父控件宽度的 50%做为起始点 x 轴坐标。(具体意义,后面会举例演示) android:pivotY 缩放起点 Y 轴坐标,取值及意义跟 android:pivotX 一样。 放到代码中,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 pivotY) ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

第一个构造函数是从本地XML 文件加载动画,基本用不到的,我们主要看下面三个构造函数。 在标签属性 android:pivotX 中有三种取值,数,百分数,百分数 p;体现在构造函数中,就是最后一个构造函数的 pivotXType,它的取值有三个,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;

 

AlphaAnimation:

android:fromAlpha 动画开始的透明度,从 0.0 --1.0 ,0.0 表示全透明,1.0 表示完全不透明 android:toAlpha 动画结束时的透明度,也是从 0.0 --1.0 ,0.0 表示全透明,1.0 表示完全不透明 所对应的构造函数为: AlphaAnimation(Context context, AttributeSet attrs) 同样,从本地 XML 加载动画,基本不用 AlphaAnimation(float fromAlpha, float toAlpha) 这里只剩最后一个构造函数,难度不大,下面举个例子说明下用法。 在第一篇文章中,我们构造的 XML 代码为:

 

 

RotateAnimation

android:fromDegrees 开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数 android:toDegrees 结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数 android:pivotX 缩放起点 X 轴坐标,可以是数值、百分数、百分数 p 三种样式,比如 50、50%、50%p,具体意义已在 scale 标签中讲述,这里就不再重讲 android:pivotY 缩放起点 Y 轴坐标,可以是数值、百分数、百分数 p 三种样式,比如 50、50%、50%p 对应的构造函数有: RotateAnimation(Context context, AttributeSet attrs)  从本地 XML 文档加载动画,同样,基本不用 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 跟 ScaleAnimation 差不多,关键问题同样是 pivotXType 和 pivotYType 的选择,同样有三个取值:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;

TranslateAnimation

android:fromXDelta 起始点 X 轴坐标,可以是数值、百分数、百分数 p 三种样式,比如 50、50%、50%p,具体意义已在 scale 标签中讲述,这里就不再重讲 android:fromYDelta 起始点 Y 轴从标,可以是数值、百分数、百分数 p 三种样式; android:toXDelta 结束点 X 轴坐标 android:toYDelta 结束点 Y 轴坐标 这些属性所对应的构造函数为: 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) 由于 fromXDelta、fromYDelta、toXDelta、toYDelta 这三个属性都具有三种状态,所以在构造函数中,最理想的状态就是第三个构造函数,能够指定每个值的类型,第二个构造函数:TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)使用是绝对数值。只有最后一个构造函数可以指定百分数和相对父控件的百分数。

 

AnimationSet:

AnimationSet(Context context, AttributeSet attrs) 同样,基本不用

·       AnimationSet(booleanshareInterpolator) shareInterpolator 取值 true false,取 true时,指在 AnimationSet 中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。增加动画的函数为:(更多函数,请参看 SDK 文档)

public void addAnimation (Animation a) 下面在第一篇中的 XML 代码为例写出能构造同样效果的 JAVA 代码:

 

public void together(View view){ TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.5f); ta.setDuration(2000); ScaleAnimation sa = new ScaleAnimation(1.5f,3.0f,1.0f,2.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); sa.setDuration(3000); RotateAnimation ra = new RotateAnimation(90.0f,150.0f,Animation.RELATIVE_TO_SELF,0.3f,Animation.RELATIVE_TO_SELF,0.5f); ra.setDuration(2000); final AlphaAnimation aa = new AlphaAnimation(0.3f,0.6f); aa.setDuration(2000); AnimationSet as = new AnimationSet(true); as.addAnimation(ta); // as.addAnimation(sa); // as.addAnimation(ra); // as.addAnimation(aa); img.startAnimation(as); as.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(AllActivity.this, "愿您在使用时有个好心情", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(AllActivity.this, ":-(", Toast.LENGTH_SHORT).show(); btn_together.setText("break"); } @Override public void onAnimationRepeat(Animation animation) { Toast.makeText(AllActivity.this, "开启哲学之旅", Toast.LENGTH_SHORT).show(); } });

ObjectAnimator

第一个参数用于指定这个动画要操作的是哪个控件 第二个参数用于指定这个动画要操作这个控件的哪个属性 第三个参数是可变长参数,这个就跟 ValueAnimator 中的可变长参数的意义一样了,就是指这个属性值是从哪变到哪。像我们上面的代码中指定的就是将 textview 的 alpha 属性从 0 变到 1 再变到 0; 下面我们再来看一下如何实现旋转效果:

ObjectAnimator animator =ObjectAnimator.ofFloat(tv,"rotation",0,180,0); 

animator.setDuration(2000); 

animator.start();

ProperActivity:

ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_propre); img = (ImageView) findViewById(R.id.imgView); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { img.setAlpha(0.5f); // img.setImageAlpha(120);//0~255 } }); // img.setTranslationX(); } /***/ public void translate(View view){ ObjectAnimator oa = ObjectAnimator.ofFloat(img,"translationY",0,200,-150,250); oa.setDuration(2000); oa.start(); } public void alpha(View view){ // img.setAlpha(); ObjectAnimator oa = ObjectAnimator.ofFloat(img,"alpha",1.0f,0.1f,1.0f,0.1f); oa.setDuration(2000); oa.start(); } /** * 直接点击imageView 设置 图片的透明度*/ public void togther(View view){ //AnimationSet 补间动画集 AnimatorSet as = new AnimatorSet(); ObjectAnimator oa_t = ObjectAnimator.ofFloat(img,"translationY",0,200,-150,250); ObjectAnimator oa_a = ObjectAnimator.ofFloat(img,"alpha",1.0f,0.1f,1.0f,0.1f); ObjectAnimator oa_t1 = ObjectAnimator.ofFloat(img,"translationX",0,10,50,-50,200); // as.playTogether(oa_t,oa_a);//同时执行 // as.playSequentially(oa_t,oa_a);//挨个执行 /**play : 执行 with:和谁一起执行 after:在谁之后执行*/ as.play(oa_t).with(oa_a).after(oa_t1);//看情况执行 as.setTarget(img); as.setDuration(5000); as.start(); }

转载请注明原文地址: https://www.6miu.com/read-410.html

最新回复(0)