Android自定義View精品(RollWeekView-炫酷的星期日期選擇控件)

版權聲明:本文爲openXu原創文章【openXu的博客】,未經博主允許不得以任何形式轉載

目錄:


  最近收到一個自定義控件的需求,需要做一個日期選擇控件,實現圖如下:
    這裏寫圖片描述

  一次展示一個星期的5天,中間放大的爲當前選中的;如果點擊了其中一個日期,比如星期五,那麼整體向左滑動,並將星期五慢慢放大,星期三慢慢縮小,星期六和星期天就展示出來了;如果繼續點擊星期六,那麼照樣是整體向左滑動一個單位,星期日右邊就需要塞進一個星期一。就是一個無限循環的過程,中間選中的需要展示當天的日期。

1、分析

  最開始,我想到在LinearLayout中依次排開7個子控件(星期幾),當點擊之後,把所有的子控件通過動畫移動相應的偏移量,但是,如果移動後就會出現空位的情況(週一的左邊和週日的右邊沒有了),所以這種方式可是可以,但是要解決無限循環空位的情況。
  於是就想到能不能多添加幾個子控件作爲空位替補?這個方法肯定能實現,但是究竟需要多少個替補呢?這些替補應該放在什麼位置呢?
  當前展示的5個子控件中,如果點擊最左邊或者最右邊,這樣的偏移量是最大的,需要便宜兩個子控件單位,所以左邊和右邊最多個需要2個替補就行了,算起來也不多,一共才9個(當前展示5個,左邊隱藏替補2個,右邊隱藏替補2個)。在點擊之前,先將替補位置設置好,當點擊之後,所有子控件執行偏移動畫(不可能出現空位,最多偏移2位),動畫結束之後,再重新爲隱藏的4個替補分配位置並綁定數據,綁定什麼數據那就看當前中間顯示的是星期幾了。

分析圖如下:
這裏寫圖片描述

  但是新的問題又來了,動畫執行完畢之後,怎麼判斷哪些子控件是當前正在顯示的呢(中間五個)?由於正在顯示的子控件的位置是可以確定的,因爲要重寫onMeasure(),在onMeasure()方法中可以得到單個子控件的寬度(item_width=getMeasureWidth()/5),動畫結束之後,遍歷這些子控件,如果x==0的那肯定就是當前正在展示的第一個子控件,x==item_width則是第二個…,剩餘的4個就不用判斷了,直接將兩個移動到左邊,兩個移動到右邊,然後重新爲這四個替補設置數據。

2、定義控件佈局

組合控件的佈局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_5"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_6"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_7"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_8"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_9"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_width="@dimen/week_item_sise"
            android:layout_height="@dimen/week_item_sise"
            android:background="@drawable/week_one_bg"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/tv_9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
            <TextView
                android:id="@+id/tv_date9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

3、定義CustomWeekView

  佈局寫完後,接下來應該找一個容器接納他們,這個容器就是我們定義的CustomWeekView,讓其繼承LinearLayout,然後重寫構造方法,並初始化子控件;此處自定義屬性就不再贅述,之前的所有自定義控件案例中都講解過,如果不熟悉請參考 Android自定義View(二、深入解析自定義屬性)

public class CustomWeekView extends LinearLayout{
    public CustomWeekView(Context context) {
    this(context, null);
}

    public CustomWeekView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomWeekView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.custom_week_layout, this, true);
        ll_1 = (LinearLayout) findViewById(R.id.ll_1);
        ll_2 = (LinearLayout) findViewById(R.id.ll_2);
        ...
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        ...
        tv_date1 = (TextView) findViewById(R.id.tv_date1);
        tv_date2 = (TextView) findViewById(R.id.tv_date2);
        ...
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LimitScroller);
            limit = 5;
            background = ta.getColor(R.styleable.LimitScroller_android_background, Color.TRANSPARENT);
            textSize = ta.getDimension(R.styleable.LimitScroller_android_textSize, 15f);
            final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
            textSize = textSize / fontScale + 0.5f;
            dateTextSize = ta.getInteger(R.styleable.LimitScroller_dateTextSize, 12);
            textColor = ta.getColor(R.styleable.LimitScroller_android_textColor, Color.BLACK);
            dateTextColor = ta.getColor(R.styleable.LimitScroller_dateTextColor, Color.RED);
            durationTime = ta.getInt(R.styleable.LimitScroller_durationTime, 1000);
            scaleSize = ta.getFloat(R.styleable.LimitScroller_scaleSize, 0);
            ta.recycle();  //注意回收
        }
    }
}

