package com.example.tabanim; import android.animation.Animator; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { private ImageView image; private TextView btn1, btn2, btn3, btn4; private Button cancel; private TabIndicatorAnim mTabIndicatorAnim; private int mWidth = (int) (1080/3.0); private int mLastPos = 0; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView)findViewById(R.id.imageView); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) image .getLayoutParams(); lp.width = mWidth; image.setLayoutParams(lp); image.setScaleType(ScaleType.FIT_XY); btn1 = (TextView)findViewById(R.id.textView1); btn2 = (TextView)findViewById(R.id.textView2); btn3 = (TextView)findViewById(R.id.textView3); // btn4 = (Button)findViewById(R.id.button4); cancel = (Button)findViewById(R.id.Cancel); mTabIndicatorAnim = new TabIndicatorAnim(image, mWidth); image.setTranslationX(mLastPos*mWidth); btn1.setOnClickListener(new Button.OnClickListener(){//創(chuàng)建監(jiān)聽 public void onClick(View v) { mTabIndicatorAnim.startAnim(mLastPos, 0); mLastPos = 0; } }); btn2.setOnClickListener(new Button.OnClickListener(){//創(chuàng)建監(jiān)聽 public void onClick(View v) { mTabIndicatorAnim.startAnim(mLastPos, 1); mLastPos = 1; } }); btn3.setOnClickListener(new Button.OnClickListener(){//創(chuàng)建監(jiān)聽 public void onClick(View v) { mTabIndicatorAnim.startAnim(mLastPos, 2); mLastPos = 2; } }); cancel.setOnClickListener(new Button.OnClickListener(){//創(chuàng)建監(jiān)聽 public void onClick(View v) { mTabIndicatorAnim.cancelAnim(); float f1 = 360.0f; int f2 = mWidth; int retval = Float.compare(f1, f2); if(retval > 0) { Log.e("suosuo", "f1 is greater than f2"); } else if(retval < 0) { Log.e("suosuo", "f1 is less than f2"); } else { Log.e("suosuo", "f1 is equal to f2"); } } }); // btn4.setOnClickListener(new Button.OnClickListener(){//創(chuàng)建監(jiān)聽 // public void onClick(View v) { // mTabIndicatorAnim.startAnim(mLastPos, 3); // mLastPos = 3; // } // }); } class TabIndicatorAnim { private int mWidth; private View mView; private int mCancelLastStartPos = -1; private ValueAnimator mAnimator; private float mScaleUpProp = 130; private float mTotalProp = 480; private int mDuration = 5000;//480; private float mScaleDownValue = 1.0f; private float mScaleUpValue = 2.0f; TabIndicatorAnim(View view, int width) { mView = view; mWidth = width; } public void startAnim(int from, int to){ //cancelAnim(); if (from == to) { return; } else if (from < to) { // from left to right cancelAnim(); anim(from, to, 1, to-from); } else { // from right to left cancelAnim(); anim(from, to, 0, from-to); } } private void cancelAnim(){ if (mAnimator != null) mAnimator.cancel(); } private float getX1(){ return (float)mView.getTranslationX(); } private float getX2(){ float scale = mView.getScaleX(); return (float)(mView.getTranslationX()+scale*mWidth); } private void setLine(float x1, float x2, float pivot){ float scale = (float)(x2-x1)/mWidth; if (scale > 2) scale = 2; if (scale < 1) scale = 1; mView.setScaleX(scale); } private void anim(final int from, final int to, final int l2r, final int count) { mAnimator = ValueAnimator.ofFloat(0, 1080); mAnimator.setTarget(mView); mAnimator.setDuration(mDuration).start(); // mAnimator.setInterpolator(new LinearInterpolator()); final float toX1 = to*mWidth; final float toX2 = (to+1)*mWidth; mAnimator.addUpdateListener(new AnimatorUpdateListener() { boolean isAfterScaleUp = false; boolean isAfterLeaveGap = false; int afterScaleUpX = 0; float orginalScaleX = mView.getScaleX(); float orginalTranslationX = mView.getTranslationX(); int transXCmpRet = Float.compare(orginalTranslationX, from*mWidth); int scaleXCmpRet = Float.compare(2.0f, orginalScaleX); int test = 1; int step = 10; public void onAnimationUpdate(ValueAnimator animation) { Float value = (Float) animation.getAnimatedValue(); float aa =toX1; float x1 = getX1(); float x2 = getX2(); float scale = mView.getScaleX(); Log.e("suosuoa", "from " + from + " to " + to + " value " + value + " scale " + scale + " x1 " + x1 + " x2 " + x2 + " toX1 " + toX1 + " toX2 " + toX2); if (test == 1) { if (x1 == toX1 && x2 == toX2) { mAnimator.cancel(); } if (l2r == 1) { if (x2 < toX2){ if (scale >= 1 && scale < 2) { Log.e("suosuoa", "mmmmmmm"); mView.setPivotX(0); setLine(x1, x2+step, 0); } else if (scale == 2) { // move Log.e("suosuoa", "aaaaaaaaaaaaaaaaaaaaa"); mView.setTranslationX(x1+step); } else if (scale < 1) { mView.setScaleX(1.0f); } } else if (x2 == toX2) { if (x1 <= toX1) { Log.e("suosuoa", "bbbbbbbbbbbbbb"); if (scale == 2) { Log.e("suosuoa", "cccccc"); mView.setPivotX(mWidth); mView.setTranslationX((from+1)*mWidth); } setLine(x1+step, x2, mWidth); } } else if (x2 > toX2){ setLine(x1+step, x2-step, mWidth/2); } } else { if (x1 > toX1){ if (scale >= 1 && scale < 2) { Log.e("suosuoa", "nnnnnnnnn"); mView.setPivotX(mWidth); setLine(x1-step, x2, 0); } else if (scale == 2) { // move Log.e("suosuoa", "dddddd"); mView.setTranslationX(x1-step); } else if (scale < 1) { mView.setScaleX(1.0f); } } else if (x1 == toX1) { if (x2 <= toX2) { Log.e("suosuoa", "eeeeee"); if (scale == 2) { Log.e("suosuoa", "ffffffff"); mView.setPivotX(0); mView.setTranslationX((from-1)*mWidth); } setLine(x1, x2-step, mWidth); } } else if (x1 < toX1){ //setLine(x1+step, x2-step, mWidth/2); } } } else { if (value < mScaleUpProp){ mView.setPivotX((l2r == 1)?(0):(mWidth)); mView.setScaleX((float) (1+value/mScaleUpProp)); // scale 1 to 2 //Log.e("suosuo", "getTranslationX1 " + mView.getTranslationX()); } else if (value < mTotalProp) {//-50){ if (!isAfterScaleUp) { mView.setPivotX((l2r == 1)?(mWidth):(0)); if (count == 1) { mView.setTranslationX(to*mWidth); //Log.e("suosuo", "getTranslationX2 " + mView.getTranslationX()); } else { afterScaleUpX = (l2r == 1)?((from+1)*mWidth):((from-1)*mWidth); mView.setTranslationX(afterScaleUpX); } isAfterScaleUp = true; Log.e("suosuo", "getTranslationX " + mView.getTranslationX()); } else { //mView.setPivotX((l2r == 1)?(mWidth):(0)); // mView.setScaleX((float) (2-(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); //scale 2 to 1 // if (count > 1) { // mView.setTranslationX((l2r == 1)? // (afterScaleUpX+(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp)): // (afterScaleUpX-(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); // Log.e("suosuo", "getTranslationX3 " + mView.getTranslationX()); // } } } // else if (value < mTotalProp-20){ // if (!isAfterLeaveGap) { // mView.setPivotX((l2r == 1)?(mWidth):(0)); // if (count == 1) { // mView.setTranslationX(to*mWidth+40); // Log.e("suosuo", "getTranslationX2 " + mView.getTranslationX()); // } else { // afterScaleUpX = (l2r == 1)?((from+1)*mWidth):((from-1)*mWidth); // mView.setTranslationX(afterScaleUpX); // } // isAfterLeaveGap = true; // } else { // mView.setScaleX((float) (2-(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); //scale 2 to 1 // if (count > 1) { // mView.setTranslationX((l2r == 1)? // (afterScaleUpX+(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp)): // (afterScaleUpX-(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); // Log.e("suosuo", "getTranslationX3 " + mView.getTranslationX()); // } // } // } else { // if (!isAfterScaleUp) { // mView.setPivotX((l2r == 1)?(mWidth):(0)); // if (count == 1) { // mView.setTranslationX(to*mWidth); // Log.e("suosuo", "getTranslationX2 " + mView.getTranslationX()); // } else { // afterScaleUpX = (l2r == 1)?((from+1)*mWidth):((from-1)*mWidth); // mView.setTranslationX(afterScaleUpX); // } // isAfterScaleUp = true; // } else { // mView.setScaleX((float) (2-(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); //scale 2 to 1 // if (count > 1) { // mView.setTranslationX((l2r == 1)? // (afterScaleUpX+(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp)): // (afterScaleUpX-(count-1)*mWidth*(value-mScaleUpProp)/(mTotalProp-mScaleUpProp))); // Log.e("suosuo", "getTranslationX3 " + mView.getTranslationX()); // } // } // } } } }); mAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationCancel(Animator arg0) { // TODO Auto-generated method stub Log.e("suosuo", "onAnimationCancel"); mCancelLastStartPos = from; } @Override public void onAnimationEnd(Animator arg0) { // TODO Auto-generated method stub // mView.setScaleX(1); // mView.setTranslationX(to*mWidth); } @Override public void onAnimationRepeat(Animator arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animator arg0) { // TODO Auto-generated method stub } }); } } } |
|