分享

tabindicator 4 - with textcolor changed

 liluvu 2016-07-19
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);
        btn1.setTextColor(0xff0000ff);
        btn2.setTextColor(0x0f0000ff);
        btn3.setTextColor(0x0f0000ff);
        int color = btn1.getCurrentTextColor();
        Log.e("suosuoa", "color = " + color);
        if (color == 0xff0000ff) Log.e("suosuoa", "color same");
        TextView[] textViews = {btn1, btn2, btn3};
        mTabIndicatorAnim = new TabIndicatorAnim(image, textViews, mWidth);

        image.setTranslationX(mLastPos*mWidth);
        
        btn1.setOnClickListener(new Button.OnClickListener(){//创建监听    
            public void onClick(View v) {    
            mTabIndicatorAnim.startAnim(mLastPos, 0);
            mLastPos = 0;
            }    
        }); 
        
        btn2.setOnClickListener(new Button.OnClickListener(){//创建监听    
            public void onClick(View v) {    
            mTabIndicatorAnim.startAnim(mLastPos, 1);
            mLastPos = 1;
            }    
        }); 
        
        btn3.setOnClickListener(new Button.OnClickListener(){//创建监听    
            public void onClick(View v) {    
            mTabIndicatorAnim.startAnim(mLastPos, 2);
            mLastPos = 2;
            }    
        });  
        
        cancel.setOnClickListener(new Button.OnClickListener(){//创建监听    
            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");
                }            
                
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) image.getLayoutParams();  
                lp.leftMargin = 180;
                lp.width =180;
                image.setLayoutParams(lp);  
                
                image.getLeft();
                Log.e("suosuo", "image.getLeft(); " + image.getLeft() + " width " + image.getWidth());
                Log.e("suosuo", "image.getLeft(); " + image.getLeft() + " width " + image.getWidth());
            }    
        });          
