關於項目的一點點總結,簽到返利日曆的實現

突然發現之前已經有兩篇博客了,最近開發了兩個新項目,10.1之前上線,所以一直都很忙,新項目用到了幾個知識點,所以在此總結一下,
首先做了一個簽到返利效果的,在網上找了蠻多的demo,但是沒有合適的,主要看了兩種
我接觸的兩種方式
1.通過GridView 去寫一個日曆
2.通過自定義去畫一個日曆
最後我選了自定義View 不管哪一種,都需要用到DateUtils,獲取年月日

(這裏因爲是測試庫的數據,所以簽到金額UI顯示有問題,但是線上庫的數據不是100,是0.1,0.2這樣的數字,所以文字上偏移不會太大)
這裏寫圖片描述


public class DateUtils {
    /**
     * 通過年份和月份 得到當月的日子
     * 
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: 
            return 31;
        case 4:
        case 6:
        case 9:
        case 11: 
            return 30;
        case 2:
            if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
                return 29;
            }else{
                return 28;
            }
        default:
            return  -1;
        }
    }
    /**
     * 返回當前月份1號位於周幾
     * @param year
     *      年份
     * @param month
     *      月份,傳入系統獲取的,不需要正常的
     * @return
     *  日:1     一:2     二:3     三:4     四:5     五:6     六:7
     */
    public static int getFirstDayWeek(int year, int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

}

然後就是在代碼中畫出來自己想要的佈局
這裏是先通過一個循環遍歷取出一個月中的天,給每一天設置在Canvas中顯示的位置,(關於自定義View,推薦扔物線大神寫的一系列文章 http://hencoder.com/ui-1-5/)

這裏寫代碼片  只給出核心代碼
public class MonthDateView extends View {
     @Override
    protected void onDraw(Canvas canvas) {

        initSize();
        daysString = new int[6][7];

        mPaint.setTextSize(mDaySize * mDisplayMetrics.scaledDensity);
        String dayString;
        //日 數量 30
        int mMonthDays = DateUtils.getMonthDays(mSelYear, mSelMonth);
        //獲取當前第一天 是第幾周
        int weekNumber = DateUtils.getFirstDayWeek(mSelYear, mSelMonth);
        Log.d("DateView", "DateView:" + mSelMonth + "月1號周" + weekNumber);

        //這是畫每一天
        for (int day = 0; day < mMonthDays; day++) {
            //dayString 就是當前date的Int 值
            dayString = (day + 1) + "";
            int column = (day + weekNumber - 1) % 7;
            int row = (day + weekNumber - 1) / 7;
            //畫每一天
            daysString[row][column] = day + 1;
            int startX = (int) (mColumnSize * column + (mColumnSize - mPaint.measureText(dayString)) / 2);
            int startY = (int) (mRowSize * row + mRowSize / 2 - (mPaint.ascent() + mPaint.descent()) / 2);
            if (dayString.equals(mSelDay + "")) {
                //畫 選中每一天的背景樣式
                float startCirX = (float) (mColumnSize * column + mColumnSize / 2);
                float startCirY = (float) (mRowSize * row + mRowSize / 2);
                mPaint.setColor(mCircleColor);
                mPaint.setAntiAlias(true);
                canvas.drawCircle(startCirX, startCirY, 40, mPaint);
                //記錄第幾行,即第幾周
                weekRow = row + 1;
            }


            //畫有返利金額天數的背景樣式 以及下面的 樣式  畫了很多次造成這樣子的UI效果


//            drawCircle(startX, startY, row,day, canvas, dayMoney);
            drawCircle(row, column, day, canvas, dayMoney);


            if (dayString.equals(mSelDay + "")) {
                mPaint.setColor(mSelectDayColor);
            } else if (dayString.equals(mCurrDay + "") && mCurrDay != mSelDay && mCurrMonth == mSelMonth) {
                //正常月,選中其他日期,則今日爲紅色
                mPaint.setColor(mCurrentColor);
            } else {
                mPaint.setColor(mDayColor);
            }
            canvas.drawText(dayString, startX, startY, mPaint);
            if (tv_date != null) {
                tv_date.setText(mSelYear + "年" + (mSelMonth + 1) + "月");
            }

            if (tv_week != null) {
                tv_week.setText("第" + weekRow + "周");
            }
        }

    }



}

因爲我們還需要設置一些簽到之後顯示的效果,所以把顯示的效果在
drawCircle(int row, int column, int day, Canvas canvas, Map

 public void drawCircle(int row, int column, int day, Canvas canvas, Map<Integer, String> textMoney) {
        if (daysHasThingList != null && daysHasThingList.size() > 0) {
            //包含此天!!!!!!!!
            if (!daysHasThingList.contains(day)) return;
            if (getmSelMonth() == mCurrMonth) {

                Resources resources = getResources();

                float startCirX = (float) (mColumnSize * column + mColumnSize / 2);
                float startCirY = (float) (mRowSize * row + mRowSize / 2);


                //天數 背景 圓環  mColumnSize = 97 固定值
                int newColumn = mColumnSize * column;

                int newRow = mRowSize * row;
                Bitmap bitmap2 = BitmapFactory.decodeResource(resources, myDrawableSHape);
                int mBitWidth = bitmap2.getWidth();
                int mBitHeight = bitmap2.getHeight();
//
//                float shapeX = (float) (newColumn+ mColumnSize*0.17 );
//                float shapey = (float) (newRow +mRowSize*0.18 );
                float shapeX = (float) (newColumn+mBitWidth*0.28 );
                float shapey = (float) (newRow +mBitHeight/4 );
                mPaint.setAntiAlias(true);
                canvas.drawBitmap(bitmap2, shapeX, shapey, mPaint);


                /**
                 *TODO 重新畫 很重要
                 */

//                int mBitWidth = bitmap2.getWidth();
//                int mBitHeight = bitmap2.getHeight();
//                int roundleft = newColumn;
//                int roundtop = newRow ;
//                mSrcRect = new Rect(roundleft, roundtop, mBitWidth, mBitHeight);
//                mDestRect = new Rect(roundleft, roundtop, mBitWidth, mBitHeight);
//                canvas.drawBitmap(bitmap2, mSrcRect, mDestRect, mPaint);


                //金額背景
                float circleX = (float) (mColumnSize * column + mColumnSize * 0.05);
                float circley = (float) (mRowSize * row + mRowSize * 0.7);
                mPaint.setAntiAlias(true);

                Bitmap bitmap = BitmapFactory.decodeResource(resources, myDrawable);
                canvas.drawBitmap(bitmap, circleX, circley, mPaint);
                //金額
                String money = textMoney.get(day);

//                float textY = (float) circley + mRowSize / 3;

                Paint paint1 = new Paint();
                paint1.setColor(mMoneyColor);
                paint1.setAntiAlias(true);
                paint1.setTextSize((mDaySize * mDisplayMetrics.scaledDensity / 3) * 2);
//                float textX =   (float) (mColumnSize * column + (mColumnSize - paint1.measureText(String.valueOf(day))) / 2);;
                float textX = (float) newColumn +mColumnSize/4;
                float textY = (float) ((float) circley +(float) mColumnSize *0.3);
                String newmoney ="+" + money + "元";
                canvas.drawText(newmoney, textX, textY, paint1);
            }
        }
    }

主要功能代碼大概就是這樣,現在貼出來全部代碼

public class MonthDateView extends View {
    private static final int NUM_COLUMNS = 7;
    private static final int NUM_ROWS = 6;
    private Paint mPaint;
    private int mDayColor = Color.parseColor("#000000");
    private int mSelectDayColor = Color.parseColor("#ffffff");
    private int mSelectBGColor = Color.parseColor("#1FC2F3");
    private int mCurrentColor = Color.parseColor("#ff0000");
    private int mCurrYear, mCurrMonth, mCurrDay;
    private int mSelYear, mSelMonth, mSelDay;
    private int mColumnSize, mRowSize;
    private DisplayMetrics mDisplayMetrics;
    private int mDaySize = 16;
    private TextView tv_date, tv_week;
    private int weekRow;
    private int[][] daysString;
    private int mCircleRadius = 6;
    private DateClick dateClick;
    private int mCircleColor = Color.parseColor("#ff0000");
    private int mTextColor = Color.parseColor("#1d1d1d");
//    private int mMoneyColor = Color.parseColor("#1d1d1d");
    private int mMoneyColor = Color.parseColor("#ffffff");


    private List<Integer> daysHasThingList;
    private Map<Integer, String> dayMoney;
    private int myDrawable = R.drawable.signin_calendar_moneybg;
    private int myDrawableSHape = R.drawable.signin_calendar_round;

    private int dateTag = 0;

    public MonthDateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //density = 2.0
        mDisplayMetrics = getResources().getDisplayMetrics();
        Calendar calendar = Calendar.getInstance();
        mPaint = new Paint();
        mCurrYear = calendar.get(Calendar.YEAR);
        mCurrMonth = calendar.get(Calendar.MONTH);
        mCurrDay = calendar.get(Calendar.DATE);
        setSelectYearMonth(mCurrYear, mCurrMonth, mCurrDay);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        initSize();
        daysString = new int[6][7];

        mPaint.setTextSize(mDaySize * mDisplayMetrics.scaledDensity);
        String dayString;
        //日 數量 30
        int mMonthDays = DateUtils.getMonthDays(mSelYear, mSelMonth);
        //獲取當前第一天 是第幾周
        int weekNumber = DateUtils.getFirstDayWeek(mSelYear, mSelMonth);
        Log.d("DateView", "DateView:" + mSelMonth + "月1號周" + weekNumber);

        //這是畫每一天
        for (int day = 0; day < mMonthDays; day++) {
            //dayString 就是當前date的Int 值
            dayString = (day + 1) + "";
            int column = (day + weekNumber - 1) % 7;
            int row = (day + weekNumber - 1) / 7;
            //畫每一天
            daysString[row][column] = day + 1;
            int startX = (int) (mColumnSize * column + (mColumnSize - mPaint.measureText(dayString)) / 2);
            int startY = (int) (mRowSize * row + mRowSize / 2 - (mPaint.ascent() + mPaint.descent()) / 2);
            if (dayString.equals(mSelDay + "")) {
                //畫 選中每一天的背景樣式
                float startCirX = (float) (mColumnSize * column + mColumnSize / 2);
                float startCirY = (float) (mRowSize * row + mRowSize / 2);
                mPaint.setColor(mCircleColor);
                mPaint.setAntiAlias(true);
                canvas.drawCircle(startCirX, startCirY, 40, mPaint);
                //記錄第幾行,即第幾周
                weekRow = row + 1;
            }


            //畫有返利金額天數的背景樣式 以及下面的 樣式  畫了很多次造成這樣子的UI效果


//            drawCircle(startX, startY, row,day, canvas, dayMoney);
            drawCircle(row, column, day, canvas, dayMoney);


            if (dayString.equals(mSelDay + "")) {
                mPaint.setColor(mSelectDayColor);
            } else if (dayString.equals(mCurrDay + "") && mCurrDay != mSelDay && mCurrMonth == mSelMonth) {
                //正常月,選中其他日期,則今日爲紅色
                mPaint.setColor(mCurrentColor);
            } else {
                mPaint.setColor(mDayColor);
            }
            canvas.drawText(dayString, startX, startY, mPaint);
            if (tv_date != null) {
                tv_date.setText(mSelYear + "年" + (mSelMonth + 1) + "月");
            }

            if (tv_week != null) {
                tv_week.setText("第" + weekRow + "周");
            }
        }

    }

    private Rect mSrcRect, mDestRect;

    public void drawCircle(int row, int column, int day, Canvas canvas, Map<Integer, String> textMoney) {
        if (daysHasThingList != null && daysHasThingList.size() > 0) {
            //包含此天!!!!!!!!
            if (!daysHasThingList.contains(day)) return;
            if (getmSelMonth() == mCurrMonth) {

                Resources resources = getResources();

                float startCirX = (float) (mColumnSize * column + mColumnSize / 2);
                float startCirY = (float) (mRowSize * row + mRowSize / 2);


                //天數 背景 圓環  mColumnSize = 97 固定值
                int newColumn = mColumnSize * column;

                int newRow = mRowSize * row;
                Bitmap bitmap2 = BitmapFactory.decodeResource(resources, myDrawableSHape);
                int mBitWidth = bitmap2.getWidth();
                int mBitHeight = bitmap2.getHeight();
//
//                float shapeX = (float) (newColumn+ mColumnSize*0.17 );
//                float shapey = (float) (newRow +mRowSize*0.18 );
                float shapeX = (float) (newColumn+mBitWidth*0.28 );
                float shapey = (float) (newRow +mBitHeight/4 );
                mPaint.setAntiAlias(true);
                canvas.drawBitmap(bitmap2, shapeX, shapey, mPaint);


                /**
                 *TODO 重新畫 很重要
                 */

//                int mBitWidth = bitmap2.getWidth();
//                int mBitHeight = bitmap2.getHeight();
//                int roundleft = newColumn;
//                int roundtop = newRow ;
//                mSrcRect = new Rect(roundleft, roundtop, mBitWidth, mBitHeight);
//                mDestRect = new Rect(roundleft, roundtop, mBitWidth, mBitHeight);
//                canvas.drawBitmap(bitmap2, mSrcRect, mDestRect, mPaint);


                //金額背景
                float circleX = (float) (mColumnSize * column + mColumnSize * 0.05);
                float circley = (float) (mRowSize * row + mRowSize * 0.7);
                mPaint.setAntiAlias(true);

                Bitmap bitmap = BitmapFactory.decodeResource(resources, myDrawable);
                canvas.drawBitmap(bitmap, circleX, circley, mPaint);
                //金額
                String money = textMoney.get(day);

//                float textY = (float) circley + mRowSize / 3;

                Paint paint1 = new Paint();
                paint1.setColor(mMoneyColor);
                paint1.setAntiAlias(true);
                paint1.setTextSize((mDaySize * mDisplayMetrics.scaledDensity / 3) * 2);
//                float textX =   (float) (mColumnSize * column + (mColumnSize - paint1.measureText(String.valueOf(day))) / 2);;
                float textX = (float) newColumn +mColumnSize/4;
                float textY = (float) ((float) circley +(float) mColumnSize *0.3);
                String newmoney ="+" + money + "元";
                canvas.drawText(newmoney, textX, textY, paint1);
            }
        }
    }
    @Override
    public boolean performClick() {
        return super.performClick();
    }

    private int downX = 0, downY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventCode = event.getAction();
        switch (eventCode) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                downY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                int upX = (int) event.getX();
                int upY = (int) event.getY();
                if (Math.abs(upX - downX) < 10 && Math.abs(upY - downY) < 10) {//點擊事件
                    performClick();
                    doClickAction((upX + downX) / 2, (upY + downY) / 2);
                }
                break;
        }
        return true;
    }

