电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android 實現簽到足跡功能

瀏覽:3日期:2022-09-19 16:21:18

UI 妹紙又給了個圖叫我做,我一看是這樣的:

Android 實現簽到足跡功能

我們首先把這個控件劃分成 幾個部分:

1.底下部分的直線 :

2.左右兩邊的半圓弧度 :

3.線上面的小圖標 :

4.最后的文字說明 :

首先我們把線畫出來,大概這個樣子

Android 實現簽到足跡功能

我們這里根據一個月得總天數,和一條線上需要畫七個圖,計算出總共需要畫出的線條數,以及畫出左邊和右邊的弧度,根據當前線是單數還是雙數,來計算出是否是左半邊的弧度,還是右半邊的弧度,以及是否是最后的一條線,因為最后一條線不需要畫弧度。

代碼如下:

@Override protected void onDraw(Canvas canvas){paint.setColor(backColor);paint.setStrokeWidth(strokeWidth);int rowCount = (monthDays % 7 == 0 ? monthDays / 7 : monthDays / 7 + 1);int rowHeigh = height / (rowCount);int startX = 0 + rowHeigh / 2;int endX = width - rowHeigh / 2;int days = 0;for (int a = 0; a < rowCount; a++){ if (a + 1 == rowCount) {endX = (endX - startX) / 7 * (monthDays % 7) + checkBitmap.getWidth() / 2; } paint.setStrokeWidth(strokeWidth); int y = rowHeigh * a + rowHeigh / 2; canvas.drawLine(startX, y, endX, y, paint); paint.setColor(rashColor); paint.setStrokeWidth(1); canvas.drawLine(startX, y, endX, y, paint); // 這里是來判斷,是否需要畫出左半邊還是右半邊的半圓弧度? if (a % 2 != 0) {if (a + 1 != rowCount){ drawLeftOrRightArc(true, canvas, 0 + strokeWidth, y, 0 + rowHeigh + strokeWidth, y + rowHeigh);} } else {if (a + 1 != rowCount){ drawLeftOrRightArc(false, canvas, endX - rowHeigh / 2 - strokeWidth, y, endX + rowHeigh / 2 - strokeWidth, y + rowHeigh);} }} }然后再在線上畫出禮物數量

// 這里是來判斷,本次這根線上畫出的禮物的點,以及順序是順畫,還是倒畫出。 bitmapList.clear(); for (int b = 0; b < (a + 1 == rowCount ? (monthDays % 7) : 7); b++) {days++;if (days <= signInCount){ if (days == 3 || days == 8 || days == 14 || days == 21 || days == monthDays) {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), openGiftBitmap); } else {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), checkBitmap); }} else{ if (days == 3 || days == 8 || days == 14 || days == 21 || days == monthDays) {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), closeGiftBitmap); } else {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), uncheckBitmap); }} }

這里有一個需要注意的地方,就是,在線為雙數的時候,這時候禮物的排列是需要反過來排列的,我這里使用了一個LinkedList來保存禮物的排列順序,然后我們通過計算平均數,計算出每個禮物的位置。

/** * 畫出的按路線上的圖片,勾選,禮物 * @param bitmapList * @param startX * @param endX * @param y * @param canvas */ private void drawImgs(List<Bitmap> bitmapList, float startX, float endX, float y, Canvas canvas){startX = startX - bitmapList.get(0).getWidth() / 2;int count = bitmapList.size();float bitmap_width = (endX - startX) / (count - 1);for (int a = 0; a < count; a++){ Bitmap bitmap = bitmapList.get(a); canvas.drawBitmap(bitmap, startX + (bitmap_width * a), y - bitmap.getHeight() / 2, paint);} }

這里也有一個需要注意的地方,就是,當最后一條線是短的時候,這個時候,你的禮物的排列需要按照那條線的開始位置和結束位置來平均計算每個禮物的位置。

最后,我們在最后一條線最后的位置,畫出文字

/** * 畫出文字 * @param canvas * @param y * @param x */ private void drawText(Canvas canvas, float y, float x){int oldColor = paint.getColor();Paint.Style old_style = paint.getStyle();paint.setStyle(Paint.Style.FILL);paint.setColor(textColor);String drawText = '已累計簽到'+signInCount+'天';paint.setTextSize(DensityUtil.sp2px(getContext(), 15));int textHeigh = getStringHeight(drawText);int textWidth = getStringWidth(drawText);canvas.drawText(drawText, x + textWidth/2, y + textHeigh / 2, paint);paint.setColor(oldColor);paint.setStyle(old_style); }

Android 實現簽到足跡功能

好了,這就是所有的思路。下面貼一下最新完整代碼:

package com.sjl.keeplive.track;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import com.sjl.keeplive.R;import java.util.LinkedList;import java.util.List;public class SignInView extends View { private int width, height; private int monthDays = 31;//本月有31天 private Paint paint; private RectF oval = new RectF(); private float strokeWidth = 10; private Bitmap checkBitmap, uncheckBitmap, closeGiftBitmap, openGiftBitmap; private int backColor = Color.parseColor('#C3DEEA'), rashColor = Color.parseColor('#B2CADB'), textColor = Color.parseColor('#60ADE5'); private List<Bitmap> bitmapList = new LinkedList<>(); private int signInCount = 9; public SignInView(Context context) {this(context, null); } public SignInView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0); } public SignInView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs); } private void init(Context context, AttributeSet attrs) {paint = new Paint();paint.setAntiAlias(true);strokeWidth = DensityUtil.dip2px(6);checkBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_sign_in_check_img);uncheckBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_sign_in_uncheck_img);closeGiftBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_close_gift_img);openGiftBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_open_gift_img); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {height = MeasureSpec.getSize(heightMeasureSpec);width = MeasureSpec.getSize(widthMeasureSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 設置本月天數 * * @param monthDays */ public void setMonthDays(int monthDays) {this.monthDays = monthDays;if (monthDays == 0) { this.monthDays = 31;}postInvalidate(); } /** * 設置一共簽到了幾天 * * @param days */ public void setProgress(int days) {this.signInCount = days;postInvalidate(); } @Override protected void onDraw(Canvas canvas) {paint.setColor(backColor);paint.setStrokeWidth(strokeWidth);int rowCount = (monthDays % 7 == 0 ? monthDays / 7 : monthDays / 7 + 1);int rowHeigh = height / (rowCount);int startX = 0 + rowHeigh / 2;int endX = width - rowHeigh / 2;int days = 0;for (int a = 0; a < rowCount; a++) { if (a + 1 == rowCount) {endX = (endX - startX) / 7 * (monthDays % 7 == 0 ? 7 : (monthDays % 7)) + checkBitmap.getWidth() / 2; } paint.setStrokeWidth(strokeWidth); int y = rowHeigh * a + rowHeigh / 2; canvas.drawLine(startX, y, endX, y, paint); paint.setColor(rashColor); paint.setStrokeWidth(1); canvas.drawLine(startX, y, endX, y, paint); // 這里是來判斷,是否需要畫出左半邊還是右半邊的半圓弧度? if (a % 2 != 0) {if (a + 1 != rowCount) { drawLeftOrRightArc(true, canvas, 0 + strokeWidth, y, 0 + rowHeigh + strokeWidth, y + rowHeigh);} } else {if (a + 1 != rowCount) { drawLeftOrRightArc(false, canvas, endX - rowHeigh / 2 - strokeWidth, y, endX + rowHeigh / 2 - strokeWidth, y + rowHeigh);} } // 這里是來判斷,本次這根線上畫出的禮物的點,以及順序是順畫,還是倒畫出。 bitmapList.clear(); int lastDay = (monthDays % 7) == 0 ? 7 : (monthDays % 7); for (int b = 0; b < (a + 1 == rowCount ? (lastDay) : 7); b++) {days++;if (days <= signInCount) { if (days == 3 || days == 8 || days == 14 || days == 21 || days == monthDays) {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), openGiftBitmap); } else {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), checkBitmap); }} else { if (days == 3 || days == 8 || days == 14 || days == 21 || days == monthDays) {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), closeGiftBitmap); } else {bitmapList.add(a % 2 != 0 ? 0 : bitmapList.size(), uncheckBitmap); }} } drawImgs(bitmapList, startX, endX, y, canvas);}super.onDraw(canvas); } /** * 畫出的按路線上的圖片,勾選,禮物 * * @param bitmapList * @param startX * @param endX * @param y * @param canvas */ private void drawImgs(List<Bitmap> bitmapList, float startX, float endX, float y, Canvas canvas) {if (!bitmapList.isEmpty()) { startX = startX - bitmapList.get(0).getWidth() / 2; int count = bitmapList.size(); float bitmap_width = (endX - startX) / (count - 1); for (int a = 0; a < count; a++) {Bitmap bitmap = bitmapList.get(a);canvas.drawBitmap(bitmap, startX + (bitmap_width * a), y - bitmap.getHeight() / 2, paint); }} } /** * 這里畫出左邊半圓弧,還是右邊半圓弧 * * @param isLeft * @param canvas * @param left * @param top * @param right * @param bottom */ private void drawLeftOrRightArc(boolean isLeft, Canvas canvas, float left, float top, float right, float bottom) {paint.setStrokeWidth(strokeWidth);paint.setColor(backColor);if (isLeft) { paint.setStyle(Paint.Style.STROKE); oval.setEmpty(); oval.set(left, top, right, bottom); canvas.drawArc(oval, 90, 180, false, paint); paint.setStrokeWidth(1); paint.setColor(rashColor); canvas.drawArc(oval, 90, 180, false, paint);} else { paint.setStyle(Paint.Style.STROKE); oval.setEmpty(); oval.set(left, top, right, bottom); canvas.drawArc(oval, 270, 180, false, paint); paint.setStrokeWidth(1); paint.setColor(rashColor); canvas.drawArc(oval, 270, 180, false, paint);}paint.setStrokeWidth(strokeWidth);paint.setColor(backColor); }}

布局文件使用:

<com.sjl.keeplive.track.SignInViewandroid:layout_width='match_parent'android:layout_height='match_parent'android:visibility='visible'/>

項目地址:

鏈接:https://pan.baidu.com/s/1IUh9og2T3IlxeXhaLLOKGg 提取碼:thoc

由于demo集合比較多,單這篇看下面代碼即可:

Android 實現簽到足跡功能

以上就是Android 實現簽到足跡功能的詳細內容,更多關于Android 簽到功能的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 沈阳楼承板_彩钢板_压型钢板厂家-辽宁中盛绿建钢品股份有限公司 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 猎头招聘_深圳猎头公司_知名猎头公司| 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 纸布|钩编布|钩针布|纸草布-莱州佳源工艺纸布厂 | TPU薄膜_TPU薄膜生产厂家_TPU热熔胶膜厂家定制_鑫亘环保科技(深圳)有限公司 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 深圳装修_店面装修设计_餐厅设计_装修全包价格-尚泰装饰设计 | 许昌奥仕达自动化设备有限公司| 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 精密光学实验平台-红外粉末压片机模具-天津博君 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 汽液过滤网厂家_安平县银锐丝网有限公司| 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 钣金加工厂家-钣金加工-佛山钣金厂-月汇好 | 中宏网-今日新闻-财经新闻| 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 湖南印刷厂|长沙印刷公司|画册印刷|挂历印刷|台历印刷|杂志印刷-乐成印刷 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 代办建筑资质升级-建筑资质延期就找上海国信启航| 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 活性炭厂家-蜂窝活性炭-粉状/柱状/果壳/椰壳活性炭-大千净化-活性炭 | 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 公交驾校-北京公交驾校欢迎您! 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 风信子发稿-专注为企业提供全球新闻稿发布服务 | 石英粉,滑石粉厂家,山东滑石粉-莱州市向阳滑石粉有限公司 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 |