Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2014 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 android.widget;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.graphics.Canvas;
     23 import android.graphics.Paint;
     24 import android.graphics.Typeface;
     25 import android.util.AttributeSet;
     26 
     27 import com.android.internal.R;
     28 
     29 class TextViewWithCircularIndicator extends TextView {
     30 
     31     private static final int SELECTED_CIRCLE_ALPHA = 60;
     32 
     33     private final Paint mCirclePaint = new Paint();
     34 
     35     private final String mItemIsSelectedText;
     36     private int mCircleColor;
     37     private boolean mDrawIndicator;
     38 
     39     public TextViewWithCircularIndicator(Context context) {
     40         this(context, null);
     41     }
     42 
     43     public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
     44         this(context, attrs, 0);
     45     }
     46 
     47     public TextViewWithCircularIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
     48         this(context, attrs, defStyleAttr, 0);
     49     }
     50 
     51     public TextViewWithCircularIndicator(Context context, AttributeSet attrs,
     52             int defStyleAttr, int defStyleRes) {
     53         super(context, attrs);
     54 
     55 
     56         // Use Theme attributes if possible
     57         final TypedArray a = mContext.obtainStyledAttributes(attrs,
     58                 R.styleable.DatePicker, defStyleAttr, defStyleRes);
     59         final int resId = a.getResourceId(R.styleable.DatePicker_yearListItemTextAppearance, -1);
     60         if (resId != -1) {
     61             setTextAppearance(context, resId);
     62         }
     63 
     64         final Resources res = context.getResources();
     65         mItemIsSelectedText = res.getString(R.string.item_is_selected);
     66 
     67         a.recycle();
     68 
     69         init();
     70     }
     71 
     72     private void init() {
     73         mCirclePaint.setTypeface(Typeface.create(mCirclePaint.getTypeface(), Typeface.BOLD));
     74         mCirclePaint.setAntiAlias(true);
     75         mCirclePaint.setTextAlign(Paint.Align.CENTER);
     76         mCirclePaint.setStyle(Paint.Style.FILL);
     77     }
     78 
     79     public void setCircleColor(int color) {
     80         if (color != mCircleColor) {
     81             mCircleColor = color;
     82             mCirclePaint.setColor(mCircleColor);
     83             mCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA);
     84             requestLayout();
     85         }
     86     }
     87 
     88     public void setDrawIndicator(boolean drawIndicator) {
     89         mDrawIndicator = drawIndicator;
     90     }
     91 
     92     @Override
     93     public void onDraw(Canvas canvas) {
     94         super.onDraw(canvas);
     95         if (mDrawIndicator) {
     96             final int width = getWidth();
     97             final int height = getHeight();
     98             int radius = Math.min(width, height) / 2;
     99             canvas.drawCircle(width / 2, height / 2, radius, mCirclePaint);
    100         }
    101     }
    102 
    103     @Override
    104     public CharSequence getContentDescription() {
    105         CharSequence itemText = getText();
    106         if (mDrawIndicator) {
    107             return String.format(mItemIsSelectedText, itemText);
    108         } else {
    109             return itemText;
    110         }
    111     }
    112 }