欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义按周签到打卡功能实例代码

程序员文章站 2023-11-28 09:16:10
前言 之前实现过《android可签到的日历控件》的功能,跟这篇一样都是实现签到打卡功能,这篇实现的是按月进行打卡做标识,本篇内容实现的按周进行签到打卡。 实现签到...

前言

之前实现过《android可签到的日历控件》的功能,跟这篇一样都是实现签到打卡功能,这篇实现的是按月进行打卡做标识,本篇内容实现的按周进行签到打卡。

实现签到规则如下:

1、连续签到7天,即可获得额外积分奖励。

2、连续签到记录在第8天开始时将清零重新计算。

3、如果中断签到,连续签到记录也将清零。

实现步骤:

1.效果图

2.自定义签到打卡view

3.主程序逻辑处理

4.主界面

5.签到bean

6.总结

实现过程:

1.效果图

Android自定义按周签到打卡功能实例代码

2.自定义签到打卡view

/**
 * description: 自定义签到view.
 */
public class stepsview extends view {

 /**
 * 动画执行的时间 230毫秒
 */
 private final static int animation_time = 230;
 /**
 * 动画执行的间隔次数
 */
 private final static int animation_interval = 10;

 /**
 * 线段的高度
 */
 private float mcompletedlineheight = calcutils.dp2px(getcontext(), 2f);

 /**
 * 图标宽度
 */
 private float miconwidth = calcutils.dp2px(getcontext(), 21.5f);
 /**
 * 图标的高度
 */
 private float miconheight = calcutils.dp2px(getcontext(), 24f);
 /**
 * up宽度
 */
 private float mupwidth = calcutils.dp2px(getcontext(), 20.5f);
 /**
 * up的高度
 */
 private float mupheight = calcutils.dp2px(getcontext(), 12f);

 /**
 * 线段长度
 */
 private float mlinewidth = calcutils.dp2px(getcontext(), 25f);

 /**
 * 已经完成的图标
 */
 private drawable mcompleteicon;
 /**
 * 正在进行的图标
 */
 private drawable mattentionicon;
 /**
 * 默认的图标
 */
 private drawable mdefaulticon;
 /**
 * up图标
 */
 private drawable mupicon;
 /**
 * 图标中心点y
 */
 private float mcentery;
 /**
 * 线段的左上方的y
 */
 private float mlefty;
 /**
 * 线段的右下方的y
 */
 private float mrighty;

 /**
 * 数据源
 */
 private list<stepbean> mstepbeanlist;
 private int mstepnum = 0;

 /**
 * 图标中心点位置
 */
 private list<float> mcirclecenterpointpositionlist;
 /**
 * 未完成的线段paint
 */
 private paint muncompletedpaint;
 /**
 * 完成的线段paint
 */
 private paint mcompletedpaint;
 /**
 * 未完成颜色
 */
 private int muncompletedlinecolor = contextcompat.getcolor(getcontext(), r.color.c_d5a872);
 /**
 * 积分颜色
 */
 private int muncompletedtextcolor = contextcompat.getcolor(getcontext(), r.color.c_cccccc);

 /**
 * 天数颜色
 */
 private int muncompleteddaytextcolor = contextcompat.getcolor(getcontext(), r.color.c_736657);

 /**
 * up魅力值颜色
 */
 private int mcurrenttextcolor = contextcompat.getcolor(getcontext(), r.color.c_white);
 /**
 * 完成的颜色
 */
 private int mcompletedlinecolor = contextcompat.getcolor(getcontext(), r.color.c_d5a872);

 private paint mtextnumberpaint;


 private paint mtextdaypaint;

 /**
 * 是否执行动画
 */
 private boolean isanimation = false;

 /**
 * 记录重绘次数
 */
 private int mcount = 0;

 /**
 * 执行动画线段每次绘制的长度,线段的总长度除以总共执行的时间乘以每次执行的间隔时间
 */
 private float manimationwidth = (mlinewidth / animation_time) * animation_interval;

 /**
 * 执行动画的位置
 */
 private int mposition;
 private int[] mmax;

 public stepsview(context context) {
 this(context, null);
 }

 public stepsview(context context, attributeset attrs) {
 this(context, attrs, 0);
 }

