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.
12 package com.cyngn.eleven.widgets;
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;
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}.
26 * @author Daniel Nilsson
28 public class ColorPanelView extends View {
31 * The width in pixels of the border surrounding the color panel.
33 private final static float BORDER_WIDTH_PX = 1;
35 private static float mDensity = 1f;
37 private int mBorderColor = 0xff6E6E6E;
39 private int mColor = 0xff000000;
41 private Paint mBorderPaint;
43 private Paint mColorPaint;
45 private RectF mDrawingRect;
47 private RectF mColorRect;
49 private AlphaPatternDrawable mAlphaPattern;
51 public ColorPanelView(final Context context) {
55 public ColorPanelView(final Context context, final AttributeSet attrs) {
56 this(context, attrs, 0);
59 public ColorPanelView(final Context context, final AttributeSet attrs, final int defStyle) {
60 super(context, attrs, defStyle);
65 mBorderPaint = new Paint();
66 mColorPaint = new Paint();
67 mDensity = getContext().getResources().getDisplayMetrics().density;
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);
81 if (mAlphaPattern != null) {
82 mAlphaPattern.draw(canvas);
85 mColorPaint.setColor(mColor);
86 canvas.drawRect(rect, mColorPaint);
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);
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();
114 private void setUpColorRect() {
115 final RectF dRect = mDrawingRect;
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;
122 mColorRect = new RectF(left, top, right, bottom);
124 mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity));
126 mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top),
127 Math.round(mColorRect.right), Math.round(mColorRect.bottom));
131 * Set the color that should be shown by this view.
135 public void setColor(final int color) {
141 * Get the color currently show by this view.
145 public int getColor() {
150 * Set the color of the border surrounding the panel.
154 public void setBorderColor(final int color) {
155 mBorderColor = color;
160 * Get the color of the border surrounding the panel.
162 public int getBorderColor() {