当前位置:   article > 正文

Android Gantt View 安卓实现项目甘特图

Android Gantt View 安卓实现项目甘特图

需要做一个项目管理工具,其中使用到了甘特图。发现全网甘特图解决方案比较少,于是自动动手丰衣足食。

前面我用 Python和 Node.js 前端都做过,这次仅仅是移植到 Android上面。

其实甘特图非常简单,开发也不难,如果我专职去做,能做出一个非常棒产品。我写这个只是消遣,玩玩,闲的蛋痛,所以不怎么上心,就搞成下面这德行吧。仅仅供大家学习,参考。

那天心情好了,完善一下。

屏幕布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. </ScrollView>
  11. <HorizontalScrollView
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent">
  14. <cn.netkiller.gantt.ui.GanttView
  15. android:layout_width="wrap_content"
  16. android:layout_height="match_parent"
  17. android:background="#DEDEDE"
  18. android:keepScreenOn="true"
  19. android:padding="15dp"
  20. android:text="TextView" />
  21. </HorizontalScrollView>
  22. </androidx.constraintlayout.widget.ConstraintLayout>

View 代码

  1. package cn.netkiller.gantt.ui;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Point;
  7. import android.graphics.drawable.Drawable;
  8. import android.text.TextPaint;
  9. import android.util.AttributeSet;
  10. import android.util.Log;
  11. import android.view.View;
  12. import java.text.ParseException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Calendar;
  15. import java.util.Collection;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.LinkedHashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;
  23. /**
  24. * TODO: document your custom view class.
  25. */
  26. public class GanttView extends View {
  27. private final String TAG = GanttView.class.getSimpleName();
  28. private Drawable mExampleDrawable;
  29. private int contentWidth, contentHeight;
  30. private int paddingLeft, paddingTop, paddingRight, paddingBottom;
  31. private int canvasLeft, canvasTop, canvasRight, canvasBottom;
  32. private Canvas canvas;
  33. private int textSize;
  34. private Map<Date, Coordinate> coordinates = new HashMap<Date, Coordinate>();
  35. public static class Coordinate {
  36. public int x, y;
  37. public Coordinate(int x, int y) {
  38. this.x = x;
  39. this.y = y;
  40. }
  41. public int getX() {
  42. return x;
  43. }
  44. public void setX(int x) {
  45. this.x = x;
  46. }
  47. public int getY() {
  48. return y;
  49. }
  50. public void setY(int y) {
  51. this.y = y;
  52. }
  53. @Override
  54. public String toString() {
  55. return "Coordinate{" +
  56. "x=" + x +
  57. ", y=" + y +
  58. '}';
  59. }
  60. }
  61. public GanttView(Context context) {
  62. super(context);
  63. init(null, 0);
  64. }
  65. public GanttView(Context context, AttributeSet attrs) {
  66. super(context, attrs);
  67. init(attrs, 0);
  68. }
  69. public GanttView(Context context, AttributeSet attrs, int defStyle) {
  70. super(context, attrs, defStyle);
  71. init(attrs, defStyle);
  72. }
  73. private void init(AttributeSet attrs, int defStyle) {
  74. paddingLeft = getPaddingLeft();
  75. paddingTop = getPaddingTop();
  76. paddingRight = getPaddingRight();
  77. paddingBottom = getPaddingBottom();
  78. contentWidth = getWidth() - paddingLeft - paddingRight;
  79. contentHeight = getHeight() - paddingTop - paddingBottom;
  80. // Load attributes
  81. // final TypedArray a = getContext().obtainStyledAttributes(
  82. // attrs, R.styleable.MyView, defStyle, 0);
  83. //
  84. mExampleString = a.getString(
  85. R.styleable.MyView_exampleString);
  86. // mExampleString = "AAAA";
  87. // mExampleColor = a.getColor(
  88. // R.styleable.MyView_exampleColor,
  89. // mExampleColor);
  90. // // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
  91. // // values that should fall on pixel boundaries.
  92. // mExampleDimension = a.getDimension(
  93. // R.styleable.MyView_exampleDimension,
  94. // mExampleDimension);
  95. //
  96. // if (a.hasValue(R.styleable.MyView_exampleDrawable)) {
  97. // mExampleDrawable = a.getDrawable(
  98. // R.styleable.MyView_exampleDrawable);
  99. // mExampleDrawable.setCallback(this);
  100. // }
  101. //
  102. // a.recycle();
  103. }
  104. // @Override
  105. // protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  106. // super.onSizeChanged(w, h, oldw, oldh);
  107. //
  108. if (h < computeVerticalScrollRange()) {
  109. canScroll = true;
  110. } else {
  111. canScroll = false;
  112. }
  113. // }
  114. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  115. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  116. setMeasuredDimension(contentWidth, contentHeight);
  117. }
  118. //
  119. // private boolean canScroll = true;
  120. //
  121. // @Override
  122. // protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  123. // super.onSizeChanged(w, h, oldw, oldh);
  124. //
  125. // if (h < computeVerticalScrollRange()) {
  126. // canScroll = true;
  127. // } else {
  128. // canScroll = false;
  129. // }
  130. // }
  131. List<Data> taskList = List.of(
  132. new Data("AAA", "2024-06-28", "2024-07-04", "1", "Neo"),
  133. new Data("AAABBB", "2024-06-15", "2024-06-20", "10", "Neo"),
  134. new Data("AAACC", "2024-06-25", "2024-06-27", "1", "Neo"),
  135. new Data("AAABBCCD", "2024-06-25", "2024-06-28", "1", "Neo"),
  136. new Data("消息推送", "2024-01-15", "2024-06-30", "1", "Neo"),
  137. new Data("AAA", "2024-06-05", "2024-07-12", "1", "Neo"),
  138. new Data("AAA", "2024-06-05", "2024-07-17", "1", "Neo"),
  139. new Data("AAA", "2024-06-15", "2024-07-07", "1", "Neo"),
  140. new Data("AAA", "2024-06-18", "2024-07-02", "1", "Neo"),
  141. new Data("AAA", "2024-06-05", "2024-07-05", "1", "Neo")
  142. );
  143. @Override
  144. protected void onDraw(Canvas canvas) {
  145. super.onDraw(canvas);
  146. this.canvas = canvas;
  147. canvasTop = paddingTop;
  148. canvasLeft = paddingLeft;
  149. canvasRight = canvas.getWidth() - paddingRight;
  150. canvasBottom = canvas.getHeight() - paddingBottom;
  151. Paint paint = new Paint();
  152. paint.setStyle(Paint.Style.STROKE);
  153. paint.setColor(Color.BLUE);
  154. paint.setStrokeWidth(2);
  155. canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
  156. paint.setColor(Color.DKGRAY);
  157. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  158. paint.setAlpha(30);
  159. canvas.drawRect(canvasLeft, canvasTop, canvasRight, canvasBottom, paint);
  160. title("canvas");
  161. try {
  162. calendar("2024-06-12", "2024-07-20");
  163. tasks(taskList);
  164. } catch (Exception e) {
  165. }
  166. // Draw the example drawable on top of the text.
  167. if (mExampleDrawable != null) {
  168. mExampleDrawable.setBounds(paddingLeft, paddingTop,
  169. paddingLeft + contentWidth, paddingTop + contentHeight);
  170. mExampleDrawable.draw(canvas);
  171. }
  172. }
  173. public Map<String, Float> colume(List<Data> taskList) {
  174. TextPaint textPaint = new TextPaint();
  175. textPaint.setTextSize(sp2px(20));
  176. float name = textPaint.measureText("任务");
  177. float start = textPaint.measureText("开始日期");
  178. float finish = textPaint.measureText("截止日期");
  179. float day = textPaint.measureText("工时");
  180. float resource = textPaint.measureText("资源");
  181. for (Data data : taskList) {
  182. float textWidth = textPaint.measureText(data.name);
  183. if (textWidth > name) {
  184. name = textWidth;
  185. }
  186. if (textPaint.measureText(data.start) > start) {
  187. start = textPaint.measureText(data.start);
  188. }
  189. if (textPaint.measureText(data.finish) > finish) {
  190. finish = textPaint.measureText(data.finish);
  191. }
  192. if (textPaint.measureText(data.day) > day) {
  193. day = textPaint.measureText(data.day);
  194. }
  195. if (textPaint.measureText(data.resource) > resource) {
  196. resource = textPaint.measureText(data.resource);
  197. }
  198. }
  199. float finalName = name;
  200. float finalStart = start;
  201. float finalResource = resource;
  202. float finalFinish = finish;
  203. float finalDay = day;
  204. return new LinkedHashMap<String, Float>() {{
  205. put("任务", finalName);
  206. put("开始日期", finalStart);
  207. put("截止日期", finalFinish);
  208. put("工时", finalDay);
  209. put("资源", finalResource);
  210. }};
  211. }
  212. private int titleHeight;
  213. public float sp2px(float spValue) {
  214. //fontScale (DisplayMetrics类中属性scaledDensity)
  215. final float fontScale = getResources().getDisplayMetrics().scaledDensity;
  216. return (spValue * fontScale + 0.5f);
  217. }
  218. private void title(String value) {
  219. // Draw the text.
  220. TextPaint textPaint = new TextPaint();
  221. textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  222. textPaint.setTextAlign(Paint.Align.LEFT);
  223. textPaint.setTextSize(sp2px(25));
  224. float textWidth = textPaint.measureText(value);
  225. Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
  226. // float textHeight = fontMetrics.bottom;
  227. float textHeight = textPaint.getFontSpacing();
  228. canvas.drawText(value,
  229. canvasLeft + (getWidth() - textWidth) / 2,
  230. canvasTop + textHeight,
  231. textPaint);
  232. textPaint.setTextSize(sp2px(18));
  233. String copyright = "https://www.netkiller.cn - design by netkiller";
  234. textWidth = textPaint.measureText(copyright);
  235. textHeight += textPaint.getFontSpacing();
  236. canvas.drawText(copyright,
  237. getWidth() - textWidth - canvasLeft,
  238. canvasTop + textHeight,
  239. textPaint);
  240. titleHeight = (int) textHeight;
  241. }
  242. private void process(String string, int x, int y, int size) {
  243. TextPaint mTextPaint = new TextPaint();
  244. mTextPaint.setTextSize(sp2px(size));
  245. // mTextWidth = mTextPaint.measureText(string);
  246. // Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
  247. // mTextHeight = fontMetrics.bottom;
  248. }
  249. private int tableEnd = 0;
  250. // private void table() {
  251. //
  252. //
  253. TextPaint textPaint = new TextPaint();
  254. // calendarTextPaint.setTextSize(sp2px(20));
  255. //
  256. // Paint.FontMetrics fontMetrics = calendarTextPaint.getFontMetrics();
  257. // float fontSpacing = calendarTextPaint.getFontSpacing();
  258. mTextHeight = fontMetrics.bottom;
  259. // int textX = canvasLeft + (int) fontSpacing / 2;
  260. // int textY = canvasTop + (int) fontSpacing * 3 + titleHeight;
  261. //
  262. // int startX = 0;
  263. // int startY = (int) (canvasTop + titleHeight + fontSpacing * 2);
  264. // int stopX = startX;
  265. // int stopY = canvasBottom;
  266. //
  267. // for (String text : List.of("任务", "开始日期", "截止日期", "工时", "资源")) {
  268. // canvas.drawText(text, textX, textY, calendarTextPaint);
  269. // textX += (int) (calendarTextPaint.measureText(text) + fontSpacing);
  270. // startX = stopX = textX;
  271. // canvas.drawLine(startX - (fontSpacing / 2), startY, stopX - (fontSpacing / 2), stopY, calendarPaint);
  272. // }
  273. // tableEnd = (int) (startX - (int) fontSpacing - fontSpacing / 2);
  274. //
  275. // }
  276. private void table() {
  277. // TextPaint textPaint = new TextPaint();
  278. calendarTextPaint.setTextSize(sp2px(20));
  279. // Paint.FontMetrics fontMetrics = calendarTextPaint.getFontMetrics();
  280. float fontSpacing = calendarTextPaint.getFontSpacing();
  281. // mTextHeight = fontMetrics.bottom;
  282. int tableLeft = canvasLeft;
  283. int tableTop = canvasTop + (int) fontSpacing * 2 + titleHeight;
  284. int tableRight = canvasRight;
  285. int tableBottom = canvasBottom;
  286. int textX = tableLeft;
  287. int textY = tableTop + calendarFontSpacing;
  288. int startX = tableLeft;
  289. int startY = tableTop;
  290. int stopX = startX;
  291. int stopY = tableBottom;
  292. for (Map.Entry<String, Float> entry : colume(taskList).entrySet()) {
  293. String text = entry.getKey();
  294. Float textWidth = entry.getValue();
  295. canvas.drawText(text, textX + (int) fontSpacing / 2, textY, calendarTextPaint);
  296. textX += (int) (textWidth + calendarFontSpacing);
  297. startX += (int) (textWidth + calendarFontSpacing);
  298. stopX = startX;
  299. canvas.drawLine(startX, startY, stopX, stopY, calendarPaint);
  300. }
  301. tableEnd = tableRight = (int) (startX - calendarFontSpacing / 2);
  302. }
  303. private void tasks(List<Data> taskList) throws ParseException {
  304. Paint paint = new Paint();
  305. paint.setStyle(Paint.Style.FILL);
  306. paint.setColor(Color.BLUE);
  307. paint.setStrokeWidth(2);
  308. // TextPaint textPaint = new TextPaint();
  309. // calendarPaint.setColor(Color.DKGRAY);
  310. calendarPaint.setColor(Color.GRAY);
  311. calendarTextPaint.setTextSize(sp2px(20));
  312. int taskTop = calendarTop + calendarFontSpacing * 3;
  313. int taskLeft = canvasLeft;
  314. int taskRight = tableEnd;
  315. int taskBottom = canvasBottom;
  316. // Paint.FontMetrics fontMetrics = calendarTextPaint.getFontMetrics();
  317. // float fontSpacing = calendarTextPaint.getFontSpacing();
  318. int textX = 0;
  319. int textY = taskTop + calendarFontSpacing;
  320. int startX = 0;
  321. int startY = taskTop;
  322. int stopX = startX;
  323. int stopY = taskTop + calendarFontSpacing;
  324. // canvas.drawLine(startX, calendarTop + calendarFontSpacing * 1, calendarRight, calendarTop + calendarFontSpacing * 1, calendarPaint);
  325. // canvas.drawLine(startX, startY - calendarFontSpacing, startX, stopY, calendarTextPaint);
  326. Map<String, Float> col = colume(taskList);
  327. Log.d(TAG, col.toString());
  328. for (Data task : taskList) {
  329. textX = taskLeft + (int) calendarFontSpacing / 2;
  330. Iterator<Float> aa = col.values().iterator();
  331. for (String text : List.of(task.name, task.start, task.finish, task.day, task.resource)) {
  332. Float textWidth = aa.next();
  333. canvas.drawText(text, textX, textY, calendarTextPaint);
  334. textX += (int) (textWidth + calendarFontSpacing);
  335. startX = stopX = textX;
  336. }
  337. textY += (int) (calendarFontSpacing);
  338. try {
  339. Date startData = new SimpleDateFormat("yyyy-MM-dd").parse(task.start);
  340. Date finishData = new SimpleDateFormat("yyyy-MM-dd").parse(task.finish);
  341. Log.e(TAG, "Start: " + String.valueOf(startData) + " Finish: " + finishData);
  342. Coordinate startCoordinates = coordinates.get(startData);
  343. Coordinate finishCoordinates = coordinates.get(finishData);
  344. Log.e(TAG, "Start: " + startCoordinates.toString() + "Finish: " + finishCoordinates);
  345. canvas.drawRect(startCoordinates.x + 5, startY + 5, finishCoordinates.x + calendarFontSpacing - 5, stopY - 5, paint);
  346. } catch (Exception e) {
  347. }
  348. // canvas.drawText(task.name, textX, textY, calendarTextPaint);
  349. startY += (int) (calendarFontSpacing);
  350. canvas.drawLine(canvasLeft, startY, calendarRight, stopY, calendarPaint);
  351. stopY += (int) (calendarFontSpacing);
  352. }
  353. }
  354. private Paint calendarPaint = new Paint();
  355. private TextPaint calendarTextPaint = new TextPaint();
  356. private int calendarFontSpacing;
  357. private int calendarLeft, calendarTop, calendarRight, calendarBottom;
  358. private void calendar(String startDate, String endDate) throws ParseException {
  359. calendarPaint.setStyle(Paint.Style.STROKE);
  360. calendarPaint.setColor(Color.DKGRAY);
  361. calendarPaint.setStrokeWidth(2);
  362. // calendarPaint.setTextSize(sp2px(20));
  363. // paint.setAlpha(50);
  364. calendarTextPaint.setTextSize(sp2px(20));
  365. calendarFontSpacing = (int) calendarTextPaint.getFontSpacing();
  366. //
  367. // Paint paint = new Paint();
  368. // paint.setTypeface(Typeface.DEFAULT);
  369. // paint.setTextSize(getTextSize());
  370. // Paint.FontMetrics fontMetrics = paint.getFontMetrics();
  371. // float textHeight = fm.getAscent() + fm.getDescent();
  372. // float calendarFontSpacing = fontMetrics.descent - fontMetrics.ascent;
  373. // float calendarFontSpacing = fontMetrics.bottom - fontMetrics.top;
  374. // Paint.FontMetrics fontMetrics = calendarPaint.getFontMetrics();
  375. // float textHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
  376. // 边框
  377. canvas.drawRect(canvasLeft, canvasTop + titleHeight, canvasRight, canvasBottom, calendarPaint);
  378. table();
  379. calendarLeft = canvasLeft + tableEnd;
  380. calendarTop = canvasTop + titleHeight;
  381. calendarRight = canvasRight;
  382. calendarBottom = canvasBottom;
  383. int textX = calendarLeft;
  384. int textY = calendarTop + (int) calendarFontSpacing * 2;
  385. int startX = calendarLeft;
  386. int startY = calendarTop + (int) calendarFontSpacing * 1;
  387. int stopX = 0;
  388. int stopY = calendarBottom;
  389. canvas.drawLine(startX, calendarTop + calendarFontSpacing * 1, calendarRight, calendarTop + calendarFontSpacing * 1, calendarPaint);
  390. canvas.drawLine(startX, startY - calendarFontSpacing, startX, stopY, calendarTextPaint);
  391. canvas.drawLine(canvasLeft, calendarTop + calendarFontSpacing * 2, calendarRight, calendarTop + calendarFontSpacing * 2, calendarPaint);
  392. canvas.drawLine(canvasLeft, calendarTop + calendarFontSpacing * 3, canvasRight, calendarTop + calendarFontSpacing * 3, calendarPaint);
  393. // Paint paint = new Paint();
  394. calendarPaint.setStyle(Paint.Style.FILL);
  395. calendarPaint.setColor(Color.BLUE);
  396. calendarPaint.setStrokeWidth(2);
  397. startY = calendarTop + (int) calendarFontSpacing * 2;
  398. int measureWeek = (int) calendarTextPaint.measureText("六");
  399. int measureDay = (int) calendarTextPaint.measureText("30");
  400. int measureText = measureWeek > measureDay ? measureWeek : measureDay;
  401. List<String> weeks = List.of("日", "一", "二", "三", "四", "五", "六");
  402. Calendar calendar = Calendar.getInstance();
  403. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
  404. Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);//定义起始日期
  405. Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);//定义结束日期
  406. calendar.setTime(d2);
  407. calendar.add(Calendar.DATE, 1);
  408. d2 = calendar.getTime();
  409. calendar.setTime(d1);//设置日期起始时间
  410. while (calendar.getTime().before(d2)) {//判断是否到结束日期
  411. String month = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
  412. String day = new SimpleDateFormat("d").format(calendar.getTime());
  413. coordinates.put(calendar.getTime(), new Coordinate(startX, startY));
  414. // if (dateRange.containsKey(month)) {
  415. // List<Date> tmp = dateRange.get(month);
  416. // tmp.add(calendar.getTime());
  417. // } else {
  418. // dateRange.put(month, List.of(calendar.getTime()));
  419. // }
  420. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  421. String week = weeks.get(dayOfWeek - 1);
  422. if (Set.of("六", "日").contains(week)) {
  423. calendarPaint.setColor(Color.WHITE);
  424. } else {
  425. calendarPaint.setColor(Color.GRAY);
  426. }
  427. // Log.d(TAG, String.valueOf());
  428. // Log.d(TAG, String.valueOf());
  429. stopX = (int) (startX + measureText);
  430. canvas.drawRect(startX, startY, stopX, stopY, calendarPaint);
  431. canvas.drawText(week, textX, textY, calendarTextPaint);
  432. if (calendarTextPaint.measureText(day) < calendarTextPaint.measureText(week)) {
  433. canvas.drawText(day, textX + calendarTextPaint.measureText(day) / 2, textY + calendarFontSpacing, calendarTextPaint);
  434. } else {
  435. canvas.drawText(day, textX, textY + calendarFontSpacing, calendarTextPaint);
  436. }
  437. if (day.equals("1")) {
  438. canvas.drawText(month, textX, textY - calendarFontSpacing, calendarTextPaint);
  439. canvas.drawLine(startX, startY - calendarFontSpacing * 2, startX, stopY, calendarTextPaint);
  440. }
  441. if (week.equals("日")) {
  442. canvas.drawLine(stopX, startY - calendarFontSpacing, stopX, stopY, calendarTextPaint);
  443. }
  444. textX += measureText + 2;
  445. startX = textX;
  446. calendar.add(Calendar.DATE, 1);//进行当前日期月份加1
  447. }
  448. calendarPaint.setColor(Color.GRAY);
  449. canvas.drawLine(canvasLeft, calendarTop + calendarFontSpacing * 2, calendarRight, calendarTop + calendarFontSpacing * 2, calendarPaint);
  450. canvas.drawLine(canvasLeft, calendarTop + calendarFontSpacing * 3, canvasRight, calendarTop + calendarFontSpacing * 3, calendarPaint);
  451. calendarLeft = stopX;
  452. }
  453. public class Data {
  454. public Data(String name, String start, String finish, String day, String resource) {
  455. this.name = name;
  456. this.start = start;
  457. this.finish = finish;
  458. this.day = day;
  459. this.resource = resource;
  460. }
  461. public String name;
  462. public String start;
  463. public String finish;
  464. public String day;
  465. public String resource;
  466. }
  467. // public class Coordinate{
  468. //
  469. // }
  470. public void setTextSize(int textSize) {
  471. this.textSize = textSize;
  472. }
  473. public int getTextSize() {
  474. return textSize;
  475. }
  476. /**
  477. * Gets the example dimension attribute value.
  478. *
  479. * @return The example dimension attribute value.
  480. */
  481. // public float getExampleDimension() {
  482. // return mExampleDimension;
  483. // }
  484. /**
  485. * Sets the view"s example dimension attribute value. In the example view, this dimension
  486. * is the font size.
  487. *
  488. * @param exampleDimension The example dimension attribute value to use.
  489. */
  490. // public void setExampleDimension(float exampleDimension) {
  491. // mExampleDimension = exampleDimension;
  492. // invalidateTextPaintAndMeasurements();
  493. // }
  494. /**
  495. * Gets the example drawable attribute value.
  496. *
  497. * @return The example drawable attribute value.
  498. */
  499. // public Drawable getExampleDrawable() {
  500. // return mExampleDrawable;
  501. // }
  502. /**
  503. * Sets the view"s example drawable attribute value. In the example view, this drawable is
  504. * drawn above the text.
  505. *
  506. * @param exampleDrawable The example drawable attribute value to use.
  507. */
  508. // public void setExampleDrawable(Drawable exampleDrawable) {
  509. // mExampleDrawable = exampleDrawable;
  510. // }
  511. private void week() {
  512. Paint paint = new Paint();
  513. paint.setStyle(Paint.Style.FILL);
  514. paint.setColor(Color.BLUE);
  515. paint.setStrokeWidth(2);
  516. // Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
  517. // float fontSpacing = calendarTextPaint.getFontSpacing();
  518. int textX = calendarLeft;
  519. int textY = calendarTop + (int) calendarFontSpacing * 2;
  520. int startX = calendarLeft;
  521. int startY = calendarTop + (int) calendarFontSpacing * 2;
  522. int stopX = 0;
  523. int stopY = calendarBottom;
  524. int measureWeek = (int) calendarTextPaint.measureText("六");
  525. int measureDay = (int) calendarTextPaint.measureText("30");
  526. int measureText = measureWeek > measureDay ? measureWeek : measureDay;
  527. List<String> weeks = List.of("一", "二", "三", "四", "五", "六", "日");
  528. int w = 0;
  529. for (int i = 1; i <= 31; i++) {
  530. // for (String week : List.of("一", "二", "三", "四", "五", "六", "日")) {
  531. String week = weeks.get(w);
  532. w++;
  533. if (w >= weeks.size()) {
  534. w = 0;
  535. }
  536. String day = String.valueOf(i);
  537. if (Set.of("六", "日").contains(week)) {
  538. paint.setColor(Color.WHITE);
  539. } else {
  540. paint.setColor(Color.GRAY);
  541. }
  542. // Log.d(TAG, String.valueOf());
  543. // Log.d(TAG, String.valueOf());
  544. stopX = (int) (startX + measureText);
  545. canvas.drawRect(startX, startY, stopX, stopY, paint);
  546. canvas.drawText(week, textX, textY, calendarTextPaint);
  547. if (calendarTextPaint.measureText(day) < calendarTextPaint.measureText(week)) {
  548. canvas.drawText(day, textX + calendarTextPaint.measureText(day) / 2, textY + calendarFontSpacing, calendarTextPaint);
  549. } else {
  550. canvas.drawText(day, textX, textY + calendarFontSpacing, calendarTextPaint);
  551. }
  552. // if (week.equals("一")) {
  553. // canvas.drawLine(startX, startY - calendarFontSpacing, startX, stopY, calendarTextPaint);
  554. // }
  555. if (week.equals("日")) {
  556. canvas.drawLine(stopX, startY - calendarFontSpacing, stopX, stopY, calendarTextPaint);
  557. }
  558. textX += measureText + 2;
  559. startX = textX;
  560. // stopX = (int) (startX + fontSpacing);
  561. }
  562. calendarLeft = stopX;
  563. }
  564. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/822144
推荐阅读
相关标签
  

闽ICP备14008679号