两个文本动画:
public class TwoTextActivity extends Activity { private ObjectAnimator mPeekAnimator; private MoveRunner mPeekRunner = null; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two_text_activity_main); startAnimation(); } private void startAnimation() { TextView view = (TextView) findViewById(R.id.text); TextView view2 = (TextView) findViewById(R.id.text2); mPeekRunner = new MoveRunner(view, view2, 36, false); this.mHandler.postDelayed(this.mPeekRunner, 1000L); } class MoveRunner implements Runnable { private final View mCurrent; private final View mNext; private final boolean mWillBeHidden; private final float mXBy; MoveRunner(View currentView, View nextView, float xBy, boolean willBeHiden) { this.mCurrent = currentView; this.mNext = nextView; this.mXBy = xBy; this.mWillBeHidden = willBeHiden; } public void run() { runPeekAnimator(this.mCurrent, this.mNext, this.mXBy, this.mWillBeHidden); } } private void runPeekAnimator(View view, View paramView2, float paramFloat, boolean paramBoolean) { if (shouldRunPeekAnimator()) { float f = view.getX(); float[] arrayOfFloat = new float[2]; arrayOfFloat[0] = f; arrayOfFloat[1] = (f + paramFloat); mPeekAnimator = ObjectAnimator.ofFloat(view, "x", arrayOfFloat); mPeekAnimator.setDuration(200L); mPeekAnimator.addListener(new PeekAnimatorListener(view, paramView2, paramFloat, this.mHandler, paramBoolean)); mPeekAnimator.start(); // this.mFragment.vibrate(200L); } else { // this.mFragment.stopVibrating(); } } class PeekAnimatorListener extends SimpleAnimatorListener { private final View mCurrent; private final Handler mHandler; private final View mNext; private final boolean mWillEndHidden; private final float mXBy; public PeekAnimatorListener(View currentView, View nextView, float xBy, Handler handler, boolean hiden) { this.mCurrent = currentView; this.mNext = nextView; this.mXBy = xBy; this.mHandler = handler; this.mWillEndHidden = hiden; } public void onAnimationEnd(Animator animator) { int delay = 0; if (shouldRunPeekAnimator()) { Log.d("TwoTextActivity", "mWillEndHidden:" + mWillEndHidden); if (!this.mWillEndHidden) { delay = 100; this.mHandler.postDelayed(new MoveRunner(this.mCurrent, this.mNext, -this.mXBy, true), delay); } else { delay = 400; this.mHandler.postDelayed(new MoveRunner(this.mNext, this.mCurrent, this.mXBy, false), delay); } } } } private boolean shouldRunPeekAnimator() { return true; } }
一个文本动画:
public class OneTextActivity extends Activity { private ObjectAnimator mPeekAnimator; private MoveRunner mPeekRunner = null; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one_text_activity_main); startAnimation(); } private void startAnimation() { TextView view = (TextView) findViewById(R.id.text); // TextView view2 = (TextView) findViewById(R.id.text2); mPeekRunner = new MoveRunner(view, /*view2,*/ 36, false); this.mHandler.postDelayed(this.mPeekRunner, 1000L); } class MoveRunner implements Runnable { private final View mCurrent; // private final View mNext; private final boolean mWillBeHidden; private final float mXBy; MoveRunner(View currentView, /*View nextView,*/ float xBy, boolean willBeHiden) { this.mCurrent = currentView; // this.mNext = nextView; this.mXBy = xBy; this.mWillBeHidden = willBeHiden; } public void run() { runPeekAnimator(this.mCurrent, /*this.mNext,*/ this.mXBy, this.mWillBeHidden); } } private void runPeekAnimator(View view, /*View paramView2,*/ float paramFloat, boolean paramBoolean) { if (shouldRunPeekAnimator()) { float f = view.getX(); float[] arrayOfFloat = new float[2]; arrayOfFloat[0] = f; arrayOfFloat[1] = (f + paramFloat); mPeekAnimator = ObjectAnimator.ofFloat(view, "x", arrayOfFloat); mPeekAnimator.setDuration(200L); mPeekAnimator.addListener(new PeekAnimatorListener(view, /*paramView2,*/ paramFloat, this.mHandler, paramBoolean)); mPeekAnimator.start(); // this.mFragment.vibrate(200L); } else { // this.mFragment.stopVibrating(); } } class PeekAnimatorListener extends SimpleAnimatorListener { private final View mCurrent; private final Handler mHandler; // private final View mNext; private boolean mWillEndHidden; private final float mXBy; public PeekAnimatorListener(View currentView, /*View nextView,*/ float xBy, Handler handler, boolean hiden) { this.mCurrent = currentView; // this.mNext = nextView; this.mXBy = xBy; this.mHandler = handler; this.mWillEndHidden = hiden; } public void onAnimationEnd(Animator animator) { int delay = 0; if (shouldRunPeekAnimator()) { Log.d("OneTextActivity", "mWillEndHidden:" + mWillEndHidden); if (!this.mWillEndHidden) { delay = 100; this.mHandler.postDelayed(new MoveRunner(this.mCurrent, /*this.mNext,*/ -this.mXBy, true), delay); } else { delay = 400; // this.mHandler.postDelayed(new MoveRunner(/*this.mNext,*/ this.mCurrent, this.mXBy, // false), delay); this.mHandler.postDelayed(new MoveRunner(this.mCurrent, /*this.mNext,*/ -this.mXBy, false), delay); } } } } private boolean shouldRunPeekAnimator() { return true; } }
时间: 2024-10-10 21:54:08