diff --git a/README.md b/README.md index e83c54ec7..ef64b887d 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,17 @@ Usage }; ``` +6. Add an icon at a WeekViewEvent with `WeekViewEvent.setIconId(int iconId)` and change its color with `WeekViewEvent.setIconColor(int iconColor)`. + + ```java + WeekViewEvent event = new WeekViewEvent(1, "My title", startTime, endTime); + + // Add a vector asset + event.setIconId(R.drawable.ic_my_vector); + // Change the color of the asset (by default, it's the same that the title color) + event.setIconColor(R.color.myColor); + ``` + Customization ------------------- @@ -126,6 +137,7 @@ You can customize the look of the `WeekView` in xml. Use the following attribute - `showNowLine` - `nowLineColor` - `nowLineThickness` +- `eventIconSize` Interfaces ---------- diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index cb22eefca..dd8f16601 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -6,10 +6,12 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; +import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Region; import android.graphics.Typeface; +import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.Nullable; import android.support.v4.view.GestureDetectorCompat; @@ -121,6 +123,7 @@ private enum Direction { private int mHourSeparatorHeight = 2; private int mTodayHeaderTextColor = Color.rgb(39, 137, 228); private int mEventTextSize = 12; + private int mEventIconSize = 12; private int mEventTextColor = Color.BLACK; private int mEventPadding = 8; private int mHeaderColumnBackgroundColor = Color.WHITE; @@ -331,6 +334,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) { mHourSeparatorHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mHourSeparatorHeight); mTodayHeaderTextColor = a.getColor(R.styleable.WeekView_todayHeaderTextColor, mTodayHeaderTextColor); mEventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_eventTextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventTextSize, context.getResources().getDisplayMetrics())); + mEventIconSize = a.getDimensionPixelSize(R.styleable.WeekView_eventIconSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventIconSize, context.getResources().getDisplayMetrics())); mEventTextColor = a.getColor(R.styleable.WeekView_eventTextColor, mEventTextColor); mEventPadding = a.getDimensionPixelSize(R.styleable.WeekView_eventPadding, mEventPadding); mHeaderColumnBackgroundColor = a.getColor(R.styleable.WeekView_headerColumnBackground, mHeaderColumnBackgroundColor); @@ -767,6 +771,7 @@ top < getHeight() && mEventBackgroundPaint.setColor(mEventRects.get(i).event.getColor() == 0 ? mDefaultEventColor : mEventRects.get(i).event.getColor()); canvas.drawRoundRect(mEventRects.get(i).rectF, mEventCornerRadius, mEventCornerRadius, mEventBackgroundPaint); drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, top, left); + drawEventIcon(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas); } else mEventRects.get(i).rectF = null; @@ -804,6 +809,10 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2); int availableWidth = (int) (rect.right - originalLeft - mEventPadding * 2); + if (event.getIconId() != 0) { + availableWidth -= mEventIconSize + mEventPadding; + } + // Get text dimensions. StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, availableWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); @@ -831,6 +840,42 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa } + /** + * Draw the icon associated at the event on right bottom of the event rectangle. + * + * @param event The event of which the title (and location) should be drawn. + * @param rect The rectangle on which the text is to be drawn. + * @param canvas The canvas to draw upon. + */ + private void drawEventIcon(WeekViewEvent event, RectF rect, Canvas canvas) { + if (rect.right - rect.left - mEventPadding * 2 - mEventIconSize < 0) return; + if (rect.bottom - rect.top - mEventPadding * 2 - mEventIconSize < 0) return; + + int iconId = event.getIconId(); + + if (iconId != 0) { + Drawable drawable = mContext.getResources().getDrawable(iconId); + + if (drawable != null) { + int left = 0; + int top = 0; + int right = mEventIconSize; + int bottom = mEventIconSize; + int color = event.getIconColor() == 0 ? mEventTextColor : event.getIconColor(); + + drawable.setBounds(left, top, right, bottom); + drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); + + canvas.save(); + canvas.translate(rect.right - mEventIconSize - mEventPadding, + rect.bottom - mEventIconSize - mEventPadding); + drawable.draw(canvas); + canvas.restore(); + } + } + } + + /** * A class to hold reference to the events and their visual representation. An EventRect is * actually the rectangle that is drawn on the calendar for a given event. There may be more @@ -1972,4 +2017,4 @@ private Calendar today(){ return today; } -} +} \ No newline at end of file diff --git a/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java b/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java index e0d4765a8..27006f8d6 100644 --- a/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java @@ -13,6 +13,8 @@ public class WeekViewEvent { private String mName; private String mLocation; private int mColor; + private int mIconId; + private int mIconColor; public WeekViewEvent(){ @@ -20,6 +22,7 @@ public WeekViewEvent(){ /** * Initializes the event for week view. + * * @param id The id of the event. * @param name Name of the event. * @param startYear Year when the event starts. @@ -32,8 +35,12 @@ public WeekViewEvent(){ * @param endDay Day when the event ends. * @param endHour Hour (in 24-hour format) when the event ends. * @param endMinute Minute when the event ends. + * @param iconId Icon associated at the event. + * @param iconColor Color of the icon associated at the event. */ - public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay, int startHour, int startMinute, int endYear, int endMonth, int endDay, int endHour, int endMinute) { + public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay, + int startHour, int startMinute, int endYear, int endMonth, int endDay, + int endHour, int endMinute, int iconId, int iconColor) { this.mId = id; this.mStartTime = Calendar.getInstance(); @@ -51,35 +58,80 @@ public WeekViewEvent(long id, String name, int startYear, int startMonth, int st this.mEndTime.set(Calendar.MINUTE, endMinute); this.mName = name; + + this.mIconId = iconId; + this.mIconColor = iconColor; + } + + /** + * Initializes the event for week view. + * + * @param id The id of the event. + * @param name Name of the event. + * @param startYear Year when the event starts. + * @param startMonth Month when the event starts. + * @param startDay Day when the event starts. + * @param startHour Hour (in 24-hour format) when the event starts. + * @param startMinute Minute when the event starts. + * @param endYear Year when the event ends. + * @param endMonth Month when the event ends. + * @param endDay Day when the event ends. + * @param endHour Hour (in 24-hour format) when the event ends. + * @param endMinute Minute when the event ends. + */ + public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay, + int startHour, int startMinute, int endYear, int endMonth, int endDay, + int endHour, int endMinute) { + this(id, name, startYear, startMonth, startDay, startHour, startMinute, + endYear, endMonth, endDay, endHour, endMinute, 0, 0); } /** * Initializes the event for week view. + * * @param id The id of the event. * @param name Name of the event. * @param location The location of the event. * @param startTime The time when the event starts. * @param endTime The time when the event ends. + * @param iconId Icon associated at the event. + * @param iconColor Color of the icon associated at the event. */ - public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime) { + public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime, + int iconId, int iconColor) { this.mId = id; this.mName = name; this.mLocation = location; this.mStartTime = startTime; this.mEndTime = endTime; + this.mIconId = iconId; + this.mIconColor = iconColor; } /** * Initializes the event for week view. + * * @param id The id of the event. * @param name Name of the event. + * @param location The location of the event. * @param startTime The time when the event starts. * @param endTime The time when the event ends. */ - public WeekViewEvent(long id, String name, Calendar startTime, Calendar endTime) { - this(id, name, null, startTime, endTime); + public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime) { + this(id, name, location, startTime, endTime, 0, 0); } + /** + * Initializes the event for week view. + * + * @param id The id of the event. + * @param name Name of the event. + * @param startTime The time when the event starts. + * @param endTime The time when the event ends. + */ + public WeekViewEvent(long id, String name, Calendar startTime, Calendar endTime) { + this(id, name, null, startTime, endTime, 0, 0); + } public Calendar getStartTime() { return mStartTime; @@ -129,6 +181,22 @@ public void setId(long id) { this.mId = id; } + public int getIconId() { + return mIconId; + } + + public void setIconId(int iconId) { + this.mIconId = iconId; + } + + public int getIconColor() { + return mIconColor; + } + + public void setIconColor(int iconColor) { + this.mIconColor = iconColor; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -144,4 +212,4 @@ public boolean equals(Object o) { public int hashCode() { return (int) (mId ^ (mId >>> 32)); } -} +} \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 6db63dd96..b8df3a72a 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -15,6 +15,7 @@ +