4、重寫onMeasure

  由於本控件寬度是填充父窗體,高度是包裹內容,這裏不需要我們手動寫代碼測量,直接調用super.onMeasure()好了。(onMeasure()詳解請移步:Android自定義View(三、深入解析控件測量onMeasure))。但是我們需要在測量完畢後,計算子控件的寬度並初次安置每個子控件的位置,這個寬度就是後面移動的單位,也作爲位置判斷的依據。由於onMeasure()方法會被調用多次(後面設置位置、顯示隱藏等等操作都會導致onMeasure()觸發),所以需要判斷一下如果ITEM_WIDTH<=0才需要計算;然後還要爲子控件設置初始日期:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.w(TAG, "測量完成,寬度*高度="+getMeasuredWidth()+"*"+getMeasuredHeight());
        if(ITEM_WIDTH<=0) {
            ITEM_WIDTH = getMeasuredWidth()/limit;
//            Log.w(TAG, "每小項尺寸:"+ITEM_WIDTH+"*"+getMeasuredHeight());
            measureInit();
        }
        if(ll_2.getX()>0 && !isFirstSeted) {
            //設置今天的日期在中間
            animalFinish = false;
            Calendar cal = Calendar.getInstance();
            int todayNum = cal.get(Calendar.DAY_OF_WEEK)-1;
            Log.d(TAG, "今天是星期"+WEEK_STR[todayNum]);
            setCenter(getEnumByNum(todayNum));
            animalFinish = true;
            isFirstSeted = true;
        }
    }

/**根據屏幕的寬度和顯示的個數,設置item的寬度*/
private void measureInit(){
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ll_1.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_1.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_2.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_2.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_3.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_3.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_4.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_4.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_5.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_5.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_6.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_6.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_7.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_7.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_8.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_8.setLayoutParams(lp);
    lp = (LinearLayout.LayoutParams)ll_9.getLayoutParams();
    lp.width = ITEM_WIDTH;
    ll_9.setLayoutParams(lp);
}

5、點擊後執行動畫

  上面的步驟完成之後,基本上已經完成的控件的初始設置,接下來就是點擊子控件後執行動畫了。這裏一共需要執行兩套動畫,一套是縮放動畫、一套是移動動畫。添加點擊事件的代碼就不貼出來了,下面是執行動畫的方法:

