DayOfMonthDrawable.java revision 7be65caf4efa310c1b28cc74278eb0f8327039c8
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 17package com.android.calendar; 18 19import android.content.Context; 20import android.graphics.Canvas; 21import android.graphics.ColorFilter; 22import android.graphics.Paint; 23import android.graphics.PixelFormat; 24import android.graphics.Rect; 25import android.graphics.Typeface; 26import android.graphics.drawable.Drawable; 27 28 29 30 31/** 32 * A custom view to draw the day of the month in the today button in the options menu 33 */ 34 35public class DayOfMonthDrawable extends Drawable { 36 37 private String mDayOfMonth = "1"; 38 private final Paint mPaint; 39 private final Rect mTextBounds = new Rect(); 40 private static float mTextSize = 14; 41 42 public DayOfMonthDrawable(Context c) { 43 mTextSize = c.getResources().getDimension(R.dimen.today_icon_text_size); 44 mPaint = new Paint(); 45 mPaint.setAlpha(255); 46 mPaint.setColor(0xFF777777); 47 mPaint.setTypeface(Typeface.DEFAULT_BOLD); 48 mPaint.setTextSize(mTextSize); 49 } 50 51 @Override 52 public void draw(Canvas canvas) { 53 mPaint.getTextBounds(mDayOfMonth, 0, mDayOfMonth.length(), mTextBounds); 54 int textWidth = mTextBounds.right - mTextBounds.left; 55 int textHeight = mTextBounds.bottom - mTextBounds.top; 56 Rect bounds = getBounds(); 57 canvas.drawText(mDayOfMonth, ((float)(bounds.right - textWidth)) / 2, 58 ((float)bounds.bottom + textHeight) / 2, mPaint); 59 } 60 61 @Override 62 public void setAlpha(int alpha) { 63 mPaint.setAlpha(alpha); 64 } 65 66 @Override 67 public void setColorFilter(ColorFilter cf) { 68 // Ignore 69 } 70 71 @Override 72 public int getOpacity() { 73 return PixelFormat.UNKNOWN; 74 } 75 76 public void setDayOfMonth(int day) { 77 mDayOfMonth = Integer.toString(day); 78 invalidateSelf(); 79 } 80} 81