Home | History | Annotate | Download | only in ui
      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.gallery3d.filtershow.ui;
     18 
     19 import android.content.Context;
     20 import android.content.res.TypedArray;
     21 import android.graphics.Canvas;
     22 import android.graphics.Paint;
     23 import android.graphics.Path;
     24 import android.graphics.Rect;
     25 import android.util.AttributeSet;
     26 import android.widget.ImageButton;
     27 
     28 import com.android.gallery3d.R;
     29 
     30 public class FramedTextButton extends ImageButton {
     31     private static final String LOGTAG = "FramedTextButton";
     32     private String mText = null;
     33     private static int mTextSize = 24;
     34     private static int mTextPadding = 20;
     35     private static Paint gPaint = new Paint();
     36     private static Path gPath = new Path();
     37     private static int mTrianglePadding = 2;
     38     private static int mTriangleSize = 30;
     39 
     40     public static void setTextSize(int value) {
     41         mTextSize = value;
     42     }
     43 
     44     public static void setTextPadding(int value) {
     45         mTextPadding = value;
     46     }
     47 
     48     public static void setTrianglePadding(int value) {
     49         mTrianglePadding = value;
     50     }
     51 
     52     public static void setTriangleSize(int value) {
     53         mTriangleSize = value;
     54     }
     55 
     56     public void setText(String text) {
     57         mText = text;
     58         invalidate();
     59     }
     60 
     61     public void setTextFrom(int itemId) {
     62         switch (itemId) {
     63             case R.id.curve_menu_rgb: {
     64                 setText(getContext().getString(R.string.curves_channel_rgb));
     65                 break;
     66             }
     67             case R.id.curve_menu_red: {
     68                 setText(getContext().getString(R.string.curves_channel_red));
     69                 break;
     70             }
     71             case R.id.curve_menu_green: {
     72                 setText(getContext().getString(R.string.curves_channel_green));
     73                 break;
     74             }
     75             case R.id.curve_menu_blue: {
     76                 setText(getContext().getString(R.string.curves_channel_blue));
     77                 break;
     78             }
     79         }
     80         invalidate();
     81     }
     82 
     83     public FramedTextButton(Context context) {
     84         this(context, null);
     85     }
     86 
     87     public FramedTextButton(Context context, AttributeSet attrs) {
     88         super(context, attrs);
     89         if (attrs == null) {
     90             return;
     91         }
     92         TypedArray a = getContext().obtainStyledAttributes(
     93                 attrs, R.styleable.ImageButtonTitle);
     94 
     95         mText = a.getString(R.styleable.ImageButtonTitle_android_text);
     96     }
     97 
     98     public String getText(){
     99         return mText;
    100     }
    101 
    102     @Override
    103     public void onDraw(Canvas canvas) {
    104         gPaint.setARGB(96, 255, 255, 255);
    105         gPaint.setStrokeWidth(2);
    106         gPaint.setStyle(Paint.Style.STROKE);
    107         int w = getWidth();
    108         int h = getHeight();
    109         canvas.drawRect(mTextPadding, mTextPadding, w - mTextPadding,
    110                 h - mTextPadding, gPaint);
    111         gPath.reset();
    112         gPath.moveTo(w - mTextPadding - mTrianglePadding - mTriangleSize,
    113                      h - mTextPadding - mTrianglePadding);
    114         gPath.lineTo(w - mTextPadding - mTrianglePadding,
    115                      h - mTextPadding - mTrianglePadding - mTriangleSize);
    116         gPath.lineTo(w - mTextPadding - mTrianglePadding,
    117                      h - mTextPadding - mTrianglePadding);
    118         gPath.close();
    119         gPaint.setARGB(128, 255, 255, 255);
    120         gPaint.setStrokeWidth(1);
    121         gPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    122         canvas.drawPath(gPath, gPaint);
    123         if (mText != null) {
    124             gPaint.reset();
    125             gPaint.setARGB(255, 255, 255, 255);
    126             gPaint.setTextSize(mTextSize);
    127             float textWidth = gPaint.measureText(mText);
    128             Rect bounds = new Rect();
    129             gPaint.getTextBounds(mText, 0, mText.length(), bounds);
    130             int x = (int) ((w - textWidth) / 2);
    131             int y = (h + bounds.height()) / 2;
    132 
    133             canvas.drawText(mText, x, y, gPaint);
    134         }
    135     }
    136 
    137 }
    138