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 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.ColorFilter;
22 import android.graphics.Paint;
23 import android.graphics.PixelFormat;
24 import android.graphics.Rect;
25 import android.graphics.Typeface;
26 import android.graphics.drawable.Drawable;
27 
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 
34     private String mDayOfMonth = "1";
35     private final Paint mPaint;
36     private final Rect mTextBounds = new Rect();
37     private static float mTextSize = 14;
38 
DayOfMonthDrawable(Context c)39     public DayOfMonthDrawable(Context c) {
40         mTextSize = c.getResources().getDimension(R.dimen.today_icon_text_size);
41         mPaint = new Paint();
42         mPaint.setAlpha(255);
43         mPaint.setColor(0xFF777777);
44         mPaint.setTypeface(Typeface.DEFAULT_BOLD);
45         mPaint.setTextSize(mTextSize);
46         mPaint.setTextAlign(Paint.Align.CENTER);
47     }
48 
49     @Override
draw(Canvas canvas)50     public void draw(Canvas canvas) {
51         mPaint.getTextBounds(mDayOfMonth, 0, mDayOfMonth.length(), mTextBounds);
52         int textHeight = mTextBounds.bottom - mTextBounds.top;
53         Rect bounds = getBounds();
54         canvas.drawText(mDayOfMonth, bounds.right / 2, ((float) bounds.bottom + textHeight + 1) / 2,
55                 mPaint);
56     }
57 
58     @Override
setAlpha(int alpha)59     public void setAlpha(int alpha) {
60         mPaint.setAlpha(alpha);
61     }
62 
63     @Override
setColorFilter(ColorFilter cf)64     public void setColorFilter(ColorFilter cf) {
65         // Ignore
66     }
67 
68     @Override
getOpacity()69     public int getOpacity() {
70         return PixelFormat.UNKNOWN;
71     }
72 
setDayOfMonth(int day)73     public void setDayOfMonth(int day) {
74         mDayOfMonth = Integer.toString(day);
75         invalidateSelf();
76     }
77 }
78