Browse Source

Update ColorPicker to 696bb05

pull/554/head
TacoTheDank 4 years ago
parent
commit
660478aafc
  1. 1
      app/build.gradle
  2. 3
      app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java
  3. 141
      app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java
  4. 14
      app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java
  5. 127
      app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java
  6. 77
      app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java
  7. 61
      app/src/main/res/layout-land/dialog_color_picker.xml
  8. 67
      app/src/main/res/layout/dialog_color_picker.xml

1
app/build.gradle

@ -78,6 +78,7 @@ android {
dependencies { dependencies {
// Android support // Android support
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
implementation 'com.google.android.material:material:1.1.0' implementation 'com.google.android.material:material:1.1.0'

3
app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker; package net.margaritov.preference.colorpicker;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -28,6 +30,7 @@ import android.graphics.drawable.Drawable;
* This drawable that draws a simple white and gray chessboard pattern. * This drawable that draws a simple white and gray chessboard pattern.
* It's pattern you will often see as a background behind a * It's pattern you will often see as a background behind a
* partly transparent image in many applications. * partly transparent image in many applications.
*
* @author Daniel Nilsson * @author Daniel Nilsson
*/ */
public class AlphaPatternDrawable extends Drawable { public class AlphaPatternDrawable extends Drawable {

141
app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java

@ -14,34 +14,75 @@
* limitations under the License. * limitations under the License.
*/ */
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker; package net.margaritov.preference.colorpicker;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatDialog;
import org.transdroid.R; import org.transdroid.R;
public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColorChangedListener, View.OnClickListener { import java.util.Locale;
public class ColorPickerDialog
extends
AppCompatDialog
implements
ColorPickerView.OnColorChangedListener,
View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {
private ColorPickerView mColorPicker; private ColorPickerView mColorPicker;
private ColorPickerPanelView mOldColor; private ColorPickerPanelView mOldColor;
private ColorPickerPanelView mNewColor; private ColorPickerPanelView mNewColor;
private EditText mHexVal;
private boolean mHexValueEnabled = false;
private ColorStateList mHexDefaultTextColor;
private OnColorChangedListener mListener; private OnColorChangedListener mListener;
private int mOrientation;
private View mLayout;
private String mTitle;
@Override
public void onGlobalLayout() {
if (getContext().getResources().getConfiguration().orientation != mOrientation) {
final int oldcolor = mOldColor.getColor();
final int newcolor = mNewColor.getColor();
mLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
setUp(oldcolor);
mNewColor.setColor(newcolor);
mColorPicker.setColor(newcolor);
}
}
public interface OnColorChangedListener { public interface OnColorChangedListener {
public void onColorChanged(int color); public void onColorChanged(int color);
} }
public ColorPickerDialog(Context context, int initialColor) { public ColorPickerDialog(Context context, int initialColor, String title) {
super(context); super(context);
mTitle = title;
init(initialColor); init(initialColor);
} }
@ -57,19 +98,53 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_color_picker, null); mLayout = inflater.inflate(R.layout.dialog_color_picker, null);
mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
mOrientation = getContext().getResources().getConfiguration().orientation;
setContentView(mLayout);
setContentView(layout); setTitle(mTitle);
setTitle(R.string.dialog_color_picker); mColorPicker = (ColorPickerView) mLayout.findViewById(R.id.color_picker_view);
mOldColor = (ColorPickerPanelView) mLayout.findViewById(R.id.old_color_panel);
mNewColor = (ColorPickerPanelView) mLayout.findViewById(R.id.new_color_panel);
mColorPicker = (ColorPickerView) layout.findViewById(R.id.color_picker_view); mHexVal = (EditText) mLayout.findViewById(R.id.hex_val);
mOldColor = (ColorPickerPanelView) layout.findViewById(R.id.old_color_panel); mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mNewColor = (ColorPickerPanelView) layout.findViewById(R.id.new_color_panel); mHexDefaultTextColor = mHexVal.getTextColors();
((LinearLayout) mOldColor.getParent()) mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() {
.setPadding(Math.round(mColorPicker.getDrawingOffset()), 0, Math.round(mColorPicker.getDrawingOffset()),
0); @Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
String s = mHexVal.getText().toString();
if (s.length() > 5 || s.length() < 10) {
try {
int c = ColorPickerPreference.convertToColorInt(s.toString());
mColorPicker.setColor(c, true);
mHexVal.setTextColor(mHexDefaultTextColor);
} catch (IllegalArgumentException e) {
mHexVal.setTextColor(Color.RED);
}
} else {
mHexVal.setTextColor(Color.RED);
}
return true;
}
return false;
}
});
((LinearLayout) mOldColor.getParent()).setPadding(
Math.round(mColorPicker.getDrawingOffset()),
0,
Math.round(mColorPicker.getDrawingOffset()),
0
);
mOldColor.setOnClickListener(this); mOldColor.setOnClickListener(this);
mNewColor.setOnClickListener(this); mNewColor.setOnClickListener(this);
@ -84,6 +159,9 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor
mNewColor.setColor(color); mNewColor.setColor(color);
if (mHexValueEnabled)
updateHexValue(color);
/* /*
if (mListener != null) { if (mListener != null) {
mListener.onColorChanged(color); mListener.onColorChanged(color);
@ -92,13 +170,52 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor
} }
public void setHexValueEnabled(boolean enable) {
mHexValueEnabled = enable;
if (enable) {
mHexVal.setVisibility(View.VISIBLE);
updateHexLengthFilter();
updateHexValue(getColor());
} else
mHexVal.setVisibility(View.GONE);
}
public boolean getHexValueEnabled() {
return mHexValueEnabled;
}
private void updateHexLengthFilter() {
if (getAlphaSliderVisible())
mHexVal.setFilters(new InputFilter[]{new InputFilter.LengthFilter(9)});
else
mHexVal.setFilters(new InputFilter[]{new InputFilter.LengthFilter(7)});
}
private void updateHexValue(int color) {
if (getAlphaSliderVisible()) {
mHexVal.setText(ColorPickerPreference.convertToARGB(color).toUpperCase(Locale.getDefault()));
} else {
mHexVal.setText(ColorPickerPreference.convertToRGB(color).toUpperCase(Locale.getDefault()));
}
mHexVal.setTextColor(mHexDefaultTextColor);
}
public void setAlphaSliderVisible(boolean visible) { public void setAlphaSliderVisible(boolean visible) {
mColorPicker.setAlphaSliderVisible(visible); mColorPicker.setAlphaSliderVisible(visible);
if (mHexValueEnabled) {
updateHexLengthFilter();
updateHexValue(getColor());
}
}
public boolean getAlphaSliderVisible() {
return mColorPicker.getAlphaSliderVisible();
} }
/** /**
* Set a OnColorChangedListener to get notified when the color * Set a OnColorChangedListener to get notified when the color
* selected by the user has changed. * selected by the user has changed.
*
* @param listener * @param listener
*/ */
public void setOnColorChangedListener(OnColorChangedListener listener) { public void setOnColorChangedListener(OnColorChangedListener listener) {

14
app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker; package net.margaritov.preference.colorpicker;
import android.content.Context; import android.content.Context;
@ -27,6 +29,7 @@ import android.view.View;
* This class draws a panel which which will be filled with a color which can be set. * 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 * It can be used to show the currently selected color which you will get from
* the {@link ColorPickerView}. * the {@link ColorPickerView}.
*
* @author Daniel Nilsson * @author Daniel Nilsson
*/ */
public class ColorPickerPanelView extends View { public class ColorPickerPanelView extends View {
@ -125,13 +128,18 @@ public class ColorPickerPanelView extends View {
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity)); mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity));
mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top), Math.round(mColorRect.right), mAlphaPattern.setBounds(
Math.round(mColorRect.bottom)); 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. * Set the color that should be shown by this view.
*
* @param color * @param color
*/ */
public void setColor(int color) { public void setColor(int color) {
@ -141,6 +149,7 @@ public class ColorPickerPanelView extends View {
/** /**
* Get the color currently show by this view. * Get the color currently show by this view.
*
* @return * @return
*/ */
public int getColor() { public int getColor() {
@ -149,6 +158,7 @@ public class ColorPickerPanelView extends View {
/** /**
* Set the color of the border surrounding the panel. * Set the color of the border surrounding the panel.
*
* @param color * @param color
*/ */
public void setBorderColor(int color) { public void setBorderColor(int color) {

127
app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker; package net.margaritov.preference.colorpicker;
import android.content.Context; import android.content.Context;
@ -24,24 +26,32 @@ import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
/** /**
* A preference type that allows a user to choose a time * A preference type that allows a user to choose a color
*
* @author Sergey Margaritov * @author Sergey Margaritov
*/ */
public class ColorPickerPreference extends Preference public class ColorPickerPreference
implements Preference.OnPreferenceClickListener, ColorPickerDialog.OnColorChangedListener { extends
Preference
implements
Preference.OnPreferenceClickListener,
ColorPickerDialog.OnColorChangedListener {
View mView; View mView;
ColorPickerDialog mDialog; ColorPickerDialog mDialog;
private int mValue = Color.BLACK; private int mValue = Color.BLACK;
private float mDensity = 0; private float mDensity = 0;
private boolean mAlphaSliderEnabled = false; private boolean mAlphaSliderEnabled = false;
private boolean mHexValueEnabled = false;
public ColorPickerPreference(Context context) { public ColorPickerPreference(Context context) {
super(context); super(context);
@ -58,10 +68,23 @@ public class ColorPickerPreference extends Preference
init(context, attrs); init(context, attrs);
} }
/**Method edited by
* @author Anna Berkovitch
* added functionality to accept hex string as defaultValue
* and to properly persist resources reference string, such as @color/someColor
* previously persisted 0*/
@Override @Override
protected Object onGetDefaultValue(TypedArray a, int index) { protected Object onGetDefaultValue(TypedArray a, int index) {
int colorInt;
String mHexDefaultValue = a.getString(index);
if (mHexDefaultValue != null && mHexDefaultValue.startsWith("#")) {
colorInt = convertToColorInt(mHexDefaultValue);
return colorInt;
} else {
return a.getColor(index, Color.BLACK); return a.getColor(index, Color.BLACK);
} }
}
@Override @Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
@ -73,29 +96,30 @@ public class ColorPickerPreference extends Preference
setOnPreferenceClickListener(this); setOnPreferenceClickListener(this);
if (attrs != null) { if (attrs != null) {
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false); mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
mHexValueEnabled = attrs.getAttributeBooleanValue(null, "hexValue", false);
} }
} }
@Override @Override
protected void onBindView(View view) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindView(view); super.onBindViewHolder(holder);
mView = view;
mView = holder.itemView;
setPreviewColor(); setPreviewColor();
} }
private void setPreviewColor() { private void setPreviewColor() {
if (mView == null) { if (mView == null) return;
return;
}
ImageView iView = new ImageView(getContext()); ImageView iView = new ImageView(getContext());
LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame)); LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame));
if (widgetFrameView == null) { if (widgetFrameView == null) return;
return;
}
widgetFrameView.setVisibility(View.VISIBLE); widgetFrameView.setVisibility(View.VISIBLE);
widgetFrameView widgetFrameView.setPadding(
.setPadding(widgetFrameView.getPaddingLeft(), widgetFrameView.getPaddingTop(), (int) (mDensity * 8), widgetFrameView.getPaddingLeft(),
widgetFrameView.getPaddingBottom()); widgetFrameView.getPaddingTop(),
(int) (mDensity * 8),
widgetFrameView.getPaddingBottom()
);
// remove already create preview image // remove already create preview image
int count = widgetFrameView.getChildCount(); int count = widgetFrameView.getChildCount();
if (count > 0) { if (count > 0) {
@ -147,11 +171,14 @@ public class ColorPickerPreference extends Preference
} }
protected void showDialog(Bundle state) { protected void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), mValue); mDialog = new ColorPickerDialog(getContext(), mValue, getTitle().toString());
mDialog.setOnColorChangedListener(this); mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) { if (mAlphaSliderEnabled) {
mDialog.setAlphaSliderVisible(true); mDialog.setAlphaSliderVisible(true);
} }
if (mHexValueEnabled) {
mDialog.setHexValueEnabled(true);
}
if (state != null) { if (state != null) {
mDialog.onRestoreInstanceState(state); mDialog.onRestoreInstanceState(state);
} }
@ -160,14 +187,25 @@ public class ColorPickerPreference extends Preference
/** /**
* Toggle Alpha Slider visibility (by default it's disabled) * Toggle Alpha Slider visibility (by default it's disabled)
*
* @param enable * @param enable
*/ */
public void setAlphaSliderEnabled(boolean enable) { public void setAlphaSliderEnabled(boolean enable) {
mAlphaSliderEnabled = enable; mAlphaSliderEnabled = enable;
} }
/**
* Toggle Hex Value visibility (by default it's disabled)
*
* @param enable
*/
public void setHexValueEnabled(boolean enable) {
mHexValueEnabled = enable;
}
/** /**
* For custom purposes. Not used by ColorPickerPreferrence * For custom purposes. Not used by ColorPickerPreferrence
*
* @param color * @param color
* @author Unknown * @author Unknown
*/ */
@ -196,33 +234,49 @@ public class ColorPickerPreference extends Preference
return "#" + alpha + red + green + blue; return "#" + alpha + red + green + blue;
} }
/**
* Method currently used by onGetDefaultValue method to
* convert hex string provided in android:defaultValue to color integer.
*
* @param color
* @return A string representing the hex value of color,
* without the alpha value
* @author Charles Rosaaen
*/
public static String convertToRGB(int color) {
String red = Integer.toHexString(Color.red(color));
String green = Integer.toHexString(Color.green(color));
String blue = Integer.toHexString(Color.blue(color));
if (red.length() == 1) {
red = "0" + red;
}
if (green.length() == 1) {
green = "0" + green;
}
if (blue.length() == 1) {
blue = "0" + blue;
}
return "#" + red + green + blue;
}
/** /**
* For custom purposes. Not used by ColorPickerPreferrence * For custom purposes. Not used by ColorPickerPreferrence
*
* @param argb * @param argb
* @throws NumberFormatException * @throws NumberFormatException
* @author Unknown * @author Unknown
*/ */
public static int convertToColorInt(String argb) throws NumberFormatException { public static int convertToColorInt(String argb) throws IllegalArgumentException {
if (argb.startsWith("#")) {
argb = argb.replace("#", "");
}
int alpha = -1, red = -1, green = -1, blue = -1;
if (argb.length() == 8) { if (!argb.startsWith("#")) {
alpha = Integer.parseInt(argb.substring(0, 2), 16); argb = "#" + argb;
red = Integer.parseInt(argb.substring(2, 4), 16);
green = Integer.parseInt(argb.substring(4, 6), 16);
blue = Integer.parseInt(argb.substring(6, 8), 16);
} else if (argb.length() == 6) {
alpha = 255;
red = Integer.parseInt(argb.substring(0, 2), 16);
green = Integer.parseInt(argb.substring(2, 4), 16);
blue = Integer.parseInt(argb.substring(4, 6), 16);
} }
return Color.argb(alpha, red, green, blue); return Color.parseColor(argb);
} }
@Override @Override
@ -269,7 +323,8 @@ public class ColorPickerPreference extends Preference
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) { public SavedState createFromParcel(Parcel in) {
return new SavedState(in); return new SavedState(in);
} }

77
app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker; package net.margaritov.preference.colorpicker;
import android.content.Context; import android.content.Context;
@ -38,6 +40,7 @@ import android.view.View;
* to select a color. A slider for the alpha channel is * to select a color. A slider for the alpha channel is
* also available. Enable it by setting * also available. Enable it by setting
* setAlphaSliderVisible(boolean) to true. * setAlphaSliderVisible(boolean) to true.
*
* @author Daniel Nilsson * @author Daniel Nilsson
*/ */
public class ColorPickerView extends View { public class ColorPickerView extends View {
@ -220,9 +223,7 @@ public class ColorPickerView extends View {
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) { if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) return;
return;
}
drawSatValPanel(canvas); drawSatValPanel(canvas);
drawHuePanel(canvas); drawHuePanel(canvas);
@ -236,18 +237,18 @@ public class ColorPickerView extends View {
if (BORDER_WIDTH_PX > 0) { if (BORDER_WIDTH_PX > 0) {
mBorderPaint.setColor(mBorderColor); mBorderPaint.setColor(mBorderColor);
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
} }
if (mValShader == null) { if (mValShader == null) {
mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000, mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom,
TileMode.CLAMP); 0xffffffff, 0xff000000, TileMode.CLAMP);
} }
int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f}); int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f});
mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP); mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top,
0xffffffff, rgb, TileMode.CLAMP);
ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY); ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY);
mSatValPaint.setShader(mShader); mSatValPaint.setShader(mShader);
@ -269,13 +270,15 @@ public class ColorPickerView extends View {
if (BORDER_WIDTH_PX > 0) { if (BORDER_WIDTH_PX > 0) {
mBorderPaint.setColor(mBorderColor); mBorderPaint.setColor(mBorderColor);
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, canvas.drawRect(rect.left - BORDER_WIDTH_PX,
rect.bottom + BORDER_WIDTH_PX, mBorderPaint); rect.top - BORDER_WIDTH_PX,
rect.right + BORDER_WIDTH_PX,
rect.bottom + BORDER_WIDTH_PX,
mBorderPaint);
} }
if (mHueShader == null) { if (mHueShader == null) {
mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, TileMode.CLAMP);
TileMode.CLAMP);
mHuePaint.setShader(mHueShader); mHuePaint.setShader(mHueShader);
} }
@ -298,16 +301,17 @@ public class ColorPickerView extends View {
private void drawAlphaPanel(Canvas canvas) { private void drawAlphaPanel(Canvas canvas) {
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) { if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) return;
return;
}
final RectF rect = mAlphaRect; final RectF rect = mAlphaRect;
if (BORDER_WIDTH_PX > 0) { if (BORDER_WIDTH_PX > 0) {
mBorderPaint.setColor(mBorderColor); mBorderPaint.setColor(mBorderColor);
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, canvas.drawRect(rect.left - BORDER_WIDTH_PX,
rect.bottom + BORDER_WIDTH_PX, mBorderPaint); rect.top - BORDER_WIDTH_PX,
rect.right + BORDER_WIDTH_PX,
rect.bottom + BORDER_WIDTH_PX,
mBorderPaint);
} }
@ -317,7 +321,8 @@ public class ColorPickerView extends View {
int color = Color.HSVToColor(hsv); int color = Color.HSVToColor(hsv);
int acolor = Color.HSVToColor(0, hsv); int acolor = Color.HSVToColor(0, hsv);
mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP); mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top,
color, acolor, TileMode.CLAMP);
mAlphaPaint.setShader(mAlphaShader); mAlphaPaint.setShader(mAlphaShader);
@ -593,9 +598,7 @@ public class ColorPickerView extends View {
private boolean moveTrackersIfNeeded(MotionEvent event) { private boolean moveTrackersIfNeeded(MotionEvent event) {
if (mStartTouchPoint == null) { if (mStartTouchPoint == null) return false;
return false;
}
boolean update = false; boolean update = false;
@ -760,9 +763,7 @@ public class ColorPickerView extends View {
private void setUpAlphaRect() { private void setUpAlphaRect() {
if (!mShowAlphaPanel) { if (!mShowAlphaPanel) return;
return;
}
final RectF dRect = mDrawingRect; final RectF dRect = mDrawingRect;
@ -774,8 +775,12 @@ public class ColorPickerView extends View {
mAlphaRect = new RectF(left, top, right, bottom); mAlphaRect = new RectF(left, top, right, bottom);
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity)); mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity));
mAlphaPattern.setBounds(Math.round(mAlphaRect.left), Math.round(mAlphaRect.top), Math.round(mAlphaRect.right), mAlphaPattern.setBounds(
Math.round(mAlphaRect.bottom)); Math.round(mAlphaRect.left),
Math.round(mAlphaRect.top),
Math.round(mAlphaRect.right),
Math.round(mAlphaRect.bottom)
);
} }
@ -783,6 +788,7 @@ public class ColorPickerView extends View {
/** /**
* Set a OnColorChangedListener to get notified when the color * Set a OnColorChangedListener to get notified when the color
* selected by the user has changed. * selected by the user has changed.
*
* @param listener * @param listener
*/ */
public void setOnColorChangedListener(OnColorChangedListener listener) { public void setOnColorChangedListener(OnColorChangedListener listener) {
@ -791,6 +797,7 @@ public class ColorPickerView extends View {
/** /**
* Set the color of the border surrounding all panels. * Set the color of the border surrounding all panels.
*
* @param color * @param color
*/ */
public void setBorderColor(int color) { public void setBorderColor(int color) {
@ -807,6 +814,7 @@ public class ColorPickerView extends View {
/** /**
* Get the current color this view is showing. * Get the current color this view is showing.
*
* @return the current color. * @return the current color.
*/ */
public int getColor() { public int getColor() {
@ -815,6 +823,7 @@ public class ColorPickerView extends View {
/** /**
* Set the color the view should show. * Set the color the view should show.
*
* @param color The color that should be selected. * @param color The color that should be selected.
*/ */
public void setColor(int color) { public void setColor(int color) {
@ -823,6 +832,7 @@ public class ColorPickerView extends View {
/** /**
* Set the color this view should show. * Set the color this view should show.
*
* @param color The color that should be selected. * @param color The color that should be selected.
* @param callback If you want to get a callback to * @param callback If you want to get a callback to
* your OnColorChangedListener. * your OnColorChangedListener.
@ -830,13 +840,10 @@ public class ColorPickerView extends View {
public void setColor(int color, boolean callback) { public void setColor(int color, boolean callback) {
int alpha = Color.alpha(color); int alpha = Color.alpha(color);
int red = Color.red(color);
int blue = Color.blue(color);
int green = Color.green(color);
float[] hsv = new float[3]; float[] hsv = new float[3];
Color.RGBToHSV(red, green, blue, hsv); Color.colorToHSV(color, hsv);
mAlpha = alpha; mAlpha = alpha;
mHue = hsv[0]; mHue = hsv[0];
@ -856,6 +863,7 @@ public class ColorPickerView extends View {
* a panel to the side of the view minus the padding. * a panel to the side of the view minus the padding.
* Useful if you want to have your own panel below showing * Useful if you want to have your own panel below showing
* the currently selected color and want to align it perfectly. * the currently selected color and want to align it perfectly.
*
* @return The offset in pixels. * @return The offset in pixels.
*/ */
public float getDrawingOffset() { public float getDrawingOffset() {
@ -865,6 +873,7 @@ public class ColorPickerView extends View {
/** /**
* Set if the user is allowed to adjust the alpha panel. Default is false. * Set if the user is allowed to adjust the alpha panel. Default is false.
* If it is set to false no alpha will be set. * If it is set to false no alpha will be set.
*
* @param visible * @param visible
*/ */
public void setAlphaSliderVisible(boolean visible) { public void setAlphaSliderVisible(boolean visible) {
@ -881,13 +890,16 @@ public class ColorPickerView extends View {
mSatShader = null; mSatShader = null;
mHueShader = null; mHueShader = null;
mAlphaShader = null; mAlphaShader = null;
;
requestLayout(); requestLayout();
} }
} }
public boolean getAlphaSliderVisible() {
return mShowAlphaPanel;
}
public void setSliderTrackerColor(int color) { public void setSliderTrackerColor(int color) {
mSliderTrackerColor = color; mSliderTrackerColor = color;
@ -903,6 +915,7 @@ public class ColorPickerView extends View {
/** /**
* Set the text that should be shown in the * Set the text that should be shown in the
* alpha slider. Set to null to disable text. * alpha slider. Set to null to disable text.
*
* @param res string resource id. * @param res string resource id.
*/ */
public void setAlphaSliderText(int res) { public void setAlphaSliderText(int res) {
@ -913,6 +926,7 @@ public class ColorPickerView extends View {
/** /**
* Set the text that should be shown in the * Set the text that should be shown in the
* alpha slider. Set to null to disable text. * alpha slider. Set to null to disable text.
*
* @param text Text that should be shown. * @param text Text that should be shown.
*/ */
public void setAlphaSliderText(String text) { public void setAlphaSliderText(String text) {
@ -924,6 +938,7 @@ public class ColorPickerView extends View {
* Get the current value of the text * Get the current value of the text
* that will be shown in the alpha * that will be shown in the alpha
* slider. * slider.
*
* @return * @return
*/ */
public String getAlphaSliderText() { public String getAlphaSliderText() {

61
app/src/main/res/layout-land/dialog_color_picker.xml

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!--
<!-- Copyright (C) 2010 Daniel Nilsson Copyright (C) 2010 Daniel Nilsson
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -10,68 +10,73 @@
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and limitations under the License.
limitations under the License.
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
--> -->
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp">
android:orientation="horizontal">
<net.margaritov.preference.colorpicker.ColorPickerView <net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view" android:id="@+id/color_picker_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:tag="landscape"
android:layerType="software" android:layerType="software"
/> android:tag="landscape" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginBottom="10dp"
android:layout_marginBottom="10dp"> android:orientation="vertical">
<EditText
android:id="@+id/hex_val"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="HEX"
android:imeOptions="actionDone"
android:maxLength="7"
android:singleLine="true"
android:inputType="textCapCharacters"
android:visibility="gone"></EditText>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/press_color_to_apply" android:layout_marginBottom="5dp"
android:gravity="center"
android:layout_marginTop="6dp"
android:layout_marginLeft="6dp" android:layout_marginLeft="6dp"
android:layout_marginRight="6dp" android:layout_marginRight="6dp"
android:layout_marginBottom="5dp" android:layout_marginTop="6dp"
android:textAppearance="?android:attr/textAppearanceSmall" android:gravity="center"
/> android:text="@string/press_color_to_apply"
android:textAppearance="?android:attr/textAppearanceSmall" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView <net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/old_color_panel" android:id="@+id/old_color_panel"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:layout_weight="0.5" android:layout_weight="0.5" />
/>
<TextView <TextView
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="↓"
android:textSize="20sp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"
/> android:layout_marginTop="10dp"
android:gravity="center"
android:text="↓"
android:textSize="20sp" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView <net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/new_color_panel" android:id="@+id/new_color_panel"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:layout_weight="0.5" android:layout_weight="0.5" />
/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

67
app/src/main/res/layout/dialog_color_picker.xml

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!--
<!-- Copyright (C) 2010 Daniel Nilsson Copyright (C) 2010 Daniel Nilsson
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -10,68 +10,79 @@
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and limitations under the License.
limitations under the License.
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
--> -->
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp">
android:orientation="vertical">
<net.margaritov.preference.colorpicker.ColorPickerView <net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view" android:id="@+id/color_picker_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:tag="portrait"
android:layerType="software" android:layerType="software"
/> android:tag="portrait" />
<LinearLayout
android:id="@+id/text_hex_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/press_color_to_apply"
android:gravity="left" android:gravity="left"
android:layout_marginLeft="6dp" android:text="@string/press_color_to_apply"
android:layout_marginRight="6dp" android:textAppearance="?android:attr/textAppearanceSmall" />
android:layout_marginBottom="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" <EditText
/> android:id="@+id/hex_val"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="HEX"
android:imeOptions="actionDone"
android:maxLength="7"
android:singleLine="true"
android:inputType="textCapCharacters"
android:visibility="gone"></EditText>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="40dp" android:layout_height="40dp"
android:orientation="horizontal" android:layout_marginBottom="10dp"
android:layout_marginBottom="10dp"> android:orientation="horizontal">
<net.margaritov.preference.colorpicker.ColorPickerPanelView <net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/old_color_panel" android:id="@+id/old_color_panel"
android:layout_width="0px" android:layout_width="0px"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:layout_weight="0.5" android:layout_weight="0.5" />
/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:text="→"
android:textSize="20sp"
android:gravity="center"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
/> android:gravity="center"
android:text="→"
android:textSize="20sp" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView <net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/new_color_panel" android:id="@+id/new_color_panel"
android:layout_width="0px" android:layout_width="0px"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0.5" android:layout_weight="0.5" />
/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
Loading…
Cancel
Save