You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.9 KiB
166 lines
3.9 KiB
11 years ago
|
/*
|
||
|
* Copyright (C) 2010 Daniel Nilsson
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package net.margaritov.preference.colorpicker;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.graphics.Canvas;
|
||
|
import android.graphics.Paint;
|
||
|
import android.graphics.RectF;
|
||
|
import android.util.AttributeSet;
|
||
|
import android.view.View;
|
||
|
|
||
|
/**
|
||
|
* This class draws a panel which which will be filled with a color which can be set.
|
||
|
* It can be used to show the currently selected color which you will get from
|
||
|
* the {@link ColorPickerView}.
|
||
|
* @author Daniel Nilsson
|
||
|
*/
|
||
|
public class ColorPickerPanelView extends View {
|
||
|
|
||
|
/**
|
||
|
* The width in pixels of the border
|
||
|
* surrounding the color panel.
|
||
|
*/
|
||
10 years ago
|
private final static float BORDER_WIDTH_PX = 1;
|
||
11 years ago
|
|
||
|
private float mDensity = 1f;
|
||
|
|
||
10 years ago
|
private int mBorderColor = 0xff6E6E6E;
|
||
|
private int mColor = 0xff000000;
|
||
11 years ago
|
|
||
10 years ago
|
private Paint mBorderPaint;
|
||
|
private Paint mColorPaint;
|
||
11 years ago
|
|
||
10 years ago
|
private RectF mDrawingRect;
|
||
|
private RectF mColorRect;
|
||
11 years ago
|
|
||
|
private AlphaPatternDrawable mAlphaPattern;
|
||
|
|
||
|
|
||
10 years ago
|
public ColorPickerPanelView(Context context) {
|
||
11 years ago
|
this(context, null);
|
||
|
}
|
||
|
|
||
10 years ago
|
public ColorPickerPanelView(Context context, AttributeSet attrs) {
|
||
11 years ago
|
this(context, attrs, 0);
|
||
|
}
|
||
|
|
||
|
public ColorPickerPanelView(Context context, AttributeSet attrs, int defStyle) {
|
||
|
super(context, attrs, defStyle);
|
||
|
init();
|
||
|
}
|
||
|
|
||
10 years ago
|
private void init() {
|
||
11 years ago
|
mBorderPaint = new Paint();
|
||
|
mColorPaint = new Paint();
|
||
|
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
protected void onDraw(Canvas canvas) {
|
||
|
|
||
10 years ago
|
final RectF rect = mColorRect;
|
||
11 years ago
|
|
||
10 years ago
|
if (BORDER_WIDTH_PX > 0) {
|
||
11 years ago
|
mBorderPaint.setColor(mBorderColor);
|
||
|
canvas.drawRect(mDrawingRect, mBorderPaint);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (mAlphaPattern != null) {
|
||
11 years ago
|
mAlphaPattern.draw(canvas);
|
||
|
}
|
||
|
|
||
|
mColorPaint.setColor(mColor);
|
||
|
|
||
|
canvas.drawRect(rect, mColorPaint);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||
|
|
||
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
||
|
int height = MeasureSpec.getSize(heightMeasureSpec);
|
||
|
|
||
|
setMeasuredDimension(width, height);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||
|
super.onSizeChanged(w, h, oldw, oldh);
|
||
|
|
||
|
mDrawingRect = new RectF();
|
||
10 years ago
|
mDrawingRect.left = getPaddingLeft();
|
||
|
mDrawingRect.right = w - getPaddingRight();
|
||
11 years ago
|
mDrawingRect.top = getPaddingTop();
|
||
|
mDrawingRect.bottom = h - getPaddingBottom();
|
||
|
|
||
|
setUpColorRect();
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private void setUpColorRect() {
|
||
|
final RectF dRect = mDrawingRect;
|
||
11 years ago
|
|
||
|
float left = dRect.left + BORDER_WIDTH_PX;
|
||
|
float top = dRect.top + BORDER_WIDTH_PX;
|
||
|
float bottom = dRect.bottom - BORDER_WIDTH_PX;
|
||
|
float right = dRect.right - BORDER_WIDTH_PX;
|
||
|
|
||
10 years ago
|
mColorRect = new RectF(left, top, right, bottom);
|
||
11 years ago
|
|
||
10 years ago
|
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity));
|
||
11 years ago
|
|
||
10 years ago
|
mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top), Math.round(mColorRect.right),
|
||
|
Math.round(mColorRect.bottom));
|
||
11 years ago
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the color that should be shown by this view.
|
||
|
* @param color
|
||
|
*/
|
||
10 years ago
|
public void setColor(int color) {
|
||
11 years ago
|
mColor = color;
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the color currently show by this view.
|
||
|
* @return
|
||
|
*/
|
||
10 years ago
|
public int getColor() {
|
||
11 years ago
|
return mColor;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the color of the border surrounding the panel.
|
||
|
* @param color
|
||
|
*/
|
||
10 years ago
|
public void setBorderColor(int color) {
|
||
11 years ago
|
mBorderColor = color;
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the color of the border surrounding the panel.
|
||
|
*/
|
||
10 years ago
|
public int getBorderColor() {
|
||
11 years ago
|
return mBorderColor;
|
||
|
}
|
||
|
|
||
|
}
|