OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / widgets / ColorPanelView.java
1 /*
2  * Copyright (C) 2010 Daniel Nilsson Copyright (C) 2012 THe CyanogenMod Project
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4  * use this file except in compliance with the License. You may obtain a copy of
5  * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6  * applicable law or agreed to in writing, software distributed under the
7  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8  * OF ANY KIND, either express or implied. See the License for the specific
9  * language governing permissions and limitations under the License.
10  */
11
12 package com.cyngn.eleven.widgets;
13
14 import android.content.Context;
15 import android.graphics.Canvas;
16 import android.graphics.Paint;
17 import android.graphics.RectF;
18 import android.util.AttributeSet;
19 import android.view.View;
20
21 /**
22  * This class draws a panel which which will be filled with a color which can be
23  * set. It can be used to show the currently selected color which you will get
24  * from the {@link ColorPickerView}.
25  * 
26  * @author Daniel Nilsson
27  */
28 public class ColorPanelView extends View {
29
30     /**
31      * The width in pixels of the border surrounding the color panel.
32      */
33     private final static float BORDER_WIDTH_PX = 1;
34
35     private static float mDensity = 1f;
36
37     private int mBorderColor = 0xff6E6E6E;
38
39     private int mColor = 0xff000000;
40
41     private Paint mBorderPaint;
42
43     private Paint mColorPaint;
44
45     private RectF mDrawingRect;
46
47     private RectF mColorRect;
48
49     private AlphaPatternDrawable mAlphaPattern;
50
51     public ColorPanelView(final Context context) {
52         this(context, null);
53     }
54
55     public ColorPanelView(final Context context, final AttributeSet attrs) {
56         this(context, attrs, 0);
57     }
58
59     public ColorPanelView(final Context context, final AttributeSet attrs, final int defStyle) {
60         super(context, attrs, defStyle);
61         init();
62     }
63
64     private void init() {
65         mBorderPaint = new Paint();
66         mColorPaint = new Paint();
67         mDensity = getContext().getResources().getDisplayMetrics().density;
68     }
69
70     /**
71      * {@inheritDoc}
72      */
73     @Override
74     protected void onDraw(final Canvas canvas) {
75         final RectF rect = mColorRect;
76         if (BORDER_WIDTH_PX > 0) {
77             mBorderPaint.setColor(mBorderColor);
78             canvas.drawRect(mDrawingRect, mBorderPaint);
79         }
80
81         if (mAlphaPattern != null) {
82             mAlphaPattern.draw(canvas);
83         }
84
85         mColorPaint.setColor(mColor);
86         canvas.drawRect(rect, mColorPaint);
87     }
88
89     /**
90      * {@inheritDoc}
91      */
92     @Override
93     protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
94         final int width = MeasureSpec.getSize(widthMeasureSpec);
95         final int height = MeasureSpec.getSize(heightMeasureSpec);
96         setMeasuredDimension(width, height);
97     }
98
99     /**
100      * {@inheritDoc}
101      */
102     @Override
103     protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
104         super.onSizeChanged(w, h, oldw, oldh);
105         mDrawingRect = new RectF();
106         mDrawingRect.left = getPaddingLeft();
107         mDrawingRect.right = w - getPaddingRight();
108         mDrawingRect.top = getPaddingTop();
109         mDrawingRect.bottom = h - getPaddingBottom();
110
111         setUpColorRect();
112     }
113
114     private void setUpColorRect() {
115         final RectF dRect = mDrawingRect;
116
117         final float left = dRect.left + BORDER_WIDTH_PX;
118         final float top = dRect.top + BORDER_WIDTH_PX;
119         final float bottom = dRect.bottom - BORDER_WIDTH_PX;
120         final float right = dRect.right - BORDER_WIDTH_PX;
121
122         mColorRect = new RectF(left, top, right, bottom);
123
124         mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity));
125
126         mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top),
127                 Math.round(mColorRect.right), Math.round(mColorRect.bottom));
128     }
129
130     /**
131      * Set the color that should be shown by this view.
132      * 
133      * @param color
134      */
135     public void setColor(final int color) {
136         mColor = color;
137         invalidate();
138     }
139
140     /**
141      * Get the color currently show by this view.
142      * 
143      * @return
144      */
145     public int getColor() {
146         return mColor;
147     }
148
149     /**
150      * Set the color of the border surrounding the panel.
151      * 
152      * @param color
153      */
154     public void setBorderColor(final int color) {
155         mBorderColor = color;
156         invalidate();
157     }
158
159     /**
160      * Get the color of the border surrounding the panel.
161      */
162     public int getBorderColor() {
163         return mBorderColor;
164     }
165
166 }