Android 动画三种 摘要

动画三种   1帧动画 2view 3属性动画

A 帧动画 是把一些列图片类似于播放展示  

  1. img_loding.setImageResource(R.drawable.loading_animation);  
  2. animationDrawable = (AnimationDrawable) img_loding.getDrawable();  
  3. animationDrawable.start(); //Drawable作为View的背景并通过Drawable来播放动画

  B View 动画

     PS:

View动画改变的只是显示,并不能在新的位置响应事件。

同理,有时候会出现动画完成后View无法隐藏的现象,即setVisibility(View.GONE)失效,这时只需

调用view.clearAnimation()清除View动画即可解决问题。


    

  1  在代码里使用XML中定义好的动画:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_demo);

view.startAnimation(animation);

  2  在代码里直接使用View动画

示例:

 AlphaAnimation animation = new AlphaAnimation(0,1);

animation.setDuration(1000);

view.startAnimation(animation);

动画集合示例: 

AnimationSet set = new AnimationSet(true);

set.setDuration(1000);

 AlphaAnimation animation1 = new AlphaAnimation(0,1);

animation1.setDuration(1000);

TranslateAnimation animation2 = new TranslateAnimation(0,100, 0, 200);

animation2.setDuration(1000);

set.addAnimation(animation1);

set.addAnimation(animation2);

view.startAnimation(set);

//动画监听

animation.setAnimationListener(new Animation.AnimationListener(){

@Override

public void onAnimationStart(Animation animation){

//动画开始时

}

@Override

public void onAnimationEnd(Animation animation){

//动画结束时

}

@Override

public void onAnimationRepeat(Animation animation){

//动画重复时

}

});


3 属性动画 ps Android11 3.0 才加入 object必须提供setA方法,如果动画没有传递初始值,还必须提供getA方法,因为系统要去获取属性a的初始值(如果获取不到初始值,程序直接crash


3.1 ValueAnimator

ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f, 3f, 5f); 

anim.setDuration(1000); 

anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  

@Override  

public void onAnimationUpdate(ValueAnimator animation) {  

float currentValue = (float) animation.getAnimatedValue(); 

}); 

anim.start();

3.2

ObjectAnimator

ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(image, "scaleX", 0f, 1f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(image, "scaleY", 0f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
// 设置插值器
animatorSet.setInterpolator(new JellyInterpolator());
animatorSet.setDuration(3000);
animatorSet.start();
animator.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {
    }
    @Override
    public void onAnimationEnd(Animator animator) {
    }
    @Override
    public void onAnimationCancel(Animator animator) {
    }

    @Override
    public void onAnimationRepeat(Animator animator) {
    }
});


Android 3.0 之后,Google给View增加了animate方法来直接驱动 属性动画,它可以被认为是属性动画的一种简写方式。

java]  view plain  copy
  1. private void animateButtonsIn() {  
  2.     for (int i = 0; i < bgViewGroup.getChildCount(); i++) {  
  3.         View child = bgViewGroup.getChildAt(i);  
  4.         child.animate()  
  5.                 .setStartDelay(100 + i * DELAY)  
  6.                 .setInterpolator(interpolator)  
  7.                 .alpha(1)  
  8.                 .scaleX(1)  
  9.                 .scaleY(1);  
  10.     }  

     



猜你喜欢

转载自blog.csdn.net/yan822/article/details/79023800