mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2024-11-27 06:10:28 +01:00
White space changes and warning fixes in color picker code
This commit is contained in:
parent
421aeb8a30
commit
8789e8f9c0
|
@ -1,129 +1,111 @@
|
|||
/*
|
||||
* 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.graphics.Bitmap;
|
||||
import android.graphics.Bitmap.Config;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
/**
|
||||
* This drawable that draws a simple white and gray chessboard pattern.
|
||||
* It's pattern you will often see as a background behind a
|
||||
* partly transparent image in many applications.
|
||||
* @author Daniel Nilsson
|
||||
*/
|
||||
public class AlphaPatternDrawable extends Drawable {
|
||||
|
||||
private int mRectangleSize = 10;
|
||||
|
||||
private Paint mPaint = new Paint();
|
||||
private Paint mPaintWhite = new Paint();
|
||||
private Paint mPaintGray = new Paint();
|
||||
|
||||
private int numRectanglesHorizontal;
|
||||
private int numRectanglesVertical;
|
||||
|
||||
/**
|
||||
* Bitmap in which the pattern will be cahched.
|
||||
*/
|
||||
private Bitmap mBitmap;
|
||||
|
||||
public AlphaPatternDrawable(int rectangleSize) {
|
||||
mRectangleSize = rectangleSize;
|
||||
mPaintWhite.setColor(0xffffffff);
|
||||
mPaintGray.setColor(0xffcbcbcb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBoundsChange(Rect bounds) {
|
||||
super.onBoundsChange(bounds);
|
||||
|
||||
int height = bounds.height();
|
||||
int width = bounds.width();
|
||||
|
||||
numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));
|
||||
numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);
|
||||
|
||||
generatePatternBitmap();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This will generate a bitmap with the pattern
|
||||
* as big as the rectangle we were allow to draw on.
|
||||
* We do this to chache the bitmap so we don't need to
|
||||
* recreate it each time draw() is called since it
|
||||
* takes a few milliseconds.
|
||||
*/
|
||||
private void generatePatternBitmap(){
|
||||
|
||||
if(getBounds().width() <= 0 || getBounds().height() <= 0){
|
||||
return;
|
||||
}
|
||||
|
||||
mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(mBitmap);
|
||||
|
||||
Rect r = new Rect();
|
||||
boolean verticalStartWhite = true;
|
||||
for (int i = 0; i <= numRectanglesVertical; i++) {
|
||||
|
||||
boolean isWhite = verticalStartWhite;
|
||||
for (int j = 0; j <= numRectanglesHorizontal; j++) {
|
||||
|
||||
r.top = i * mRectangleSize;
|
||||
r.left = j * mRectangleSize;
|
||||
r.bottom = r.top + mRectangleSize;
|
||||
r.right = r.left + mRectangleSize;
|
||||
|
||||
canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);
|
||||
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
|
||||
verticalStartWhite = !verticalStartWhite;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.graphics.Bitmap;
|
||||
import android.graphics.Bitmap.Config;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
/**
|
||||
* This drawable that draws a simple white and gray chessboard pattern.
|
||||
* It's pattern you will often see as a background behind a
|
||||
* partly transparent image in many applications.
|
||||
* @author Daniel Nilsson
|
||||
*/
|
||||
public class AlphaPatternDrawable extends Drawable {
|
||||
|
||||
private int mRectangleSize;
|
||||
|
||||
private Paint mPaint = new Paint();
|
||||
private Paint mPaintWhite = new Paint();
|
||||
private Paint mPaintGray = new Paint();
|
||||
|
||||
private int numRectanglesHorizontal;
|
||||
private int numRectanglesVertical;
|
||||
|
||||
/**
|
||||
* Bitmap in which the pattern will be cached.
|
||||
*/
|
||||
private Bitmap mBitmap;
|
||||
|
||||
public AlphaPatternDrawable(int rectangleSize) {
|
||||
mRectangleSize = rectangleSize;
|
||||
mPaintWhite.setColor(0xffffffff);
|
||||
mPaintGray.setColor(0xffcbcbcb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
throw new UnsupportedOperationException("Alpha is not supported by this drawable.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
throw new UnsupportedOperationException("ColorFilter is not supported by this drawable.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBoundsChange(Rect bounds) {
|
||||
super.onBoundsChange(bounds);
|
||||
numRectanglesHorizontal = bounds.width() / mRectangleSize;
|
||||
numRectanglesVertical = bounds.height() / mRectangleSize;
|
||||
generatePatternBitmap();
|
||||
}
|
||||
|
||||
/**
|
||||
* This will generate a bitmap with the pattern
|
||||
* as big as the rectangle we were allow to draw on.
|
||||
* We do this to cache the bitmap so we don't need to
|
||||
* recreate it each time draw() is called since it
|
||||
* takes a few milliseconds.
|
||||
*/
|
||||
private void generatePatternBitmap() {
|
||||
if (getBounds().width() <= 0 || getBounds().height() <= 0)
|
||||
return;
|
||||
|
||||
mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(mBitmap);
|
||||
|
||||
Rect r = new Rect();
|
||||
for (int i = 0; i <= numRectanglesVertical; i++) {
|
||||
for (int j = 0; j <= numRectanglesHorizontal; j++) {
|
||||
boolean isWhite = (i + j) % 2 == 0;
|
||||
r.top = i * mRectangleSize;
|
||||
r.left = j * mRectangleSize;
|
||||
r.bottom = r.top + mRectangleSize;
|
||||
r.right = r.left + mRectangleSize;
|
||||
|
||||
canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,142 +1,138 @@
|
|||
/*
|
||||
* 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 org.petero.droidfish.R;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public class ColorPickerDialog
|
||||
extends
|
||||
Dialog
|
||||
implements
|
||||
ColorPickerView.OnColorChangedListener,
|
||||
View.OnClickListener {
|
||||
|
||||
private ColorPickerView mColorPicker;
|
||||
|
||||
private ColorPickerPanelView mOldColor;
|
||||
private ColorPickerPanelView mNewColor;
|
||||
|
||||
private OnColorChangedListener mListener;
|
||||
|
||||
private CharSequence additionalInfo;
|
||||
|
||||
public interface OnColorChangedListener {
|
||||
public void onColorChanged(int color);
|
||||
}
|
||||
|
||||
public ColorPickerDialog(Context context, int initialColor,
|
||||
CharSequence additionalInfo) {
|
||||
super(context);
|
||||
this.additionalInfo = additionalInfo;
|
||||
init(initialColor);
|
||||
}
|
||||
|
||||
private void init(int color) {
|
||||
// To fight color banding.
|
||||
getWindow().setFormat(PixelFormat.RGBA_8888);
|
||||
|
||||
setUp(color);
|
||||
|
||||
}
|
||||
|
||||
private void setUp(int color) {
|
||||
setContentView(R.layout.dialog_color_picker);
|
||||
|
||||
setTitle(getContext().getText(R.string.prefs_colors_title) + " '"
|
||||
+ additionalInfo + "'");
|
||||
|
||||
mColorPicker = findViewById(R.id.color_picker_view);
|
||||
mOldColor = findViewById(R.id.old_color_panel);
|
||||
mNewColor = findViewById(R.id.new_color_panel);
|
||||
|
||||
((LinearLayout) mOldColor.getParent()).setPadding(
|
||||
Math.round(mColorPicker.getDrawingOffset()),
|
||||
0,
|
||||
Math.round(mColorPicker.getDrawingOffset()),
|
||||
0
|
||||
);
|
||||
|
||||
mOldColor.setOnClickListener(this);
|
||||
mNewColor.setOnClickListener(this);
|
||||
mColorPicker.setOnColorChangedListener(this);
|
||||
mOldColor.setColor(color);
|
||||
mColorPicker.setColor(color, true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onColorChanged(int color) {
|
||||
|
||||
mNewColor.setColor(color);
|
||||
|
||||
/*
|
||||
if (mListener != null) {
|
||||
mListener.onColorChanged(color);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
public void setAlphaSliderVisible(boolean visible) {
|
||||
mColorPicker.setAlphaSliderVisible(visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a OnColorChangedListener to get notified when the color
|
||||
* selected by the user has changed.
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener){
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
return mColorPicker.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v.getId() == R.id.new_color_panel) {
|
||||
if (mListener != null) {
|
||||
mListener.onColorChanged(mNewColor.getColor());
|
||||
}
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle onSaveInstanceState() {
|
||||
Bundle state = super.onSaveInstanceState();
|
||||
state.putInt("old_color", mOldColor.getColor());
|
||||
state.putInt("new_color", mNewColor.getColor());
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
mOldColor.setColor(savedInstanceState.getInt("old_color"));
|
||||
mColorPicker.setColor(savedInstanceState.getInt("new_color"), true);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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 org.petero.droidfish.R;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public class ColorPickerDialog
|
||||
extends
|
||||
Dialog
|
||||
implements
|
||||
ColorPickerView.OnColorChangedListener,
|
||||
View.OnClickListener {
|
||||
|
||||
private ColorPickerView mColorPicker;
|
||||
|
||||
private ColorPickerPanelView mOldColor;
|
||||
private ColorPickerPanelView mNewColor;
|
||||
|
||||
private OnColorChangedListener mListener;
|
||||
|
||||
private CharSequence additionalInfo;
|
||||
|
||||
public interface OnColorChangedListener {
|
||||
public void onColorChanged(int color);
|
||||
}
|
||||
|
||||
public ColorPickerDialog(Context context, int initialColor,
|
||||
CharSequence additionalInfo) {
|
||||
super(context);
|
||||
this.additionalInfo = additionalInfo;
|
||||
init(initialColor);
|
||||
}
|
||||
|
||||
private void init(int color) {
|
||||
// To fight color banding.
|
||||
getWindow().setFormat(PixelFormat.RGBA_8888);
|
||||
|
||||
setUp(color);
|
||||
}
|
||||
|
||||
private void setUp(int color) {
|
||||
setContentView(R.layout.dialog_color_picker);
|
||||
|
||||
setTitle(getContext().getText(R.string.prefs_colors_title) + " '"
|
||||
+ additionalInfo + "'");
|
||||
|
||||
mColorPicker = findViewById(R.id.color_picker_view);
|
||||
mOldColor = findViewById(R.id.old_color_panel);
|
||||
mNewColor = findViewById(R.id.new_color_panel);
|
||||
|
||||
((LinearLayout) mOldColor.getParent()).setPadding(
|
||||
Math.round(mColorPicker.getDrawingOffset()),
|
||||
0,
|
||||
Math.round(mColorPicker.getDrawingOffset()),
|
||||
0
|
||||
);
|
||||
|
||||
mOldColor.setOnClickListener(this);
|
||||
mNewColor.setOnClickListener(this);
|
||||
mColorPicker.setOnColorChangedListener(this);
|
||||
mOldColor.setColor(color);
|
||||
mColorPicker.setColor(color, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onColorChanged(int color) {
|
||||
mNewColor.setColor(color);
|
||||
|
||||
/*
|
||||
if (mListener != null) {
|
||||
mListener.onColorChanged(color);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public void setAlphaSliderVisible(boolean visible) {
|
||||
mColorPicker.setAlphaSliderVisible(visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a OnColorChangedListener to get notified when the color
|
||||
* selected by the user has changed.
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
return mColorPicker.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v.getId() == R.id.new_color_panel) {
|
||||
if (mListener != null) {
|
||||
mListener.onColorChanged(mNewColor.getColor());
|
||||
}
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle onSaveInstanceState() {
|
||||
Bundle state = super.onSaveInstanceState();
|
||||
state.putInt("old_color", mOldColor.getColor());
|
||||
state.putInt("new_color", mNewColor.getColor());
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
mOldColor.setColor(savedInstanceState.getInt("old_color"));
|
||||
mColorPicker.setColor(savedInstanceState.getInt("new_color"), true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,171 +1,164 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
private final static float BORDER_WIDTH_PX = 1;
|
||||
|
||||
private float mDensity = 1f;
|
||||
|
||||
private int mBorderColor = 0xff6E6E6E;
|
||||
private int mColor = 0xff000000;
|
||||
|
||||
private Paint mBorderPaint;
|
||||
private Paint mColorPaint;
|
||||
|
||||
private RectF mDrawingRect;
|
||||
private RectF mColorRect;
|
||||
|
||||
private AlphaPatternDrawable mAlphaPattern;
|
||||
|
||||
|
||||
public ColorPickerPanelView(Context context){
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs){
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
mBorderPaint = new Paint();
|
||||
mColorPaint = new Paint();
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
|
||||
final RectF rect = mColorRect;
|
||||
|
||||
if(BORDER_WIDTH_PX > 0){
|
||||
mBorderPaint.setColor(mBorderColor);
|
||||
canvas.drawRect(mDrawingRect, mBorderPaint);
|
||||
}
|
||||
|
||||
if(mAlphaPattern != null){
|
||||
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();
|
||||
mDrawingRect.left = getPaddingLeft();
|
||||
mDrawingRect.right = w - getPaddingRight();
|
||||
mDrawingRect.top = getPaddingTop();
|
||||
mDrawingRect.bottom = h - getPaddingBottom();
|
||||
|
||||
setUpColorRect();
|
||||
|
||||
}
|
||||
|
||||
private void setUpColorRect(){
|
||||
final RectF dRect = mDrawingRect;
|
||||
|
||||
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;
|
||||
|
||||
mColorRect = new RectF(left,top, right, bottom);
|
||||
|
||||
mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity));
|
||||
|
||||
mAlphaPattern.setBounds(
|
||||
Math.round(mColorRect.left),
|
||||
Math.round(mColorRect.top),
|
||||
Math.round(mColorRect.right),
|
||||
Math.round(mColorRect.bottom)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color that should be shown by this view.
|
||||
* @param color
|
||||
*/
|
||||
public void setColor(int color){
|
||||
mColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color currently show by this view.
|
||||
* @return
|
||||
*/
|
||||
public int getColor(){
|
||||
return mColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color of the border surrounding the panel.
|
||||
* @param color
|
||||
*/
|
||||
public void setBorderColor(int color){
|
||||
mBorderColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color of the border surrounding the panel.
|
||||
*/
|
||||
public int getBorderColor(){
|
||||
return mBorderColor;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
private final static float BORDER_WIDTH_PX = 1;
|
||||
|
||||
private float mDensity = 1f;
|
||||
|
||||
private int mBorderColor = 0xff6E6E6E;
|
||||
private int mColor = 0xff000000;
|
||||
|
||||
private Paint mBorderPaint;
|
||||
private Paint mColorPaint;
|
||||
|
||||
private RectF mDrawingRect;
|
||||
private RectF mColorRect;
|
||||
|
||||
private AlphaPatternDrawable mAlphaPattern;
|
||||
|
||||
|
||||
public ColorPickerPanelView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
mBorderPaint = new Paint();
|
||||
mColorPaint = new Paint();
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
final RectF rect = mColorRect;
|
||||
|
||||
if (BORDER_WIDTH_PX > 0) {
|
||||
mBorderPaint.setColor(mBorderColor);
|
||||
canvas.drawRect(mDrawingRect, mBorderPaint);
|
||||
}
|
||||
|
||||
if (mAlphaPattern != null) {
|
||||
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();
|
||||
mDrawingRect.left = getPaddingLeft();
|
||||
mDrawingRect.right = w - getPaddingRight();
|
||||
mDrawingRect.top = getPaddingTop();
|
||||
mDrawingRect.bottom = h - getPaddingBottom();
|
||||
|
||||
setUpColorRect();
|
||||
}
|
||||
|
||||
private void setUpColorRect() {
|
||||
final RectF dRect = mDrawingRect;
|
||||
|
||||
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;
|
||||
|
||||
mColorRect = new RectF(left,top, right, bottom);
|
||||
|
||||
mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity));
|
||||
|
||||
mAlphaPattern.setBounds(
|
||||
Math.round(mColorRect.left),
|
||||
Math.round(mColorRect.top),
|
||||
Math.round(mColorRect.right),
|
||||
Math.round(mColorRect.bottom)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color that should be shown by this view.
|
||||
* @param color
|
||||
*/
|
||||
public void setColor(int color) {
|
||||
mColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color currently show by this view.
|
||||
* @return
|
||||
*/
|
||||
public int getColor() {
|
||||
return mColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color of the border surrounding the panel.
|
||||
* @param color
|
||||
*/
|
||||
public void setBorderColor(int color) {
|
||||
mBorderColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color of the border surrounding the panel.
|
||||
*/
|
||||
public int getBorderColor() {
|
||||
return mBorderColor;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,118 +31,115 @@ import android.widget.ImageView;
|
|||
import android.widget.LinearLayout;
|
||||
|
||||
/**
|
||||
* A preference type that allows a user to choose a time
|
||||
* @author Sergey Margaritov
|
||||
*/
|
||||
public class ColorPickerPreference
|
||||
extends
|
||||
Preference
|
||||
implements
|
||||
Preference.OnPreferenceClickListener,
|
||||
ColorPickerDialog.OnColorChangedListener {
|
||||
extends
|
||||
Preference
|
||||
implements
|
||||
Preference.OnPreferenceClickListener,
|
||||
ColorPickerDialog.OnColorChangedListener {
|
||||
|
||||
View mView;
|
||||
ColorPickerDialog mDialog;
|
||||
int mDefaultValue = Color.BLACK;
|
||||
private int mValue = Color.BLACK;
|
||||
private float mDensity = 0;
|
||||
private boolean mAlphaSliderEnabled = false;
|
||||
private View mView;
|
||||
private ColorPickerDialog mDialog;
|
||||
private int mDefaultValue = Color.BLACK;
|
||||
private int mValue = Color.BLACK;
|
||||
private float mDensity = 0;
|
||||
private boolean mAlphaSliderEnabled = false;
|
||||
|
||||
private static final String androidns = "http://schemas.android.com/apk/res/android";
|
||||
private static final String androidns = "http://schemas.android.com/apk/res/android";
|
||||
|
||||
public ColorPickerPreference(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
}
|
||||
public ColorPickerPreference(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
}
|
||||
|
||||
public ColorPickerPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
}
|
||||
public ColorPickerPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
onColorChanged(restoreValue ? getValue() : (Integer) defaultValue);
|
||||
}
|
||||
public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
onColorChanged(restoreValue ? getValue() : (Integer) defaultValue);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
setOnPreferenceClickListener(this);
|
||||
if (attrs != null) {
|
||||
String defaultValue = attrs.getAttributeValue(androidns, "defaultValue");
|
||||
if (defaultValue.startsWith("#")) {
|
||||
try {
|
||||
mDefaultValue = convertToColorInt(defaultValue);
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e("ColorPickerPreference", "Wrong color: " + defaultValue);
|
||||
mDefaultValue = convertToColorInt("#FF000000");
|
||||
}
|
||||
} else {
|
||||
int resourceId = attrs.getAttributeResourceValue(androidns, "defaultValue", 0);
|
||||
if (resourceId != 0) {
|
||||
mDefaultValue = context.getResources().getInteger(resourceId);
|
||||
}
|
||||
}
|
||||
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
|
||||
}
|
||||
mValue = mDefaultValue;
|
||||
}
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
setOnPreferenceClickListener(this);
|
||||
if (attrs != null) {
|
||||
String defaultValue = attrs.getAttributeValue(androidns, "defaultValue");
|
||||
if (defaultValue.startsWith("#")) {
|
||||
try {
|
||||
mDefaultValue = convertToColorInt(defaultValue);
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e("ColorPickerPreference", "Wrong color: " + defaultValue);
|
||||
mDefaultValue = convertToColorInt("#FF000000");
|
||||
}
|
||||
} else {
|
||||
int resourceId = attrs.getAttributeResourceValue(androidns, "defaultValue", 0);
|
||||
if (resourceId != 0) {
|
||||
mDefaultValue = context.getResources().getInteger(resourceId);
|
||||
}
|
||||
}
|
||||
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
|
||||
}
|
||||
mValue = mDefaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBindView(View view) {
|
||||
super.onBindView(view);
|
||||
mView = view;
|
||||
setPreviewColor();
|
||||
}
|
||||
@Override
|
||||
protected void onBindView(View view) {
|
||||
super.onBindView(view);
|
||||
mView = view;
|
||||
setPreviewColor();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void setPreviewColor() {
|
||||
if (mView == null) return;
|
||||
ImageView iView = new ImageView(getContext());
|
||||
LinearLayout widgetFrameView = mView.findViewById(android.R.id.widget_frame);
|
||||
if (widgetFrameView == null) return;
|
||||
widgetFrameView.setVisibility(View.VISIBLE);
|
||||
widgetFrameView.setPadding(
|
||||
widgetFrameView.getPaddingLeft(),
|
||||
widgetFrameView.getPaddingTop(),
|
||||
(int)(mDensity * 8),
|
||||
widgetFrameView.getPaddingBottom()
|
||||
);
|
||||
// remove already create preview image
|
||||
int count = widgetFrameView.getChildCount();
|
||||
if (count > 0) {
|
||||
widgetFrameView.removeViews(0, count);
|
||||
}
|
||||
widgetFrameView.addView(iView);
|
||||
widgetFrameView.setMinimumWidth(0);
|
||||
iView.setBackgroundDrawable(new AlphaPatternDrawable((int)(5 * mDensity)));
|
||||
iView.setImageBitmap(getPreviewBitmap());
|
||||
}
|
||||
private void setPreviewColor() {
|
||||
if (mView == null) return;
|
||||
ImageView iView = new ImageView(getContext());
|
||||
LinearLayout widgetFrameView = mView.findViewById(android.R.id.widget_frame);
|
||||
if (widgetFrameView == null) return;
|
||||
widgetFrameView.setVisibility(View.VISIBLE);
|
||||
widgetFrameView.setPadding(
|
||||
widgetFrameView.getPaddingLeft(),
|
||||
widgetFrameView.getPaddingTop(),
|
||||
(int)(mDensity * 8),
|
||||
widgetFrameView.getPaddingBottom()
|
||||
);
|
||||
// remove already create preview image
|
||||
int count = widgetFrameView.getChildCount();
|
||||
if (count > 0) {
|
||||
widgetFrameView.removeViews(0, count);
|
||||
}
|
||||
widgetFrameView.addView(iView);
|
||||
widgetFrameView.setMinimumWidth(0);
|
||||
iView.setBackgroundDrawable(new AlphaPatternDrawable((int)(5 * mDensity)));
|
||||
iView.setImageBitmap(getPreviewBitmap());
|
||||
}
|
||||
|
||||
private Bitmap getPreviewBitmap() {
|
||||
int d = (int) (mDensity * 31); //30dip
|
||||
int color = getValue();
|
||||
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
|
||||
int w = bm.getWidth();
|
||||
int h = bm.getHeight();
|
||||
int c = color;
|
||||
for (int i = 0; i < w; i++) {
|
||||
for (int j = i; j < h; j++) {
|
||||
c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
|
||||
bm.setPixel(i, j, c);
|
||||
if (i != j) {
|
||||
bm.setPixel(j, i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Bitmap getPreviewBitmap() {
|
||||
int d = (int) (mDensity * 31); //30dip
|
||||
int color = getValue();
|
||||
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
|
||||
int w = bm.getWidth();
|
||||
int h = bm.getHeight();
|
||||
for (int i = 0; i < w; i++) {
|
||||
for (int j = i; j < h; j++) {
|
||||
int c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
|
||||
bm.setPixel(i, j, c);
|
||||
if (i != j) {
|
||||
bm.setPixel(j, i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bm;
|
||||
}
|
||||
return bm;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
try {
|
||||
|
@ -183,42 +180,42 @@ public class ColorPickerPreference
|
|||
try {
|
||||
Preference.OnPreferenceChangeListener listener = getOnPreferenceChangeListener();
|
||||
if (listener != null)
|
||||
listener.onPreferenceChange(this, color);
|
||||
listener.onPreferenceChange(this, color);
|
||||
} catch (NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
showDialog(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void showDialog(Bundle state) {
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
showDialog(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showDialog(Bundle state) {
|
||||
mDialog = new ColorPickerDialog(getContext(), getValue(), getTitle());
|
||||
mDialog.setOnColorChangedListener(this);
|
||||
if (mAlphaSliderEnabled) {
|
||||
mDialog.setAlphaSliderVisible(true);
|
||||
}
|
||||
if (state != null) {
|
||||
mDialog.onRestoreInstanceState(state);
|
||||
}
|
||||
mDialog.show();
|
||||
}
|
||||
mDialog.setOnColorChangedListener(this);
|
||||
if (mAlphaSliderEnabled) {
|
||||
mDialog.setAlphaSliderVisible(true);
|
||||
}
|
||||
if (state != null) {
|
||||
mDialog.onRestoreInstanceState(state);
|
||||
}
|
||||
mDialog.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle Alpha Slider visibility (by default it's disabled)
|
||||
* @param enable
|
||||
*/
|
||||
public void setAlphaSliderEnabled(boolean enable) {
|
||||
mAlphaSliderEnabled = enable;
|
||||
}
|
||||
/**
|
||||
* Toggle Alpha Slider visibility (by default it's disabled)
|
||||
* @param enable
|
||||
*/
|
||||
public void setAlphaSliderEnabled(boolean enable) {
|
||||
mAlphaSliderEnabled = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* For custom purposes. Not used by ColorPickerPreferrence
|
||||
* @param color
|
||||
* @author Unknown
|
||||
*/
|
||||
public static String convertToARGB(int color) {
|
||||
/**
|
||||
* For custom purposes. Not used by ColorPickerPreference
|
||||
* @param color
|
||||
* @author Unknown
|
||||
*/
|
||||
private static String convertToARGB(int color) {
|
||||
String alpha = Integer.toHexString(Color.alpha(color));
|
||||
String red = Integer.toHexString(Color.red(color));
|
||||
String green = Integer.toHexString(Color.green(color));
|
||||
|
@ -249,11 +246,10 @@ public class ColorPickerPreference
|
|||
* @throws NumberFormatException
|
||||
* @author Unknown
|
||||
*/
|
||||
public static int convertToColorInt(String argb) throws NumberFormatException {
|
||||
|
||||
if (argb.startsWith("#")) {
|
||||
argb = argb.replace("#", "");
|
||||
}
|
||||
private static int convertToColorInt(String argb) throws NumberFormatException {
|
||||
if (argb.startsWith("#")) {
|
||||
argb = argb.replace("#", "");
|
||||
}
|
||||
|
||||
int alpha = 0, red = 0, green = 0, blue = 0;
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user