NineOldAndroid开源库简单使用demo
看到很多开源库都使用了这个动画框架,就自己试了一下,果然很强大。把测试代码贴上,方便以后使用
package com.test.animation; import android.animation.ValueAnimator; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorSet; import com.nineoldandroids.animation.ObjectAnimator; import com.nineoldandroids.animation.PropertyValuesHolder; import com.nineoldandroids.view.ViewHelper; import com.nineoldandroids.view.ViewPropertyAnimator; public class AnimationActivity extends Activity { Button button; private void initView() { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setGravity(Gravity.CENTER_HORIZONTAL); button = new Button(this); button.setText("target"); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.addView(button, params); setContentView(layout); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); // testAnim00(); // testAnim0(); // testAnim1(); // testAnim2(); testAnim3(); // testAnim4(); // testAnim5(); // testAnim6(); // testAnim7(); // startActivity(new Intent(this,AnimationContextMenuActivity.class)); } private void testAnim00() { Animator animator1 = AnimatorUtils.rotationCloseToRight(button).setDuration(2000); Animator animator2 = AnimatorUtils.rotationOpenFromRight(button).setDuration(2000); Animator animator3 = AnimatorUtils.rotationCloseVertical(button).setDuration(2000); Animator animator4 = AnimatorUtils.rotationOpenVertical(button).setDuration(2000); Animator animator5 = AnimatorUtils.alphaDisappear(button).setDuration(2000); Animator animator6 = AnimatorUtils.alphaAppear(button).setDuration(2000); Animator animator7 = AnimatorUtils.translationRight(button, 100).setDuration(2000); Animator animator8 = AnimatorUtils.translationLeft(button, 100).setDuration(2000); final AnimatorSet set = new AnimatorSet(); // set.play(animator1).before(animator2)/*.after(animator3).after(animator4).after(animator5).after(animator6).after(animator7).after(animator8)*/; // set.play(animator2).before(animator3); // set.play(animator3).before(animator4); // set.play(animator4).before(animator5); // set.play(animator5).before(animator6); // set.play(animator6).before(animator7); // set.play(animator7).before(animator8); /*===============//after before只能有两个,注意 before/after的顺序 或者用下面的===========================*/ set.playSequentially(animator1,animator2,animator3,animator4,animator5,animator6,animator7,animator8); set.start(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { set.start(); } }); } private void testAnim0() { Animator anim1 = ObjectAnimator.ofFloat(button, "rotationX", 90, 0).setDuration(2000); Animator anim2 = ObjectAnimator.ofFloat(button, "rotationX", 0, 90).setDuration(2000); final AnimatorSet set = new AnimatorSet(); set.play(anim1).after(anim2); set.start(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { set.start(); } }); } private void testAnim1() { ObjectAnimator.ofFloat(button, "translationY", 100).setDuration(1000).start(); } private void testAnim2() { Animator animator = ObjectAnimator.ofFloat(button, "translationX", 100).setDuration(1000); animator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) { } @Override public void onAnimationEnd(Animator animator) { ObjectAnimator.ofFloat(button, "translationX", 0).setDuration(1000).start(); } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } }); animator.start(); } private void testAnim3() { ViewPropertyAnimator.animate(button).translationY(100).start(); final ValueAnimator animator = ValueAnimator.ofFloat(0, 360).setDuration(2000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // ViewHelper.setPivotX(button,0); // ViewHelper.setRotationX(button, (Float) animation.getAnimatedValue()); ViewHelper.setPivotY(button, 0); ViewHelper.setRotationY(button, (Float) animation.getAnimatedValue()); //配套使用 } }); animator.start(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animator.start(); } }); } private void testAnim4() { //左移动100,右移动100,alpha 变为0.2 跟5效果一样 AnimatorSet set = new AnimatorSet(); set.playTogether(ObjectAnimator.ofFloat(button, "translationX", 100), ObjectAnimator.ofFloat(button, "translationY", 100), ObjectAnimator.ofFloat(button, "alpha", 1, 0.2f)); set.setDuration(2000).start(); } private void testAnim5() { //左移动100,右移动100,alpha 变为0.2 ObjectAnimator.ofPropertyValuesHolder(button, PropertyValuesHolder.ofFloat("translationX", -100), PropertyValuesHolder.ofFloat("translationY", 100), PropertyValuesHolder.ofFloat("alpha", 1, 0.3f)).setDuration(2000).start(); } private void testAnim6() { // ViewPropertyAnimator.animate(button).translationX(100).translationY(100).setDuration(2000).start(); // ViewPropertyAnimator.animate(button).translationY(100).rotationYBy(720).setDuration(2000).start(); ViewPropertyAnimator.animate(button).rotationYBy(360).x(300).y(500).setDuration(2000).start();//旋转角度为360的倍数,x(..)y(..)后面为动画后位置 } //===========================================================================demo========================================================= private void testAnim7() { final LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setGravity(Gravity.CENTER_HORIZONTAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); for (int i = 0; i < 5; i++) { Button btn = new Button(this); btn.setText("btn" + i); btn.setTag(i); layout.addView(btn, params); } Button startBtn = new Button(this); startBtn.setText("start"); startBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isAdimateOpen) { isAdimateOpen = true; doAnimateOpen(layout.getChildAt(0), 0, 5, 300); doAnimateOpen(layout.getChildAt(1), 1, 5, 300); doAnimateOpen(layout.getChildAt(2), 2, 5, 300); doAnimateOpen(layout.getChildAt(3), 3, 5, 300); doAnimateOpen(layout.getChildAt(4), 4, 5, 300); } else { isAdimateOpen = false; doAnimateClose(layout.getChildAt(0), 0, 5, 300); doAnimateClose(layout.getChildAt(1), 1, 5, 300); doAnimateClose(layout.getChildAt(2), 2, 5, 300); doAnimateClose(layout.getChildAt(3), 3, 5, 300); doAnimateClose(layout.getChildAt(4), 4, 5, 300); } } }); layout.addView(startBtn, params); setContentView(layout); } boolean isAdimateOpen = false; /** * 打开菜单的动画 * * @param view 执行动画的view * @param index view在动画序列中的顺序 * @param total 动画序列的个数 * @param radius 动画半径 */ private void doAnimateOpen(View view, int index, int total, int radius) { if (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } double degree = Math.PI * index / ((total - 1) * 2); int translationX = (int) (radius * Math.cos(degree)); int translationY = (int) (radius * Math.sin(degree)); AnimatorSet set = new AnimatorSet(); //包含平移、缩放和透明度动画 set.playTogether( ObjectAnimator.ofFloat(view, "translationX", 0, translationX), ObjectAnimator.ofFloat(view, "translationY", 0, translationY), ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f), ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f), ObjectAnimator.ofFloat(view, "alpha", 1f, 1.5f)); //动画周期为500ms set.setDuration(1000).start(); } /** * 关闭菜单的动画 * * @param view 执行动画的view * @param index view在动画序列中的顺序 * @param total 动画序列的个数 * @param radius 动画半径 */ private void doAnimateClose(final View view, int index, int total, int radius) { if (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } double degree = Math.PI * index / ((total - 1) * 2); int translationX = (int) (radius * Math.cos(degree)); int translationY = (int) (radius * Math.sin(degree)); AnimatorSet set = new AnimatorSet(); //包含平移、缩放和透明度动画 set.playTogether( ObjectAnimator.ofFloat(view, "translationX", translationX, 0), ObjectAnimator.ofFloat(view, "translationY", translationY, 0), ObjectAnimator.ofFloat(view, "scaleX", 1.5f, 1f), ObjectAnimator.ofFloat(view, "scaleY", 1.5f, 1f), ObjectAnimator.ofFloat(view, "scaleY", 1.5f, 1f)); set.setDuration(1000).start(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。