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 { @@ -78,6 +78,7 @@ android {
dependencies {
// Android support
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.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 @@ @@ -14,6 +14,8 @@
* limitations under the License.
*/
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker;
import android.graphics.Bitmap;
@ -28,6 +30,7 @@ import android.graphics.drawable.Drawable; @@ -28,6 +30,7 @@ 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 {

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

@ -14,34 +14,75 @@ @@ -14,34 +14,75 @@
* limitations under the License.
*/
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker;
import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
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.TextView;
import androidx.appcompat.app.AppCompatDialog;
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 ColorPickerPanelView mOldColor;
private ColorPickerPanelView mNewColor;
private EditText mHexVal;
private boolean mHexValueEnabled = false;
private ColorStateList mHexDefaultTextColor;
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 void onColorChanged(int color);
}
public ColorPickerDialog(Context context, int initialColor) {
public ColorPickerDialog(Context context, int initialColor, String title) {
super(context);
mTitle = title;
init(initialColor);
}
@ -57,19 +98,53 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor @@ -57,19 +98,53 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor
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);
mOldColor = (ColorPickerPanelView) layout.findViewById(R.id.old_color_panel);
mNewColor = (ColorPickerPanelView) layout.findViewById(R.id.new_color_panel);
mHexVal = (EditText) mLayout.findViewById(R.id.hex_val);
mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mHexDefaultTextColor = mHexVal.getTextColors();
((LinearLayout) mOldColor.getParent())
.setPadding(Math.round(mColorPicker.getDrawingOffset()), 0, Math.round(mColorPicker.getDrawingOffset()),
0);
mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@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);
mNewColor.setOnClickListener(this);
@ -84,6 +159,9 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor @@ -84,6 +159,9 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor
mNewColor.setColor(color);
if (mHexValueEnabled)
updateHexValue(color);
/*
if (mListener != null) {
mListener.onColorChanged(color);
@ -92,13 +170,52 @@ public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColor @@ -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) {
mColorPicker.setAlphaSliderVisible(visible);
if (mHexValueEnabled) {
updateHexLengthFilter();
updateHexValue(getColor());
}
}
public boolean getAlphaSliderVisible() {
return mColorPicker.getAlphaSliderVisible();
}
/**
* Set a OnColorChangedListener to get notified when the color
* selected by the user has changed.
*
* @param listener
*/
public void setOnColorChangedListener(OnColorChangedListener listener) {

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

@ -14,6 +14,8 @@ @@ -14,6 +14,8 @@
* limitations under the License.
*/
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker;
import android.content.Context;
@ -27,6 +29,7 @@ import android.view.View; @@ -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.
* 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 {
@ -125,13 +128,18 @@ public class ColorPickerPanelView extends View { @@ -125,13 +128,18 @@ public class ColorPickerPanelView extends View {
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity));
mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top), Math.round(mColorRect.right),
Math.round(mColorRect.bottom));
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) {
@ -141,6 +149,7 @@ public class ColorPickerPanelView extends View { @@ -141,6 +149,7 @@ public class ColorPickerPanelView extends View {
/**
* Get the color currently show by this view.
*
* @return
*/
public int getColor() {
@ -149,6 +158,7 @@ public class ColorPickerPanelView extends View { @@ -149,6 +158,7 @@ public class ColorPickerPanelView extends View {
/**
* Set the color of the border surrounding the panel.
*
* @param color
*/
public void setBorderColor(int color) {

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

@ -14,6 +14,8 @@ @@ -14,6 +14,8 @@
* limitations under the License.
*/
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
package net.margaritov.preference.colorpicker;
import android.content.Context;
@ -24,24 +26,32 @@ import android.graphics.Color; @@ -24,24 +26,32 @@ import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
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
*/
public class ColorPickerPreference extends Preference
implements Preference.OnPreferenceClickListener, ColorPickerDialog.OnColorChangedListener {
public class ColorPickerPreference
extends
Preference
implements
Preference.OnPreferenceClickListener,
ColorPickerDialog.OnColorChangedListener {
View mView;
ColorPickerDialog mDialog;
private int mValue = Color.BLACK;
private float mDensity = 0;
private boolean mAlphaSliderEnabled = false;
private boolean mHexValueEnabled = false;
public ColorPickerPreference(Context context) {
super(context);
@ -58,10 +68,23 @@ public class ColorPickerPreference extends Preference @@ -58,10 +68,23 @@ public class ColorPickerPreference extends Preference
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
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);
}
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
@ -73,29 +96,30 @@ public class ColorPickerPreference extends Preference @@ -73,29 +96,30 @@ public class ColorPickerPreference extends Preference
setOnPreferenceClickListener(this);
if (attrs != null) {
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
mHexValueEnabled = attrs.getAttributeBooleanValue(null, "hexValue", false);
}
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mView = view;
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
mView = holder.itemView;
setPreviewColor();
}
private void setPreviewColor() {
if (mView == null) {
return;
}
if (mView == null) return;
ImageView iView = new ImageView(getContext());
LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame));
if (widgetFrameView == null) {
return;
}
if (widgetFrameView == null) return;
widgetFrameView.setVisibility(View.VISIBLE);
widgetFrameView
.setPadding(widgetFrameView.getPaddingLeft(), widgetFrameView.getPaddingTop(), (int) (mDensity * 8),
widgetFrameView.getPaddingBottom());
widgetFrameView.setPadding(
widgetFrameView.getPaddingLeft(),
widgetFrameView.getPaddingTop(),
(int) (mDensity * 8),
widgetFrameView.getPaddingBottom()
);
// remove already create preview image
int count = widgetFrameView.getChildCount();
if (count > 0) {
@ -147,11 +171,14 @@ public class ColorPickerPreference extends Preference @@ -147,11 +171,14 @@ public class ColorPickerPreference extends Preference
}
protected void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), mValue);
mDialog = new ColorPickerDialog(getContext(), mValue, getTitle().toString());
mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) {
mDialog.setAlphaSliderVisible(true);
}
if (mHexValueEnabled) {
mDialog.setHexValueEnabled(true);
}
if (state != null) {
mDialog.onRestoreInstanceState(state);
}
@ -160,14 +187,25 @@ public class ColorPickerPreference extends Preference @@ -160,14 +187,25 @@ public class ColorPickerPreference extends Preference
/**
* Toggle Alpha Slider visibility (by default it's disabled)
*
* @param enable
*/
public void setAlphaSliderEnabled(boolean 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
*
* @param color
* @author Unknown
*/
@ -196,33 +234,49 @@ public class ColorPickerPreference extends Preference @@ -196,33 +234,49 @@ public class ColorPickerPreference extends Preference
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
*
* @param argb
* @throws NumberFormatException
* @author Unknown
*/
public static int convertToColorInt(String argb) throws NumberFormatException {
if (argb.startsWith("#")) {
argb = argb.replace("#", "");
}
int alpha = -1, red = -1, green = -1, blue = -1;
public static int convertToColorInt(String argb) throws IllegalArgumentException {
if (argb.length() == 8) {
alpha = Integer.parseInt(argb.substring(0, 2), 16);
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);
if (!argb.startsWith("#")) {
argb = "#" + argb;
}
return Color.argb(alpha, red, green, blue);
return Color.parseColor(argb);
}
@Override
@ -269,7 +323,8 @@ public class ColorPickerPreference extends Preference @@ -269,7 +323,8 @@ public class ColorPickerPreference extends Preference
}
@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) {
return new SavedState(in);
}

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

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

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 Daniel Nilsson
<?xml version="1.0" encoding="utf-8"?><!--
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.
@ -10,68 +10,73 @@ @@ -10,68 +10,73 @@
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.
See the License for the specific language governing permissions and limitations under the License.
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="horizontal">
android:paddingRight="5dp">
<net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="landscape"
android:layerType="software"
/>
android:tag="landscape" />
<LinearLayout
android:layout_width="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
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press_color_to_apply"
android:gravity="center"
android:layout_marginTop="6dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
android:layout_marginTop="6dp"
android:gravity="center"
android:text="@string/press_color_to_apply"
android:textAppearance="?android:attr/textAppearanceSmall" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/old_color_panel"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_weight="0.5"
/>
android:layout_weight="0.5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="↓"
android:textSize="20sp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
android:layout_marginTop="10dp"
android:gravity="center"
android:text="↓"
android:textSize="20sp" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/new_color_panel"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_weight="0.5"
/>
android:layout_weight="0.5" />
</LinearLayout>
</LinearLayout>

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 Daniel Nilsson
<?xml version="1.0" encoding="utf-8"?><!--
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.
@ -10,68 +10,79 @@ @@ -10,68 +10,79 @@
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.
See the License for the specific language governing permissions and limitations under the License.
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical">
android:paddingRight="5dp">
<net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:tag="portrait"
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
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press_color_to_apply"
android:gravity="left"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
android:text="@string/press_color_to_apply"
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
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/old_color_panel"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="0.5"
/>
android:layout_weight="0.5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="→"
android:textSize="20sp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
android:gravity="center"
android:text="→"
android:textSize="20sp" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/new_color_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.5"
/>
android:layout_weight="0.5" />
</LinearLayout>
</LinearLayout>
Loading…
Cancel
Save