月经计算工具
来源:互联网 发布:用户生命周期 算法 编辑:程序博客网 时间:2024/06/08 13:50
项目中突然需要由经期推算需求,在搜索到的代码,和现在的需求有点出入,经过几次的修改,一直出问题,所以仿照已有的代码,重新写一份。
设计到的知识点
- 日期的计算(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
- 月经计算工具
- 规范化好月经
- 陶子的月经论述
- 陶子的月经论述
- 月经论-陶晶莹
- 时间计算工具类
- 在线矩阵计算工具
- Java计算工具类
- DM8168 DMM_LISA_MAP计算工具
- 最早的计算工具
- 求blowfish计算工具
- 时间计算工具
- 个人所得税计算工具
- 精确计算工具
- 并行计算工具OpenMP
- 科学计算工具-ipython
- 价格计算工具类
- 十大云计算工具
- EMMC 介绍
- 继承中的多态性
- Spring 表达式语言:SpEL
- uva 10815
- HDU 2952 Counting Sheep ( DFS + BFS )
- 月经计算工具
- HttpURLConnection_Handler网络请求数据5
- IDEA 2016与eclipse 4.5.1打运行jar包教程及打包失败的原因
- Application.persistentDataPath
- 这个maven 的测试错误 哪位大神帮忙看看 谢了
- Thinking in Java——第三章-操作符
- Android Notification 用法的4种形式
- c语言学习----运算符与表达式
- Html初识整理