android animator

5059 ワード

参考属性アニメーションAndroidソース分析—属性アニメーションの動作原理
一、viewアニメーション
  • TranslateAnimation
  • ScaleAnimation
  • RotateAnimation
  • AlphaAnimation

  • XMLを使用してアニメーションを定義することをお勧めします.
    //res/anim/animation_test.xml
    ...
    
    
    

    上のアニメーションをどのように適用しますか?
    Button mButton = (Button)findViewById(R.id.buttton1);
    Animation animation = AnimationUtils.
    loadAnimation(this,R.anim.animation_test);
    mButton.startAnimation(animation);
    
  • viewアニメーションはviewオブジェクト
  • のみを制御する
  • viewアニメーションは、パンズーム回転のみフェードアウト効果
  • viewアニメーションは映像に対してアニメーションのインタラクティブなイベントをするだけで問題が発生しやすい
  • 二、フレームアニメーション
    AnimationDrawableを使用します.フレームのアニメーションはOOMを引き起こしやすくて、できるだけ大きすぎるピクチャーを使うことを避けます
    //res/drawable/frame_animation.xml
    ...
    
    
    ...
    Button mButton = (Button)findViewById(R.id.button1);
    mButton.setBackgroundResource(R.drawable.frame_animation);
    AnimationDrawable drawable = (AnimationDrawable) mButton.getBackground();
    drawable.start();
    

    三、属性アニメーション
    android3.0,api 11以降にプロパティアニメーションがあります.デフォルトフレームレート10 ms/フレーム、アニメーションデフォルト間隔300 ms.
  • ValueAnimator
  • //       ,       
    ValueAnimator colorAnim = ObjectAnimator.ofInt(this,
    "backgroundColor",/\*Red\*/0xffff8080,/\*Blue\*/0xff8080ff);
    colorAnim.setDuration(3000);
    colorAnim.setEvaluator(new ArgbEvaluator());
    colorAnim.setRepeatCount(ValueAnimator.INFINITE);
    colorAnim.setRepeatMode(ValueAnimator.REVERSE);
    colorAnim.start();
    //    0 1   
    ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);  
    anim.setDuration(300);  
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
        @Override  
        public void onAnimationUpdate(ValueAnimator animation) {  
            float currentValue = (float) animation.getAnimatedValue();  
            Log.d("TAG", "cuurent value is " + currentValue);  
        }  
    });  
    anim.start();  
    
  • ValueAnimatorから引き継いだObjectAnimator
  • ObjectAnimator.ofFloat(myObject,"translationY",
    -myObject.getHeight()).start();//        
    
  • AnimatorSetアニメーションセットの5秒以内にviewの回転、平行移動、スケール、透明度が変更されました.
  • AnimatorSet set = new AnimatorSet();
    set.playTogether(
       ObjectAnimator.ofFloat(myView,"rotationX",0,360),
       ObjectAnimator.ofFloat(myView,"rotationY",0,180),
       ObjectAnimator.ofFloat(myView,"rotation",0,-90),
       ObjectAnimator.ofFloat(myView,"translationX",0,90),
       ObjectAnimator.ofFloat(myView,"translationY",0,90),
       ObjectAnimator.ofFloat(myView,"scaleX",1,1.5f),
       ObjectAnimator.ofFloat(myView,"scaleY",1,1.5f),
       ObjectAnimator.ofFloat(myView,"alpha",1,0.25f,1)
    );
    set.setDuration(5*1000).start();
    

    四、一つのButton幅を現在の幅から500 pxに増加させる
  • 方式1:viewアニメーションを使用して、伸ばしたのは映像だけで、背景図も文字も変形します.
  • 方式2:

    public void onClick(View v){
    if(v == Button){
    performAnimate();
    }
    }
    private void performAnimate(){
    ObjectAnimator.ofInt(mButton,"width",500).setDuration(5000).start();
    }
    これは無効であり、objectはsetメソッドを呼び出し続けるが、buttonのsetWidthはTextViewから継承されているため、最大幅と最小幅が設定、本当に機能するのはwidthである.
  • 方式三:方式二を

    private static class ViewWrapper{
    private View mTarget;
    public ViewWrapper(View target){
    mTarget = target;
    }
    public int getWidth(){
    return mTarget.getLayoutParams().width;
    }
    public void setWidth(int width){
    mTarget.getLayoutParams().width = width;
    mTarget.requestLayout();
    }
    private void performAnimate(){
    ViewWrapper wrapper = new ViewWrapper(mButton);
    ObjectAnimator.ofInt(wrapper,"width",500).setDuration(5000).start();
    }
    }
  • に包装する
  • 方式4:ValueAnimator
  • を使用
    private void performAnimate(final View target,final int start,final int end){
       ValueAnimator valueAnimator = ValueAnimator.ofInt(1,100);//      1  100
       valueAnimator.addUpdateListener(
          new AnimatorUpdateListener(){
             //    IntEvaluator  ,         
             private IntEvalluator mEvaluaotr = new IntEvalluator();
             
             public void onAnimationUpdate(ValueAnimator animation){
                int currentValue = (Integer)animator.getAnimatedValue();//1~100 
                float fraction = animator.getAnimatedFraction();//    ,0~1 
                target.getLayoutParams().width = mEvaluaotr.
                evaluate(fraction,start,end);
                target.requestLayout();
            }
          }
       );
       valueAnimator.setDuration(5000).start();
    }
    performAnimate(mButton,mButton.getWidth(),500);