    /**
     * 初始化列寬行高
     */
    private void initSize() {
        mColumnSize = getWidth() / NUM_COLUMNS;
        mRowSize = getHeight() / NUM_ROWS;
    }

    /**
     * 設置年月
     *
     * @param year
     * @param month
     */
    private void setSelectYearMonth(int year, int month, int day) {
        mSelYear = year;
        mSelMonth = month;
        mSelDay = day;
    }

    /**
     * 執行點擊事件
     *
     * @param x
     * @param y
     */
    private void doClickAction(int x, int y) {
        int row = y / mRowSize;
        int column = x / mColumnSize;
        setSelectYearMonth(mSelYear, mSelMonth, daysString[row][column]);
        invalidate();
        //執行activity發送過來的點擊處理事件
        if (dateClick != null) {
            dateClick.onClickOnDate();
        }
    }

    /**
     * 左點擊,日曆向後翻頁
     */
    public void onLeftClick() {
        int year = mSelYear;
        int month = mSelMonth;
        int day = mSelDay;
        if (month == 0) {//若果是1月份,則變成12月份
            year = mSelYear - 1;
            month = 11;
        } else if (DateUtils.getMonthDays(year, month) == day) {
            //如果當前日期爲該月最後一點,當向前推的時候,就需要改變選中的日期
            month = month - 1;
            day = DateUtils.getMonthDays(year, month);
        } else {
            month = month - 1;
        }
        setSelectYearMonth(year, month, day);
        invalidate();
    }

