CircleView.java revision b8f95646fc0510eebfeaa27864023d630f34090f
1/* 2 * Copyright (C) 2013 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.datetimepicker.time; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.graphics.Canvas; 22import android.graphics.Paint; 23import android.util.Log; 24import android.view.View; 25 26import com.android.datetimepicker.R; 27 28public class CircleView extends View { 29 private static final String TAG = "CircleView"; 30 31 private final Paint mPaint = new Paint(); 32 private boolean mIs24HourMode; 33 private int mWhite; 34 private int mBlack80; 35 private float mCircleRadiusMultiplier; 36 private float mAmPmCircleRadiusMultiplier; 37 private boolean mIsInitialized; 38 39 private boolean mDrawValuesReady; 40 private int mXCenter; 41 private int mYCenter; 42 private int mCircleRadius; 43 44 public CircleView(Context context) { 45 super(context); 46 47 Resources res = context.getResources(); 48 mWhite = res.getColor(R.color.white); 49 mBlack80 = res.getColor(R.color.black_80); 50 mPaint.setAntiAlias(true); 51 52 mIsInitialized = false; 53 } 54 55 public void initialize(Context context, boolean is24HourMode) { 56 if (mIsInitialized) { 57 Log.e(TAG, "CircleView may only be initialized once."); 58 return; 59 } 60 61 Resources res = context.getResources(); 62 mIs24HourMode = is24HourMode; 63 if (is24HourMode) { 64 mCircleRadiusMultiplier = Float.parseFloat( 65 res.getString(R.string.circle_radius_multiplier_24HourMode)); 66 } else { 67 mCircleRadiusMultiplier = Float.parseFloat( 68 res.getString(R.string.circle_radius_multiplier)); 69 mAmPmCircleRadiusMultiplier = 70 Float.parseFloat(res.getString(R.string.ampm_circle_radius_multiplier)); 71 } 72 73 mIsInitialized = true; 74 } 75 76 77 @Override 78 public void onDraw(Canvas canvas) { 79 int viewWidth = getWidth(); 80 if (viewWidth == 0 || !mIsInitialized) { 81 return; 82 } 83 84 if (!mDrawValuesReady) { 85 mXCenter = getWidth() / 2; 86 mYCenter = getHeight() / 2; 87 mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier); 88 89 if (!mIs24HourMode) { 90 // We'll need to draw the AM/PM circles, so the main circle will need to have 91 // a slightly higher center. To keep the entire view centered vertically, we'll 92 // have to push it up by half the radius of the AM/PM circles. 93 int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier); 94 mYCenter -= amPmCircleRadius / 2; 95 } 96 97 mDrawValuesReady = true; 98 } 99 100 mPaint.setColor(mWhite); 101 canvas.drawCircle(mXCenter, mYCenter, mCircleRadius, mPaint); 102 103 mPaint.setColor(mBlack80); 104 canvas.drawCircle(mXCenter, mYCenter, 2, mPaint); 105 } 106} 107