From 5977d5fb7569969c0316462379cc9c902c135b34 Mon Sep 17 00:00:00 2001 From: Nazar Suhovich Date: Mon, 7 Aug 2017 19:21:57 +0300 Subject: [PATCH] Add different shape marker to day cell. --- .../naz013/awersomecalendar/MainActivity.java | 14 ++++++- .../com/github/naz013/awcalendar/DayCell.java | 37 +++++++++++++++++-- .../com/github/naz013/awcalendar/Event.java | 18 ++++++--- .../com/github/naz013/awcalendar/Shape.java | 32 ++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 awesome-calendar/src/main/java/com/github/naz013/awcalendar/Shape.java diff --git a/app/src/main/java/com/github/naz013/awersomecalendar/MainActivity.java b/app/src/main/java/com/github/naz013/awersomecalendar/MainActivity.java index 1c9789b..f865137 100644 --- a/app/src/main/java/com/github/naz013/awersomecalendar/MainActivity.java +++ b/app/src/main/java/com/github/naz013/awersomecalendar/MainActivity.java @@ -13,6 +13,7 @@ import com.github.naz013.awcalendar.AwesomeCalendarView; import com.github.naz013.awcalendar.Event; +import com.github.naz013.awcalendar.Shape; import com.github.naz013.awcalendar.Utils; import java.util.ArrayList; @@ -65,7 +66,7 @@ private List getEvents() { for (int i = 0; i < 500; i++) { int rand = r.nextInt(10); while (rand > 0) { - events.add(new Event(-1, "" + i, Utils.toDateTime(calendar.getTimeInMillis()))); + events.add(new Event(Utils.toDateTime(calendar.getTimeInMillis()), getRandomShape())); rand--; i++; } @@ -74,6 +75,17 @@ private List getEvents() { return events; } + private Shape getRandomShape() { + int i = new Random().nextInt(50); + if (i % 5 == 0) { + return Shape.CIRCLE; + } else if (i % 4 == 0) { + return Shape.SQUARE; + } else if (i % 3 == 0) { + return Shape.DIAMOND; + } else return Shape.TRIANGLE; + } + class SimpleAdapter extends RecyclerView.Adapter { @Override diff --git a/awesome-calendar/src/main/java/com/github/naz013/awcalendar/DayCell.java b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/DayCell.java index 908da27..0f43418 100644 --- a/awesome-calendar/src/main/java/com/github/naz013/awcalendar/DayCell.java +++ b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/DayCell.java @@ -2,6 +2,7 @@ import android.graphics.Canvas; import android.graphics.Paint; +import android.graphics.Path; import android.graphics.Rect; import java.util.ArrayList; @@ -174,15 +175,45 @@ private void drawEvents(Canvas canvas, Painter painter) { int left = j * circleWidth + rectLeft; Rect r = new Rect(left, top, left + circleWidth, top + circleHeight); Paint p = painter.getEventPaint(); - if (events.get(index).color != -1) { + Event event = events.get(index); + float gap = r.width() / 4f; + if (event.color != -1) { p = painter.getEventShadowPaint(); - p.setColor(events.get(index).color); + p.setColor(event.color); + } + if (event.shape == Shape.SQUARE) { + canvas.drawRect(r.left + gap, r.top + gap, r.right - gap, r.bottom - gap, p); + } else if (event.shape == Shape.DIAMOND) { + canvas.drawPath(getDiamond(r, gap), p); + } else if (event.shape == Shape.TRIANGLE) { + canvas.drawPath(getTriangle(r, gap), p); + } else { + canvas.drawCircle(r.centerX(), r.centerY(), gap, p); } - canvas.drawCircle(r.centerX(), r.centerY(), r.width() / 4f, p); } } } + private Path getTriangle(Rect r, float gap) { + Path path = new Path(); + path.moveTo(r.left + (gap * 2f), r.top + (gap * 0.5f)); + path.lineTo(r.right - (gap * 0.5f), r.bottom - gap); + path.lineTo(r.left + (gap * 0.5f), r.bottom - gap); + path.close(); + return path; + } + + private Path getDiamond(Rect r, float gap) { + Path path = new Path(); + path.moveTo(r.left + (r.width() / 2), r.top + (gap * 0.5f)); + path.lineTo(r.right - (gap * 0.5f), r.top + (r.height() / 2)); + path.lineTo(r.right - (r.width() / 2), r.bottom - (gap * 0.5f)); + path.lineTo(r.left + (gap * 0.5f), r.bottom - (r.height() / 2)); + path.lineTo(r.left + (r.width() / 2), r.top + (gap * 0.5f)); + path.close(); + return path; + } + private void drawRectText(String text, Canvas canvas, Rect r, Painter painter) { Paint paint = painter.getTextPaint(); if (isCurrent) paint = painter.getCurrentDayPaint(); diff --git a/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Event.java b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Event.java index be4ef9e..764f3c2 100644 --- a/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Event.java +++ b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Event.java @@ -6,19 +6,19 @@ /** * MIT License - * + *

* Copyright (c) 2017 Nazar Suhovich - * + *

* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -37,12 +37,18 @@ public class Event { public int color = -1; public String title = ""; public DateTime dateTime; + public Shape shape = Shape.CIRCLE; - public Event(DateTime dateTime){ + public Event(DateTime dateTime) { this.dateTime = dateTime; } - public Event(String title, DateTime dateTime){ + public Event(DateTime dateTime, Shape shape) { + this.dateTime = dateTime; + this.shape = shape; + } + + public Event(String title, DateTime dateTime) { this.title = title; this.dateTime = dateTime; } diff --git a/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Shape.java b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Shape.java new file mode 100644 index 0000000..96a7504 --- /dev/null +++ b/awesome-calendar/src/main/java/com/github/naz013/awcalendar/Shape.java @@ -0,0 +1,32 @@ +package com.github.naz013.awcalendar; + +/** + * MIT License + *

+ * Copyright (c) 2017 Nazar Suhovich + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +public enum Shape { + CIRCLE, + TRIANGLE, + SQUARE, + DIAMOND +}