    /**
     * 右點擊,日曆向前翻頁
     */
    public void onRightClick() {
        int year = mSelYear;
        int month = mSelMonth;
        int day = mSelDay;
        if (month == 11) {//若果是12月份,則變成1月份
            year = mSelYear + 1;
            month = 0;
        } else if (DateUtils.getMonthDays(year, month) == day) {
            //如果當前日期爲該月最後一點,當向前推的時候,就需要改變選中的日期
            month = month + 1;
            day = DateUtils.getMonthDays(year, month);
        } else {
            month = month + 1;
        }
        setSelectYearMonth(year, month, day);
        invalidate();
    }

    /**
     * 獲取選擇的年份
     *
     * @return
     */
    public int getmSelYear() {
        return mSelYear;
    }

    /**
     * 獲取選擇的月份
     *
     * @return
     */
    public int getmSelMonth() {
        return mSelMonth;
    }


    /**
     * 獲取選擇的日期
     *
     * @param
     */
    public int getmSelDay() {
        return this.mSelDay;
    }

    /**
     * 普通日期的字體顏色,默認黑色
     *
     * @param mDayColor
     */
    public void setmDayColor(int mDayColor) {
        this.mDayColor = mDayColor;
    }

    /**
     * 選擇日期的顏色,默認爲白色
     *
     * @param mSelectDayColor
     */
    public void setmSelectDayColor(int mSelectDayColor) {
        this.mSelectDayColor = mSelectDayColor;
    }