 public stepsview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 init();
 }

 /**
 * init
 */
 private void init() {
 mstepbeanlist = new arraylist<>();

 mcirclecenterpointpositionlist = new arraylist<>();

 //未完成文字画笔
 muncompletedpaint = new paint();
 muncompletedpaint.setantialias(true);
 muncompletedpaint.setcolor(muncompletedlinecolor);
 muncompletedpaint.setstrokewidth(2);
 muncompletedpaint.setstyle(paint.style.fill);

 //已完成画笔文字
 mcompletedpaint = new paint();
 mcompletedpaint.setantialias(true);
 mcompletedpaint.setcolor(mcompletedlinecolor);
 mcompletedpaint.setstrokewidth(2);
 mcompletedpaint.setstyle(paint.style.fill);

 //number paint
 mtextnumberpaint = new paint();
 mtextnumberpaint.setantialias(true);
 mtextnumberpaint.setcolor(muncompletedtextcolor);
 mtextnumberpaint.setstyle(paint.style.fill);
 mtextnumberpaint.settextsize(calcutils.sp2px(getcontext(), 10f));

 //number paint
 mtextdaypaint = new paint();
 mtextdaypaint.setantialias(true);
 mtextdaypaint.setcolor(muncompleteddaytextcolor);
 mtextdaypaint.setstyle(paint.style.fill);
 mtextdaypaint.settextsize(calcutils.sp2px(getcontext(), 12f));

 //已经完成的icon
 mcompleteicon = contextcompat.getdrawable(getcontext(), r.drawable.sign);
 //正在进行的icon
 mattentionicon = contextcompat.getdrawable(getcontext(), r.drawable.unsign);
 //未完成的icon
 mdefaulticon = contextcompat.getdrawable(getcontext(), r.drawable.unsign);
 //up的icon
 mupicon = contextcompat.getdrawable(getcontext(), r.drawable.jifendikuai);
 }

 @override
 protected synchronized void onmeasure(int widthmeasurespec, int heightmeasurespec) {
 setmeasureddimension(measurespec.getsize(widthmeasurespec), measurespec.getsize(heightmeasurespec));
 }

 @override
 protected void onsizechanged(int w, int h, int oldw, int oldh) {
 super.onsizechanged(w, h, oldw, oldh);
 setchange();
 }

 private void setchange() {
 //图标的中中心y点
 mcentery = calcutils.dp2px(getcontext(), 28f) + miconheight / 2;
 //获取左上方y的位置,获取该点的意义是为了方便画矩形左上的y位置
 mlefty = mcentery - (mcompletedlineheight / 2);
 //获取右下方y的位置,获取该点的意义是为了方便画矩形右下的y位置
 mrighty = mcentery + mcompletedlineheight / 2;

 //计算图标中心点
 mcirclecenterpointpositionlist.clear();
 //第一个点距离父控件左边14.5dp
 float size = miconwidth / 2 + calcutils.dp2px(getcontext(), 23f);
 mcirclecenterpointpositionlist.add(size);

 for (int i = 1; i < mstepnum; i++) {
  //从第二个点开始,每个点距离上一个点为图标的宽度加上线段的23dp的长度
  size = size + miconwidth + mlinewidth;
  mcirclecenterpointpositionlist.add(size);
 }
 }

 @suppresslint("drawallocation")
 @override
 protected synchronized void ondraw(canvas canvas) {
 super.ondraw(canvas);

 if (mstepbeanlist.size() != 0) {
  if (isanimation) {
  drawsign(canvas);
  } else {
  drawunsign(canvas);
  }
 }
 }

 /**
 * 绘制签到(伴随签到动画)
 */
 @suppresslint("drawallocation")
 private void drawsign(canvas canvas) {
 for (int i = 0; i < mcirclecenterpointpositionlist.size(); i++) {
  //绘制线段
  float precomplectedxposition = mcirclecenterpointpositionlist.get(i) + miconwidth / 2;
  if (i != mcirclecenterpointpositionlist.size() - 1) {
  //最后一条不需要绘制
  if (mstepbeanlist.get(i + 1).getstate() == stepbean.step_completed) {
   //下一个是已完成,当前才需要绘制
   canvas.drawrect(precomplectedxposition, mlefty, precomplectedxposition + mlinewidth,
    mrighty, mcompletedpaint);
  } else {
   //其余绘制灰色

   //当前位置执行动画
   if (i == mposition - 1) {
   //绿色开始绘制的地方,
   float endx = precomplectedxposition + manimationwidth * (mcount / animation_interval);
   //绘制
   canvas.drawrect(precomplectedxposition, mlefty, endx, mrighty, mcompletedpaint);
   //绘制
   canvas.drawrect(endx, mlefty, precomplectedxposition + mlinewidth,
    mrighty, muncompletedpaint);
   } else {
   canvas.drawrect(precomplectedxposition, mlefty, precomplectedxposition + mlinewidth,
    mrighty, muncompletedpaint);
   }
  }
  }

  //绘制图标
  float currentcomplectedxposition = mcirclecenterpointpositionlist.get(i);
  rect rect = new rect((int) (currentcomplectedxposition - miconwidth / 2),
   (int) (mcentery - miconheight / 2),
   (int) (currentcomplectedxposition + miconwidth / 2),
   (int) (mcentery + miconheight / 2));

  stepbean stepsbean = mstepbeanlist.get(i);

  if (i == mposition && mcount == animation_time) {
  //当前需要绘制
  mcompleteicon.setbounds(rect);
  mcompleteicon.draw(canvas);
  } else {
  if (stepsbean.getstate() == stepbean.step_undo) {
   mdefaulticon.setbounds(rect);
   mdefaulticon.draw(canvas);
  } else if (stepsbean.getstate() == stepbean.step_current) {
   mattentionicon.setbounds(rect);
   mattentionicon.draw(canvas);
  } else if (stepsbean.getstate() == stepbean.step_completed) {
   mcompleteicon.setbounds(rect);
   mcompleteicon.draw(canvas);
  }
  }

  //绘制图标
  if (stepsbean.getstate() == stepbean.step_completed || (i == mposition
   && mcount == animation_time)) {
  //已经完成了或者是当前动画完成并且需要当前位置需要改变
  if (stepsbean.getnumber() != 0) {
   //是up的需要橙色
   mtextnumberpaint.setcolor(mcurrenttextcolor);
  } else {
   //普通完成的颜色
   mtextnumberpaint.setcolor(mcompletedlinecolor);
  }
  } else {
  //还没签到的,颜色均为灰色
  mtextnumberpaint.setcolor(muncompletedlinecolor);
  }

  //绘制up
  if (stepsbean.getnumber() != 0) {
  //需要up才进行绘制
  rect rectup =
   new rect((int) (currentcomplectedxposition - mupwidth / 2),
    (int) (mcentery - miconheight / 2 - calcutils.dp2px(getcontext(), 8f) - mupheight),
    (int) (currentcomplectedxposition + mupwidth / 2),
    (int) (mcentery - miconheight / 2 - calcutils.dp2px(getcontext(), 1f)));
  mupicon.setbounds(rectup);
  mupicon.draw(canvas);
  }

  //0表示不需要显示积分,非0表示需要消失积分
  if (stepsbean.getnumber() != 0) {
  canvas.drawtext("+" + stepsbean.getnumber(),
   currentcomplectedxposition - calcutils.dp2px(getcontext(), 8f),
   mcentery / 2 - calcutils.dp2px(getcontext(), 0.5f),
   mtextnumberpaint);
  }
  //天数文字
  canvas.drawtext(stepsbean.getday(),
   currentcomplectedxposition - calcutils.dp2px(getcontext(), 12f),
   mcentery + calcutils.dp2px(getcontext(), 30f),
   mtextdaypaint);
 }

 //记录重绘次数
 mcount = mcount + animation_interval;
 if (mcount <= animation_time) {
  //引起重绘
  postinvalidate();
 } else {
  //重绘完成
  isanimation = false;
  mcount = 0;
 }
 }

 /**
 * 绘制初始状态的view
 */
 @suppresslint("drawallocation")
 private void drawunsign(canvas canvas) {

 for (int i = 0; i < mcirclecenterpointpositionlist.size(); i++) {
  //绘制线段
  float precomplectedxposition = mcirclecenterpointpositionlist.get(i) + miconwidth / 2;
  if (i != mcirclecenterpointpositionlist.size() - 1) {
  //最后一条不需要绘制
  if (mstepbeanlist.get(i + 1).getstate() == stepbean.step_completed) {
   //下一个是已完成,当前才需要绘制
   canvas.drawrect(precomplectedxposition, mlefty, precomplectedxposition + mlinewidth,
    mrighty, mcompletedpaint);
  } else {
   //其余绘制灰色
   canvas.drawrect(precomplectedxposition, mlefty, precomplectedxposition + mlinewidth,
    mrighty, muncompletedpaint);
  }
  }

  //绘制图标
  float currentcomplectedxposition = mcirclecenterpointpositionlist.get(i);
  rect rect = new rect((int) (currentcomplectedxposition - miconwidth / 2),
   (int) (mcentery - miconheight / 2),
   (int) (currentcomplectedxposition + miconwidth / 2),
   (int) (mcentery + miconheight / 2));


  stepbean stepsbean = mstepbeanlist.get(i);

  if (stepsbean.getstate() == stepbean.step_undo) {
  mdefaulticon.setbounds(rect);
  mdefaulticon.draw(canvas);
  } else if (stepsbean.getstate() == stepbean.step_current) {
  mattentionicon.setbounds(rect);
  mattentionicon.draw(canvas);
  } else if (stepsbean.getstate() == stepbean.step_completed) {
  mcompleteicon.setbounds(rect);
  mcompleteicon.draw(canvas);
  }

  //绘制增加的分数数目
  if (stepsbean.getstate() == stepbean.step_completed) {
  //已经完成了
  if (stepsbean.getnumber() != 0) {
   //是up的需要橙色
   mtextnumberpaint.setcolor(mcurrenttextcolor);
  } else {
   //普通完成的颜色
   mtextnumberpaint.setcolor(mcompletedlinecolor);
  }
  } else {
  //还没签到的,颜色均为灰色
  mtextnumberpaint.setcolor(muncompletedlinecolor);
  }

  //绘制up
  if (stepsbean.getnumber() != 0) {
  //需要up才进行绘制
  rect rectup =
   new rect((int) (currentcomplectedxposition - mupwidth / 2),
    (int) (mcentery - miconheight / 2 - calcutils.dp2px(getcontext(), 8f) - mupheight),
    (int) (currentcomplectedxposition + mupwidth / 2),
    (int) (mcentery - miconheight / 2 - calcutils.dp2px(getcontext(), 1f)));
  mupicon.setbounds(rectup);
  mupicon.draw(canvas);
  }

  //0表示不需要显示积分,非0表示需要消失积分
  if (stepsbean.getnumber() != 0) {
  //积分文字
  canvas.drawtext("+" + stepsbean.getnumber(),
   currentcomplectedxposition - calcutils.dp2px(getcontext(), 8f),
   mcentery / 2 - calcutils.dp2px(getcontext(), 0.5f),
   mtextnumberpaint);
  }


  //天数文字
  canvas.drawtext(stepsbean.getday(),
   currentcomplectedxposition - calcutils.dp2px(getcontext(), 12f),
   mcentery + calcutils.dp2px(getcontext(), 30f),
   mtextdaypaint);
 }
 }

 /**
 * 设置流程步数
 *
 * @param stepsbeanlist 流程步数
 */
 public void setstepnum(list<stepbean> stepsbeanlist) {

 if (stepsbeanlist == null && stepsbeanlist.size() == 0) {
  return;
 }
 mstepbeanlist = stepsbeanlist;
 mstepnum = mstepbeanlist.size();
 setchange();//重新绘制

 //引起重绘
 postinvalidate();
 }

 /**
 * 执行签到动画
 *
 * @param position 执行的位置
 */
 public void startsignanimation(int position) {
 //线条从灰色变为绿色
 isanimation = true;
 mposition = position;
 //引起重绘
 postinvalidate();
 }
}

