月经计算工具

来源:互联网 发布:用户生命周期 算法 编辑:程序博客网 时间:2024/04/20 21:28

项目中突然需要由经期推算需求,在搜索到的代码,和现在的需求有点出入,经过几次的修改,一直出问题,所以仿照已有的代码,重新写一份。

设计到的知识点

  • 日期的计算(Calendar)
  • 绘制日历
  • 对日历中的日期添加状态(安全期、月经期和排卵期)

Calendar

Android中用到时间的时候,很有可能用到的类。

获取一些基本的时间
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR); //获取当前年份
mMonth = c.get(Calendar.MONTH);//获取当前月份
mDay = c.get(Calendar.DAY_OF_MONTH);//获取当前月份的日期号码
mHour = c.get(Calendar.HOUR_OF_DAY);//获取当前的小时数
mMinute = c.get(Calendar.MINUTE);//获取当前的分钟数

其他的方法介绍

  • getTime:获取到Date的实例
  • setTime:设置时间
  • add:在当前的时间上增加/减少时间(这里是本节的重点)

绘制日历

首先绘制日期卡片(具体一天的界面)

public class DateCardView extends FrameLayout{    private TextView mDateNumText;      //当月的几号    private TextView mTodayText;        //今天    private ImageView mIconImage;       //标志开始和结束的图标    private LinearLayout mRootView;     //根视图    private LinearLayout mStateView;    //显示状态的图片    public DateCardView(Context context){        super(context);        initView();    }    private void initView(){        //添加根视图        mRootView = new LinearLayout(getContext());        mRootView.setOrientation(LinearLayout.VERTICAL);        mRootView.setBackgroundResource(R.drawable.period_safe_date_view_bg);        LayoutParams rootParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);        rootParams.setMargins(5,5,0,0);        mRootView.setLayoutParams(rootParams);        mRootView.setPadding(1,1,1,1);        addView(mRootView);        //添加状态的图片        mStateView = new LinearLayout(getContext());        mStateView.setOrientation(LinearLayout.VERTICAL);        mStateView.setBackgroundResource(R.drawable.period_safe_date_view_bg);        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);        mStateView.setLayoutParams(lp);        mRootView.addView(mStateView, lp);        //显示日期        mDateNumText = new TextView(getContext());        mDateNumText.setTextSize(14);        mDateNumText.setTextColor(getResources().getColor(R.color.period_card_text_state_safe));        mDateNumText.setPadding(5, 0, 0, 0);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);        mStateView.addView(mDateNumText, params);        //添加底部的开始和今天的父布局        LinearLayout bottomLayout = new LinearLayout(getContext());        bottomLayout.setOrientation(LinearLayout.HORIZONTAL);        LayoutParams bottomParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);        bottomLayout.setPadding(5, 0, 0, 0);        bottomLayout.setGravity(Gravity.CENTER_VERTICAL);        bottomLayout.setLayoutParams(bottomParams);        mStateView.addView(bottomLayout);        //显示姨妈来了、走了图标        mIconImage = new ImageView(getContext());        LayoutParams ivParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        mIconImage.setLayoutParams(ivParams);        mIconImage.setVisibility(View.GONE);        bottomLayout.addView(mIconImage);        //显示今天        mTodayText = new TextView(getContext());        LayoutParams tvParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);        mTodayText.setLayoutParams(tvParams);        mTodayText.setTextColor(getResources().getColor(R.color.period_card_text_state_safe));        mTodayText.setTextSize(12);        mTodayText.setText("今天");        mTodayText.setVisibility(View.GONE);        mTodayText.setGravity(Gravity.RIGHT);        bottomLayout.addView(mTodayText);    }    /**     * 初始化日期     * @param dateCardModule     */    public void initData(DateCardModule dateCardModule){        mDateNumText.setText(dateCardModule.date+"");//几号        setToMonth(dateCardModule.istoMonth);        setToDay(dateCardModule.isToday,dateCardModule.type);        setStart(dateCardModule.isStart);        setType(dateCardModule.type);    }    /**     * 是否显示内容(是否是这个月的日期)     * @param dayInMonth     */    private void setToMonth(boolean dayInMonth){        if (dayInMonth) {            mIconImage.setVisibility(View.VISIBLE);            mDateNumText.setVisibility(View.VISIBLE);            mTodayText.setVisibility(View.VISIBLE);        } else {            mIconImage.setVisibility(View.GONE);            mDateNumText.setVisibility(View.GONE);            mTodayText.setVisibility(View.GONE);        }    }    /**     * 今天     * @param isToday     * @param type 不同的状态     */    private void setToDay(boolean isToday,PeriodType type){        if (isToday) {            mTodayText.setVisibility(View.VISIBLE);        } else {            mTodayText.setVisibility(View.GONE);        }        if (type == PeriodType.TYPE_MENSTRUATION || type == PeriodType.TYPE_CALCULATE) {            mTodayText.setTextColor(Color.WHITE);        } else {            mTodayText.setTextColor(getResources().getColor(R.color.period_card_text_state_safe));        }    }    /**     * @param dayType 1表示开始 2表示结束     */    private void setStart(int dayType){        if (dayType == 1) {            mIconImage.setVisibility(View.VISIBLE);            mIconImage.setImageResource(R.drawable.period_begin);        } else if (dayType == 2) {            mIconImage.setVisibility(View.VISIBLE);            mIconImage.setImageResource(R.drawable.period_end);        } else {            mIconImage.setVisibility(View.GONE);        }    }    private void setType(PeriodType type){        if(type==PeriodType.TYPE_MENSTRUATION){            //经期            mDateNumText.setTextColor(getResources().getColor(R.color.period_card_text_state_menstruation));            mStateView.setBackgroundResource(R.drawable.period_menstruation_date_view_bg);        }else if(type==PeriodType.TYPE_CALCULATE){            //计算后处于经期            mDateNumText.setTextColor(getResources().getColor(R.color.period_card_text_state_calculate));            mStateView.setBackgroundResource(R.drawable.period_calculate_date_view_bg);        }else if(type==PeriodType.TYPE_SAFE){            //计算后处于安全期            mDateNumText.setTextColor(getResources().getColor(R.color.period_card_text_state_safe));            mStateView.setBackgroundResource(R.drawable.period_safe_date_view_bg);        }else if(type == PeriodType.TYPE_DANGEROUS){            //计算后处于排卵期            mDateNumText.setTextColor(getResources().getColor(R.color.period_card_text_state_dangerous));            mStateView.setBackgroundResource(R.drawable.period_dangerous_date_view_bg);        }    }

绘制日历的核心方法

@Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        ;        if (mNeedInitCards) {            mNeedInitCards = false;            int cardSize = w / COLUM_NUM;            addCards(cardSize);        }    }/**     * 添加日期     *     * @param cardSize 日期布局的大小(正方形)     */    private void addCards(int cardSize) {        DateCardView dateCardView;        int dayCount = 0;        for (int index = 0; index < TOTAL_NUM / COLUM_NUM; index++) {            for (int cloum = 0; cloum < COLUM_NUM; cloum++) {                dateCardView = new DateCardView(getContext());                dateCardView.initData(mDateCardList[dayCount]);                addView(dateCardView, cardSize, cardSize);                mDateCard[dayCount] = dateCardView;                dayCount++;            }        }    }
0 0
原创粉丝点击