    /**
     * 選中日期的背景顏色,默認藍色
     *
     * @param mSelectBGColor
     */
    public void setmSelectBGColor(int mSelectBGColor) {
        this.mSelectBGColor = mSelectBGColor;
    }

    /**
     * 當前日期不是選中的顏色,默認紅色
     *
     * @param mCurrentColor
     */
    public void setmCurrentColor(int mCurrentColor) {
        this.mCurrentColor = mCurrentColor;
    }

    /**
     * 日期的大小,默認18sp
     *
     * @param mDaySize
     */
    public void setmDaySize(int mDaySize) {
        this.mDaySize = mDaySize;
    }

    /**
     * 設置顯示當前日期的控件
     *
     * @param tv_date 顯示日期
     * @param tv_week 顯示周
     */
    public void setTextView(TextView tv_date, TextView tv_week) {
        this.tv_date = tv_date;
        this.tv_week = tv_week;
        invalidate();
    }

    /**
     * 設置事務天數
     *
     * @param daysHasThingList
     */
    public void setDaysHasThingList(List<Integer> daysHasThingList) {
        this.daysHasThingList = daysHasThingList;
    }


    /***
     * 設置圓圈的半徑,默認爲6
     *
     * @param mCircleRadius
     */
    public void setmCircleRadius(int mCircleRadius) {
        this.mCircleRadius = mCircleRadius;
    }