3.主程序逻辑处理

/**
 * 一周签到规则:
 * 1、连续签到7天,即可额外获得15积分奖励
 * 2、连续签到记录在第8天开始时将清零重新计算
 * 3、如果中断签到,连续签到记录也将清零
 *
 * 注:可以显示签到的动画,这里没有使用动画
 * 需要动画可以调用mstepview.startsignanimation(int position)
 * position表示需要做动画的位置
 */
public class mainactivity extends appcompatactivity {

 private stepsview mstepview;
 private relativelayout rl_oval;
 private textview text_sign;
 private textview text_lianxusign;
 private arraylist<stepbean> mstepbeans = new arraylist<>();

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  initview();

  initdata();

  initlistener();
 }

 private void initlistener() {

  rl_oval.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    //点击签到按钮,请求后台接口数据
    //模拟请求接口数据成功
    requestsuccessdata();
   }
  });
 }

 /**
  * 模拟请求接口数据成功后更新数据
  */
 private void requestsuccessdata() {
  mstepbeans.clear();//清空初始化数据
  string reponse = "{\n" +
    " \"datas\": {\n" +
    "  \"day\": 3,\n" +
    "  \"mypoint\": 10890,\n" +
    "  \"signlog\": {\n" +
    "   \"content\": \"每日签到\",\n" +
    "   \"createtime\": \"2019-05-29 09:42:05\",\n" +
    "   \"familyid\": \"0\",\n" +
    "   \"id\": \"951660\",\n" +
    "   \"integral\": \"4\",\n" +
    "   \"logtype\": \"3\",\n" +
    "   \"orderid\": \"0\",\n" +
    "   \"type\": \"1\",\n" +
    "   \"userid\": \"43431\"\n" +
    "  },\n" +
    "  \"signstate\": true,\n" +
    "  \"usersingninlist\": [\n" +
    "   {\n" +
    "    \"createtime\": \"2019-05-27 18:04:15\",\n" +
    "    \"day\": \"1\",\n" +
    "    \"familyid\": \"0\",\n" +
    "    \"id\": \"278904\",\n" +
    "    \"seriesday\": \"1\",\n" +
    "    \"type\": \"0\",\n" +
    "    \"userid\": \"43431\"\n" +
    "   },\n" +
    "   {\n" +
    "    \"createtime\": \"2019-05-28 09:31:02\",\n" +
    "    \"day\": \"2\",\n" +
    "    \"familyid\": \"0\",\n" +
    "    \"id\": \"278905\",\n" +
    "    \"seriesday\": \"2\",\n" +
    "    \"type\": \"0\",\n" +
    "    \"userid\": \"43431\"\n" +
    "   },\n" +
    "   {\n" +
    "    \"createtime\": \"2019-05-29 09:42:05\",\n" +
    "    \"day\": \"3\",\n" +
    "    \"familyid\": \"0\",\n" +
    "    \"id\": \"278907\",\n" +
    "    \"seriesday\": \"3\",\n" +
    "    \"type\": \"0\",\n" +
    "    \"userid\": \"43431\"\n" +
    "   }\n" +
    "  ]\n" +
    " },\n" +
    " \"msg\": \"success!\",\n" +
    " \"ret\": 0\n" +
    "}";

  //解析后台请求数据
  signlistreq signlistreq = new gson().fromjson(reponse, signlistreq.class);
  if (signlistreq.getret() == 0) {
   rl_oval.setbackgroundresource(r.drawable.lianxusign_bg);
   text_sign.settext("已签到");
   text_lianxusign.setvisibility(view.visible);
   text_lianxusign.settext("连续" + signlistreq.getdatas().getday() + "天");

   setsigndata(signlistreq.getdatas());
  }

 }

 private void initview() {
  mstepview = findviewbyid(r.id.step_view);
  rl_oval = findviewbyid(r.id.rl_oval);
  text_sign = findviewbyid(r.id.text_sign);
  text_lianxusign = findviewbyid(r.id.text_lianxusign);

 }

 private void initdata() {

  //初始化模拟请求后台数据
  string reponse = "{\n" +
    " \"datas\": {\n" +
    "  \"day\": 2,\n" +
    "  \"mypoint\": 10886,\n" +
    "  \"signlog\": {\n" +
    "   \"content\": \"每日签到\",\n" +
    "   \"createtime\": \"2019-05-28 09:31:02\",\n" +
    "   \"familyid\": \"0\",\n" +
    "   \"id\": \"951656\",\n" +
    "   \"integral\": \"9\",\n" +
    "   \"logtype\": \"3\",\n" +
    "   \"orderid\": \"0\",\n" +
    "   \"type\": \"1\",\n" +
    "   \"userid\": \"43431\"\n" +
    "  },\n" +
    "  \"signstate\": true,\n" +
    "  \"usersingninlist\": [\n" +
    "   {\n" +
    "    \"createtime\": \"2019-05-27 18:04:15\",\n" +
    "    \"day\": \"1\",\n" +
    "    \"familyid\": \"0\",\n" +
    "    \"id\": \"278904\",\n" +
    "    \"seriesday\": \"1\",\n" +
    "    \"type\": \"0\",\n" +
    "    \"userid\": \"43431\"\n" +
    "   },\n" +
    "   {\n" +
    "    \"createtime\": \"2019-05-28 09:31:02\",\n" +
    "    \"day\": \"2\",\n" +
    "    \"familyid\": \"0\",\n" +
    "    \"id\": \"278905\",\n" +
    "    \"seriesday\": \"2\",\n" +
    "    \"type\": \"0\",\n" +
    "    \"userid\": \"43431\"\n" +
    "   }\n" +
    "  ]\n" +
    " },\n" +
    " \"msg\": \"success!\",\n" +
    " \"ret\": 0\n" +
    "}";

  //解析后台请求数据
  signlistreq signlistreq = new gson().fromjson(reponse, signlistreq.class);
  if (signlistreq.getret() == 0) {
   setsigndata(signlistreq.getdatas());
  }
 }

 /**
  * 数据处理
  *
  * @param datas
  */
 private void setsigndata(signlistreq.datasbean datas) {

  //处理已签到的数据
  //先添加已签到的日期到集合中
  if (datas.getusersingninlist().size() != 0) {
   for (int i = 0; i < datas.getusersingninlist().size(); i++) {
    //时间格式:2019-05-27 18:04:15
    string createtime = datas.getusersingninlist().get(i).getcreatetime();
    simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    date d1 = null;
    try {
     d1 = df.parse(createtime);
    } catch (parseexception e) {
     e.printstacktrace();
    }
    string timestring = df.format(d1);
    //获取日期的月、日
    string[] timelist = timestring.split(" ");
    string[] split = timelist[0].split("-");
    string month = split[1];//月
    string day = split[2];//日

    //判断是否需要显示积分图标,number表示-- 0为不显示积分,非0为显示积分
    if (datas.getsignlog() != null && datas.getusersingninlist().get(i).getcreatetime().equals(datas.getsignlog().getcreatetime())) {
     mstepbeans.add(new stepbean(stepbean.step_completed, integer.parseint(datas.getsignlog().getintegral()), month + "." + day));
    } else {
     mstepbeans.add(new stepbean(stepbean.step_completed, 0, month + "." + day));
    }
   }
  }

  //添加未签到的数据,填充为最近一周数据
  if (mstepbeans.size() < 7) {

   //获取当前时间的月日
   calendar now = calendar.getinstance();
   int currentmonth = now.get(calendar.month) + 1;//当月
   int currentday = now.get(calendar.day_of_month);//当天
   string currenttime = setdata(currentmonth) + "." + setdata(currentday);

   //后台有签到集合数据
   if (datas.getusersingninlist().size() != 0) {
    string createtime = datas.getusersingninlist().get(datas.getusersingninlist().size() - 1).getcreatetime();
    simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    date d1 = null;
    try {
     d1 = df.parse(createtime);
    } catch (parseexception e) {
     e.printstacktrace();
    }
    string timestring = df.format(d1);
    string[] timelist = timestring.split(" ");
    string[] split = timelist[0].split("-");
    string month = split[1];//月
    string day = split[2];//日

    for (int i = mstepbeans.size(); i < 7; i++) {
     int parseint = integer.parseint(day) + i - 1;
     //判断累积的天数是否超过当月的总天数
     if (parseint <= getdayofmonth()) {
      string time = setdata(integer.parseint(month)) + "." + setdata(parseint);
      if (currenttime.equals(time)) {
       mstepbeans.add(new stepbean(stepbean.step_current, 0, time));
      } else {
       mstepbeans.add(new stepbean(stepbean.step_undo, 0, time));
      }
     } else {
      string time = setdata((integer.parseint(month) + 1)) + "." + setdata(parseint - getdayofmonth());
      if (currenttime.equals(time)) {
       mstepbeans.add(new stepbean(stepbean.step_current, 0, time));
      } else {
       mstepbeans.add(new stepbean(stepbean.step_undo, 0, time));
      }
     }
    }
   } else {//后台没有签到集合数据,没有的话从当天时间开始添加未来一周的日期数据
    for (int i = 0; i < 7; i++) {
     int parseint = currentday + i;
     //判断累积的天数是否超过当月的总天数
     if (parseint <= getdayofmonth()) {
      string time = setdata(currentmonth) + "." + setdata(parseint);
      if (currenttime.equals(time)) {
       mstepbeans.add(new stepbean(stepbean.step_current, 0, time));
      } else {
       mstepbeans.add(new stepbean(stepbean.step_undo, 0, time));
      }
     } else {
      string time = setdata((currentmonth + 1)) + "." + setdata(parseint - getdayofmonth());
      if (currenttime.equals(time)) {
       mstepbeans.add(new stepbean(stepbean.step_current, 0, time));
      } else {
       mstepbeans.add(new stepbean(stepbean.step_undo, 0, time));
      }
     }
    }
   }
  }

  mstepview.setstepnum(mstepbeans);
 }

 /**
  * 获取最大天数
  *
  * @return
  */
 public int getdayofmonth() {
  calendar acalendar = calendar.getinstance(locale.china);
  int day = acalendar.getactualmaximum(calendar.date);
  return day;
 }

 /**
  * 日月份处理
  *
  * @param day
  * @return
  */
 public string setdata(int day) {
  string time = "";
  if (day < 10) {
   time = "0" + day;
  } else {
   time = "" + day;
  }

  return time;
 }
}