private void startAnimation(final WEEKDAY centerWitch, final LinearLayout llClickView) {

        if(centerWitch==centerNow)
            return;
        animalFinish = false;

        LinearLayout innerLL = (LinearLayout)ll3.getChildAt(0);
        TextView tvDate = (TextView)innerLL.getChildAt(1);
        tvDate.setVisibility(View.GONE);
        innerLL = (LinearLayout)llClickView.getChildAt(0);
        tvDate = (TextView)innerLL.getChildAt(1);
        tvDate.setVisibility(View.VISIBLE);

        //根據當前中間位置顯示的 和 被點擊的日期,獲取需要偏移的增量
        int offset = getXOffset(centerWitch);
        Log.d(TAG, "當前中間爲"+centerNow+",點擊的是"+centerWitch+ "  偏移量:"+offset);

        //當前中間位置的需要縮放到原尺寸
//        Log.v(TAG, "中間item縮放量scaleX="+ll3.getChildAt(0).getScaleX()+" scaleY="+ll3.getChildAt(0).getScaleY());
        ObjectAnimator anim100 = ObjectAnimator.ofFloat(ll3.getChildAt(0), "scaleX", ll3.getChildAt(0).getScaleX(), 1.0f);
        ObjectAnimator anim101 = ObjectAnimator.ofFloat(ll3.getChildAt(0), "scaleY", ll3.getChildAt(0).getScaleY(), 1.0f);
        //被點擊的需要放大
        ObjectAnimator anim102 = ObjectAnimator.ofFloat(llClickView.getChildAt(0), "scaleX", 1, scaleSize);
        ObjectAnimator anim103 = ObjectAnimator.ofFloat(llClickView.getChildAt(0), "scaleY", 1, scaleSize);

        //透明度動畫
        ObjectAnimator anim104 = ObjectAnimator.ofFloat(llClickView.getChildAt(0), "scaleY", 1, scaleSize);

        //位移動畫
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(ll_1, "X", ll_1.getX(), ll_1.getX() + offset);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(ll_2, "X", ll_2.getX(), ll_2.getX() + offset);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(ll_3, "X", ll_3.getX(), ll_3.getX() + offset);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(ll_4, "X", ll_4.getX(), ll_4.getX() + offset);
        ObjectAnimator anim5 = ObjectAnimator.ofFloat(ll_5, "X", ll_5.getX(), ll_5.getX() + offset);
        ObjectAnimator anim6 = ObjectAnimator.ofFloat(ll_6, "X", ll_6.getX(), ll_6.getX() + offset);
        ObjectAnimator anim7 = ObjectAnimator.ofFloat(ll_7, "X", ll_7.getX(), ll_7.getX() + offset);
        ObjectAnimator anim8 = ObjectAnimator.ofFloat(ll_8, "X", ll_8.getX(), ll_8.getX() + offset);
        ObjectAnimator anim9 = ObjectAnimator.ofFloat(ll_9, "X", ll_9.getX(), ll_9.getX() + offset);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(durationTime);
        animSet.playTogether(anim100, anim101, anim102, anim103, anim1, anim2, anim3, anim4, anim5, anim6, anim7, anim8, anim9);
        animSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
            @Override
            public void onAnimationEnd(Animator animation) {

                Log.w(TAG, "動畫結束後位置:"+ll_1.getX()+" "+ll_2.getX()+" "+ll_3.getX()+" "
                        +ll_4.getX()+" "+ll_5.getX()+" "+ll_6.getX()
                        +" "+ll_7.getX()+" "+ll_8.getX()+" "+ll_9.getX());

                setCenter(centerWitch);

                animalFinish = true;
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animSet.start();
    }

7、重置預備控件

  動畫執行完畢之後,要做兩個操作。第一個就是將當前不在顯示範圍的4個子控件左右各放2個,第二步就是爲預備控件綁定新的星期日期。下面的方法是根據各個子控件的座標來判斷是隱藏還是展示,然後爲隱藏的控件重新安排位置;然後綁定數據:

/*這些引用代表當前正在顯示的5個條目 和 四個預備條目,
     *由於ll_x系列條目是不斷移動的,所以此處需要根據ll_x的位置重新爲llx賦值
     * 其中ll1-ll5爲當前正在顯示的條目, ll6、ll7爲右邊隱藏的預備條目, ll8、ll9爲左邊的隱藏預備條目
     */
    private LinearLayout ll1, ll2, ll3, ll4, ll5, ll6, ll7, ll8, ll9;
    /**1、找到正在展示的五個item,並將預備item復位*/
    private void setCenter(WEEKDAY weekDay){
        //記錄當前顯示在中間的星期X
        centerNow = weekDay;
        //1、找到當前顯示的5個條目的位置
        List<LinearLayout> list = new ArrayList<>(llList);
        for(int i = 0; i<5; i++){
            for(int j = 0; j<list.size(); j++){
                LinearLayout ll = list.get(j);
                if(ll.getX()==ITEM_WIDTH*i){
                    list.remove(ll);      //找到之後就remove可以減少後面遍歷的次數
//                    Log.d(TAG, "找到"+i+"了"+ll);
                    switch (i) {
                        case 0:
                            ll1 = ll;
                            break;
                        case 1:
                            ll2 = ll;
                            break;
                        case 2:
                            ll3 = ll;
                            //當前中間位置item放大
                            ll3.getChildAt(0).setScaleX(scaleSize);
                            ll3.getChildAt(0).setScaleY(scaleSize);
                            break;
                        case 3:
                            ll4 = ll;
                            break;
                        case 4:
                            ll5 = ll;
                            break;
                    }
                }
            }
        }
        Log.i(TAG, "找完後還剩"+list.size()+"  總:"+llList.size());
        //2、剩餘的四個作爲預備,歸位,左邊隱藏兩個,右邊隱藏兩個
        for(int i = 0; i<list.size(); i++){
            LinearLayout ll = list.get(i);
            switch (i){
                case 0:   //左1
                    ll.setX(-ITEM_WIDTH*2);
                    ll8=ll;
                    break;
                case 1:   //左2
                    ll.setX(-ITEM_WIDTH*1);
                    ll9=ll;
                    break;
                case 2:   //右1
                    ll.setX(ITEM_WIDTH*5);
                    ll6=ll;
                    break;
                case 3:   //右2
                    ll.setX(ITEM_WIDTH*6);
                    ll7=ll;
                    break;
            }
        }
        //綁定數據
        reBoundDataByCenter(weekDay);
    }

    /**2、重新綁定數據*/
    private void reBoundDataByCenter(WEEKDAY weekDay){
        if(weekDay == WEEKDAY.wk1){
            /*星期1在中間,依次爲4、5、6、7、1、2、3、4、5*/
            setLLText(ll8, 4, false);
            setLLText(ll9, 5, false);
            setLLText(ll1, 6, false);
            setLLText(ll2, 7, false);
            setLLText(ll3, 1, true);
            setLLText(ll4, 2, false);
            setLLText(ll5, 3, false);
            setLLText(ll6, 4, false);
            setLLText(ll7, 5, false);
        }else if(weekDay == WEEKDAY.wk2){
            /*星期2在中間,依次爲5、6、7、1、2、3、4、5、6*/
            setLLText(ll8, 5, false);
            setLLText(ll9, 6, false);
            setLLText(ll1, 7, false);
            setLLText(ll2, 1, false);
            setLLText(ll3, 2, true);
            setLLText(ll4, 3, false);
            setLLText(ll5, 4, false);
            setLLText(ll6, 5, false);
            setLLText(ll7, 6, false);
        }else if(weekDay == WEEKDAY.wk3){
            /*星期3在中間,依次爲6、7、1、2、3、4、5、6、7*/
            setLLText(ll8, 6, false);
            setLLText(ll9, 7, false);
            setLLText(ll1, 1, false);
            setLLText(ll2, 2, false);
            setLLText(ll3, 3, true);
            setLLText(ll4, 4, false);
            setLLText(ll5, 5, false);
            setLLText(ll6, 6, false);
            setLLText(ll7, 7, false);
        }else if(weekDay == WEEKDAY.wk4){
            /*星期4在中間,依次爲7、1、2、3、4、5、6、7、1*/
            setLLText(ll8, 7, false);
            setLLText(ll9, 1, false);
            setLLText(ll1, 2, false);
            setLLText(ll2, 3, false);
            setLLText(ll3, 4, true);
            setLLText(ll4, 5, false);
            setLLText(ll5, 6, false);
            setLLText(ll6, 7, false);
            setLLText(ll7, 1, false);
        }else if(weekDay == WEEKDAY.wk5){
            /*星期5在中間,依次爲1、2、3、4、5、6、7、1、2*/
            setLLText(ll8, 1, false);
            setLLText(ll9, 2, false);
            setLLText(ll1, 3, false);
            setLLText(ll2, 4, false);
            setLLText(ll3, 5, true);
            setLLText(ll4, 6, false);
            setLLText(ll5, 7, false);
            setLLText(ll6, 1, false);
            setLLText(ll7, 2, false);
        }else if(weekDay == WEEKDAY.wk6){
            /*星期6在中間,依次爲2、3、4、5、6、7、1、2、3*/
            setLLText(ll8, 2, false);
            setLLText(ll9, 3, false);
            setLLText(ll1, 4, false);
            setLLText(ll2, 5, false);
            setLLText(ll3, 6, true);
            setLLText(ll4, 7, false);
            setLLText(ll5, 1, false);
            setLLText(ll6, 2, false);
            setLLText(ll7, 3, false);
        }else if(weekDay == WEEKDAY.wk7){
            /*星期7在中間,依次爲3、4、5、6、7、1、2、3、4*/
            setLLText(ll8, 3, false);
            setLLText(ll9, 4, false);
            setLLText(ll1, 5, false);
            setLLText(ll2, 6, false);
            setLLText(ll3, 7, true);
            setLLText(ll4, 1, false);
            setLLText(ll5, 2, false);
            setLLText(ll6, 3, false);
            setLLText(ll7, 4, false);
        }
    }

    private void setLLText(LinearLayout ll, int witchDay, boolean showDate){
        ll.setTag(witchDay);   //便於區分點擊事件
        LinearLayout innerLL = (LinearLayout)ll.getChildAt(0);
        TextView tv = (TextView)innerLL.getChildAt(0);
        String text = "星期" + WEEK_STR[witchDay];
        tv.setText(text);

        TextView tvDate = (TextView)innerLL.getChildAt(1);
        text = DATE_STR.get(witchDay);
        tvDate.setText(text);
        if(showDate){
            tvDate.setVisibility(View.VISIBLE);
        }else{
            tvDate.setVisibility(View.GONE);
        }
    }

  當執行完上面的方法後,控件的狀態又回到了初始狀態,下次點擊又會重複上面的步驟。因爲動畫執行完後,只有被隱藏的子控件需要重新安排位置,所以,不會出現閃跳的現象。

最終效果圖如下:

      這裏寫圖片描述

喜歡請點贊,no愛請勿噴~O(∩_∩)O謝謝

##源碼下載:

http://download.csdn.net/detail/u010163442/9698848 CSDN下載平臺太流氓

https://github.com/openXu/JueAnimation

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章