    /**
     * 設置圓圈的半徑
     *
     * @param mCircleColor
     */
    public void setmCircleColor(int mCircleColor) {
        this.mCircleColor = mCircleColor;
    }

    /**
     * 設置日期的點擊回調事件
     *
     * @author shiwei.deng
     */
    public interface DateClick {
        public void onClickOnDate();
    }

    /**
     * 設置日期點擊事件
     *
     * @param dateClick
     */
    public void setDateClick(DateClick dateClick) {
        this.dateClick = dateClick;
    }

    /**
     * 跳轉至今天
     */
    public void setTodayToView() {
        setSelectYearMonth(mCurrYear, mCurrMonth, mCurrDay);
        invalidate();
    }

    /*
    需要日期 需要 金額
     */
    public void setMoney(Map<Integer, String> money) {
        this.dayMoney = money;
    }
}
public class WeekDayView extends View {
    //上橫線顏色
    private int mTopLineColor = Color.parseColor("#CCE4F2");
    //下橫線顏色
    private int mBottomLineColor = Color.parseColor("#CCE4F2");
    //週一到週五的顏色
    private int mWeedayColor = Color.parseColor("#1d1d1d");
    //週六、週日的顏色
    private int mWeekendColor = Color.parseColor("#ff0000");
    //線的寬度
    private int mStrokeWidth = 4;
    private int mWeekSize = 14;
    private Paint paint;
    private DisplayMetrics mDisplayMetrics;
    private String[] weekString = new String[]{"日","一","二","三","四","五","六"};
    public WeekDayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDisplayMetrics = getResources().getDisplayMetrics();
        paint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if(heightMode == MeasureSpec.AT_MOST){
            heightSize = mDisplayMetrics.densityDpi * 30;
        }
        if(widthMode == MeasureSpec.AT_MOST){
            widthSize = mDisplayMetrics.densityDpi * 300;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        //進行畫上下線
        paint.setStyle(Style.STROKE);
        paint.setColor(mTopLineColor);
        paint.setStrokeWidth(mStrokeWidth);
        canvas.drawLine(0, 0, width, 0, paint);

        //畫下橫線
        paint.setColor(mBottomLineColor);
        canvas.drawLine(0, height, width, height, paint);
        paint.setStyle(Style.FILL);
        paint.setTextSize(mWeekSize * mDisplayMetrics.scaledDensity);
        int columnWidth = width / 7;
        for(int i=0;i < weekString.length;i++){
            String text = weekString[i];
            int fontWidth = (int) paint.measureText(text);
            int startX = columnWidth * i + (columnWidth - fontWidth)/2;
            int startY = (int) (height/2 - (paint.ascent() + paint.descent())/2);
            if(text.indexOf("日") > -1|| text.indexOf("六") > -1){
                paint.setColor(mWeekendColor);
            }else{
                paint.setColor(mWeedayColor);
            }
            canvas.drawText(text, startX, startY, paint);
        }
    }

