android animator
5059 ワード
参考属性アニメーションAndroidソース分析—属性アニメーションの動作原理
一、viewアニメーション TranslateAnimation ScaleAnimation RotateAnimation AlphaAnimation
XMLを使用してアニメーションを定義することをお勧めします.
上のアニメーションをどのように適用しますか? viewアニメーションはviewオブジェクト のみを制御する viewアニメーションは、パンズーム回転のみフェードアウト効果 viewアニメーションは映像に対してアニメーションのインタラクティブなイベントをするだけで問題が発生しやすい 二、フレームアニメーション
AnimationDrawableを使用します.フレームのアニメーションはOOMを引き起こしやすくて、できるだけ大きすぎるピクチャーを使うことを避けます
三、属性アニメーション
android3.0,api 11以降にプロパティアニメーションがあります.デフォルトフレームレート10 ms/フレーム、アニメーションデフォルト間隔300 ms. ValueAnimator ValueAnimatorから引き継いだObjectAnimator AnimatorSetアニメーションセットの5秒以内にviewの回転、平行移動、スケール、透明度が変更されました.
四、一つのButton幅を現在の幅から500 pxに増加させる方式1:viewアニメーションを使用して、伸ばしたのは映像だけで、背景図も文字も変形します. 方式2: 方式三:方式二を に包装する方式4:ValueAnimator を使用
一、viewアニメーション
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);
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 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();
ObjectAnimator.ofFloat(myObject,"translationY",
-myObject.getHeight()).start();//
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に増加させる
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();
}
}
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);