//        btn4.setOnClickListener(new Button.OnClickListener(){//创建监听    
//            public void onClick(View v) {    
//             mTabIndicatorAnim.startAnim(mLastPos, 3);
//             mLastPos = 3;
//            }    
//        });          
    }  
    
    class TabIndicatorAnim {
    private int mWidth;
    private View mView;    
    private TextView[] mTextViews;
    private ValueAnimator mAnimator;
      private float mScaleUpProp = 130;
    private float mTotalProp = 480;
    private int mDuration = 5000;//480;    
   
    TabIndicatorAnim(View view, TextView[] views, int width) {
    mTextViews = views;
    mView = view;
    mWidth = width;    
    }
   
    public void startAnim(int from, int to){
    if (from == to) {
    return;
    } else if (from < to) { // from left to right
    cancelAnim();
    anim(from, to, 1);
    } else { // from right to left
    cancelAnim();
    anim(from, to, 0);    
    }    
    }
   
    private int preCalculateTotalDuration(int from, int to, int l2r){
    int mStep = 60;
    int duration = 0;
        int toX1 = to*mWidth;
        int toX2 = (to+1)*mWidth;
        int x1 = getX1();
        int x2 = getX2();
        float scale = getScaleX();
       
    while (!(x1 == toX1 && x2 == toX2)) {        
    if (l2r == 1) {
        if (x2 < toX2){
        if (scale >= 1 && scale < 2) {
        duration++;
        x2 += mStep;
        } else if (scale == 2) {
        duration++;     
        x1 += mStep;
        }                
        } else if (x2 == toX2) {
        if (x1 < toX1) {
        duration++;
        x1 += mStep;
        }
        } else if (x2 > toX2){
        duration++;
        x2 -= mStep;
        }
    } else {
        if (x1 > toX1){
        if (scale >= 1 && scale < 2) {
        duration++;
        x1 -= mStep;
        } else if (scale == 2) {
        duration++;
        x1 -= mStep;
        }            
        } else if (x1 == toX1) {
        if (x2 > toX2) {
        duration++;
        x2 -= mStep;
        }
        } else if (x1 < toX1){
        duration++;
        x1 += mStep;
       
    }  
    }
   
    return duration;    
    }
   
    private void cancelAnim(){
    if (mAnimator != null) mAnimator.cancel();    
    }
   
    private int getX1(){    
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
    return lp.leftMargin;
    }

    private int getX2(){    
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
    return lp.leftMargin+lp.width;
    }    
   
    private float getScaleX(){
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
    return (float)lp.width/mWidth;
    }
   
    private void setLine(int x1, int x2){
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
            lp.leftMargin = x1;
            lp.width = x2-x1;
            mView.setLayoutParams(lp);  
    }
   
    private void setTranslationX(int x){
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
            lp.leftMargin = x;
            mView.setLayoutParams(lp); 
    }
   
    private void updateTextColor(int from, int to, int colorStart, int colorEnd, float fraction) {
    int fromColor = ((colorStart-(int)((colorStart-colorEnd)*fraction)) << 24) + 0xff;
    int toColor =  ((colorEnd+(int)((colorStart-colorEnd)*fraction)) << 24) + 0xff;
    mTextViews[from].setTextColor(fromColor);
    mTextViews[to].setTextColor(toColor);    
    }
   
    private void switchTextColor(int from, int to, int colorStart, int colorEnd) {
    mTextViews[from].setTextColor((colorEnd << 24)+0xff);
    mTextViews[to].setTextColor((colorStart << 24)+0xff);
    }    
   
        private void anim(final int from, final int to, final int l2r) {             
        final int toX1 = to*mWidth;
        final int toX2 = (to+1)*mWidth;
        final int duration = preCalculateTotalDuration(from, to, l2r);
        Log.e("suosuoa", "preCalculateTotalDuration " + duration);
       
            mAnimator = ValueAnimator.ofFloat(0, duration);  
            mAnimator.setTarget(mView);  
            mAnimator.setDuration(mDuration).start();  
         //   mAnimator.setInterpolator(new LinearInterpolator());        

            mAnimator.addUpdateListener(new AnimatorUpdateListener() {        
        int step = 60;
                           
                public void onAnimationUpdate(ValueAnimator animation) {  
                Float value = (Float) animation.getAnimatedValue(); 
                int x1 = getX1();
                int x2 = getX2();
                float scale = getScaleX();
                Log.e("suosuoa", "from " + from + " to " + to +  " value " + value +  " scale " + scale + " x1 " + x1 + " x2 " + x2 + " toX1 " + toX1 + " toX2 " + toX2);
               
                updateTextColor(from, to, 0xff, 0x0f, step/(float)duration);
            if (x1 == toX1 && x2 == toX2) {
            mAnimator.cancel();
            switchTextColor(from, to,  0xff, 0x0f);
            }
           
            if (l2r == 1) {
                if (x2 < toX2){
                if (scale >= 1 && scale < 2) {
                Log.e("suosuoa", "mmmmmmm");
                setLine(x1, x2+step);
                } else if (scale == 2) {
                // move
                Log.e("suosuoa", "aaaaaaaaaaaaaaaaaaaaa");
                setTranslationX(x1+step);
                }                
                } else if (x2 == toX2) {
                if (x1 < toX1) {
                Log.e("suosuoa", "bbbbbbbbbbbbbb");           
                setLine(x1+step, x2);
                }
                } else if (x2 > toX2){
                setLine(x1, x2-step);
                }
            } else {
                if (x1 > toX1){
                if (scale >= 1 && scale < 2) {
                setLine(x1-step, x2);
                } else if (scale == 2) {

                Log.e("suosuoa", "dddddd");
                setTranslationX(x1-step);
                } else if (scale < 1) {                
                }                
                } else if (x1 == toX1) {
                if (x2 > toX2) {
                Log.e("suosuoa", "eeeeee");                
                setLine(x1, x2-step);
                }
                } else if (x1 < toX1){
                setLine(x1+step, x2);
               
            }  
               }                
              });
        }      
    }

}



<LinearLayout xmlns:android="http://schemas./apk/res/android"
    xmlns:tools="http://schemas./tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    tools:context="com.example.tabanim.MainActivity" >

    <TextView
        android:id="@+id/textView0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:id="@+id/linearLayout1">
       <ImageView 
           android:id="@+id/imageView" 
           android:layout_width="360dp" 
           android:layout_height="wrap_content" 
           android:src="@drawable/abc_tab_selected_pressed_holo">          
       </ImageView> 
    
  <LinearLayout
      android:id="@+id/linearLayout2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" >

  <TextView
      android:id="@+id/textView1"
      android:layout_width="120dp"
      android:layout_height="60dp"
      android:background="#ffffff"
      android:text="TextView"
      android:textSize="20sp" 
      android:gravity="center"/>

  <TextView
      android:id="@+id/textView2"
      android:layout_width="120dp"
      android:layout_height="60dp"

      android:background="#ffffff"       
      android:text="TextView"       
      android:textSize="20sp" 
      android:gravity="center"/>
  <TextView
      android:id="@+id/textView3"
      android:layout_width="120dp"
      android:layout_height="60dp"
      android:background="#ffffff"
      android:text="TextView"       
      android:textSize="20sp" 
      android:gravity="center"/>
  </LinearLayout>

  
      <Button
          android:id="@+id/Cancel"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Cancel" />
    </LinearLayout>



</LinearLayout>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多