4.主界面布局文件

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".ui.activity.mainactivity">

 <relativelayout
  android:id="@+id/rl_oval"
  android:layout_width="70dp"
  android:layout_height="70dp"
  android:layout_margintop="150dp"
  android:layout_centerhorizontal="true"
  android:background="@drawable/sign_bg">

  <linearlayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerinparent="true"
   android:orientation="vertical">

   <textview
    android:id="@+id/text_sign"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="签到"
    android:textcolor="#fff"
    android:layout_gravity="center_horizontal"
    android:textsize="16sp" />

   <textview
    android:id="@+id/text_lianxusign"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="连续3天"
    android:textcolor="#fff"
    android:visibility="gone"
    android:layout_gravity="center_horizontal"
    android:textsize="12sp" />
  </linearlayout>
 </relativelayout>

 <com.sorgs.stepview.ui.widget.stepsview
  android:id="@+id/step_view"
  android:layout_below="@id/rl_oval"
  android:layout_margintop="20dp"
  android:layout_marginleft="15dp"
  android:layout_width="match_parent"
  android:layout_height="77dp" />
</relativelayout>

5.签到bean

package com.sorgs.stepview.bean;

/**
 * description: 签到bean.
 */
public class stepbean {
 /**
  * 未完成
  */
 public static final int step_undo = -1;
 /**
  * 正在进行
  */
 public static final int step_current = 0;
 /**
  * 已完成
  */
 public static final int step_completed = 1;

 private int state;
 private int number;//0为不显示积分,非0为显示积分
 private string day;

 public stepbean(int state, int number, string day) {
  this.state = state;
  this.number = number;
  this.day = day;
 }


 public int getnumber() {
  return number;
 }

 public void setnumber(int number) {
  this.number = number;
 }

 public int getstate() {
  return state;
 }

 public void setstate(int state) {
  this.state = state;
 }

 public string getday() {
  return day;
 }

 public void setday(string day) {
  this.day = day;
 }
}

6.总结

该篇的功能是根据需求进行功能的处理,自定义view是实现了签到时的动画效果的,不过我们的需求不需要动画,所以这里就没调用演示,需要的可以自行调用

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。