Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.calendar; |
| 18 | |
Isaac Katzenelson | 7be65ca | 2012-05-07 18:06:34 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 21 | import android.graphics.ColorFilter; |
| 22 | import android.graphics.Paint; |
| 23 | import android.graphics.PixelFormat; |
| 24 | import android.graphics.Rect; |
Isaac Katzenelson | 7be65ca | 2012-05-07 18:06:34 -0700 | [diff] [blame] | 25 | import android.graphics.Typeface; |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 27 | |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 28 | /** |
| 29 | * A custom view to draw the day of the month in the today button in the options menu |
| 30 | */ |
| 31 | |
| 32 | public class DayOfMonthDrawable extends Drawable { |
| 33 | |
Isaac Katzenelson | 7be65ca | 2012-05-07 18:06:34 -0700 | [diff] [blame] | 34 | private String mDayOfMonth = "1"; |
| 35 | private final Paint mPaint; |
| 36 | private final Rect mTextBounds = new Rect(); |
| 37 | private static float mTextSize = 14; |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 38 | |
Isaac Katzenelson | 7be65ca | 2012-05-07 18:06:34 -0700 | [diff] [blame] | 39 | public DayOfMonthDrawable(Context c) { |
| 40 | mTextSize = c.getResources().getDimension(R.dimen.today_icon_text_size); |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 41 | mPaint = new Paint(); |
| 42 | mPaint.setAlpha(255); |
| 43 | mPaint.setColor(0xFF777777); |
Isaac Katzenelson | 7be65ca | 2012-05-07 18:06:34 -0700 | [diff] [blame] | 44 | mPaint.setTypeface(Typeface.DEFAULT_BOLD); |
| 45 | mPaint.setTextSize(mTextSize); |
Isaac Katzenelson | 407b2b4 | 2012-05-11 13:30:07 -0700 | [diff] [blame] | 46 | mPaint.setTextAlign(Paint.Align.CENTER); |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void draw(Canvas canvas) { |
| 51 | mPaint.getTextBounds(mDayOfMonth, 0, mDayOfMonth.length(), mTextBounds); |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 52 | int textHeight = mTextBounds.bottom - mTextBounds.top; |
| 53 | Rect bounds = getBounds(); |
Isaac Katzenelson | 407b2b4 | 2012-05-11 13:30:07 -0700 | [diff] [blame] | 54 | canvas.drawText(mDayOfMonth, bounds.right / 2, ((float) bounds.bottom + textHeight + 1) / 2, |
| 55 | mPaint); |
Isaac Katzenelson | 4bd4a5c | 2012-03-20 11:02:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void setAlpha(int alpha) { |
| 60 | mPaint.setAlpha(alpha); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void setColorFilter(ColorFilter cf) { |
| 65 | // Ignore |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int getOpacity() { |
| 70 | return PixelFormat.UNKNOWN; |
| 71 | } |
| 72 | |
| 73 | public void setDayOfMonth(int day) { |
| 74 | mDayOfMonth = Integer.toString(day); |
| 75 | invalidateSelf(); |
| 76 | } |
| 77 | } |