    /**
     * 設置頂線的顏色
     * @param mTopLineColor
     */
    public void setmTopLineColor(int mTopLineColor) {
        this.mTopLineColor = mTopLineColor;
    }

    /**
     * 設置底線的顏色
     * @param mBottomLineColor
     */
    public void setmBottomLineColor(int mBottomLineColor) {
        this.mBottomLineColor = mBottomLineColor;
    }

    /**
     * 設置週一-五的顏色
     * @return
     */
    public void setmWeedayColor(int mWeedayColor) {
        this.mWeedayColor = mWeedayColor;
    }

    /**
     * 設置週六、週日的顏色
     * @param mWeekendColor
     */
    public void setmWeekendColor(int mWeekendColor) {
        this.mWeekendColor = mWeekendColor;
    }

    /**
     * 設置邊線的寬度
     * @param mStrokeWidth
     */
    public void setmStrokeWidth(int mStrokeWidth) {
        this.mStrokeWidth = mStrokeWidth;
    }


    /**
     * 設置字體的大小
     * @param mWeekSize
     */
    public void setmWeekSize(int mWeekSize) {
        this.mWeekSize = mWeekSize;
    }


    /**
     * 設置星期的形式
     * @param weekString
     * 默認值  "日","一","二","三","四","五","六"
     */
    public void setWeekString(String[] weekString) {
        this.weekString = weekString;
    }
}

至於在代碼中調用 因爲需要和服務器對接數據,服務器回傳過來日期金額以及是否簽到狀態

 List<Integer> listDate = new ArrayList<Integer>();
    Map<Integer, String> listMoney = new HashMap<>();
  /**
     * 獲取網絡數據  簽到日期 返利金額
     *
     * @param response
     */
    private void processTaskData(String response) {
        Type collectionType = new TypeToken<List<DataPOJO>>() {
        }.getType();
        data = (List<DataPOJO>) new Gson()
                .fromJson(response, collectionType);
        list = new ArrayList<>();
        listDate.clear();
        listMoney.clear();
        //獲取服務器數據
        for (int i = 0; i < data.size(); i++) {
            String date = data.get(i).date;
            String money = data.get(i).money;
            String status = data.get(i).status;
            if (status.equals("0")) {
                listDate.add(i);
                listMoney.put(i, money);
            }
        }
        //設置簽到顯示
        monthDateView.setDaysHasThingList(listDate);
        monthDateView.setMoney(listMoney);
        ProgressBarUtil.dismissProgress(getActivity());
    };

這樣大概就是整個簽到返利效果的日曆了

下面是佈局的部分代碼 ,因爲借鑑網上的,所以修改了一下

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main12"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/standard_white"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@drawable/signin_bg">

        <RelativeLayout
            android:id="@+id/task_main_click"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/commen_30dp"
            android:background="@drawable/signin_menu_nor">

            <TextView
                android:id="@+id/task_main_profit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/commen_50dp"
                android:text="點擊簽到"
                android:textColor="@color/standard_red"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/task_main_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/commen_70dp"
                android:text="未登錄"
                android:textColor="#ffa478"
                android:textSize="11sp" />

        </RelativeLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/commen_10dp"
            android:gravity="center_horizontal"
            android:text="連續打卡天數越多,打卡獎金越高噢!"
            android:textColor="#99ffffff" />

    </RelativeLayout>
    <!-- 日曆時間選擇欄 -->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="@dimen/commen_30dp"
        android:background="#ffffff"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="75dp"
            android:background="@drawable/signin_arrow_left"
            android:contentDescription="@null" />

        <ImageView
            android:id="@+id/iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="75dp"
            android:background="@drawable/signin_arrow_right"
            android:contentDescription="@null" />

        <LinearLayout
            android:id="@+id/date_operator_ll"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_today"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="5dp"
                android:background="#FFD700"
                android:gravity="center"
                android:text="今"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/date_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text=""
                android:textColor="@color/text_black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/week_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:gravity="center_horizontal"
                android:text=""
                android:textColor="#93C73C"
                android:textSize="20sp"
                android:visibility="gone" />
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="@dimen/commen_30dp"
        android:background="#ffffff"
        android:orientation="vertical">

        <io.dcloud.H53CF40A7.view.WeekDayView
            android:layout_width="match_parent"
            android:layout_height="30dp" />

        <io.dcloud.H53CF40A7.view.MonthDateView
            android:id="@+id/monthDateView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


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