Adding visible pressed states to color swatches.
Bug: 8254226
Change-Id: I657996e99c1b550011c26498348113a35897727a
diff --git a/res/layout/color_picker_swatch.xml b/res/layout/color_picker_swatch.xml
new file mode 100644
index 0000000..1495820
--- /dev/null
+++ b/res/layout/color_picker_swatch.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ 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.
+-->
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+ <ImageView
+ android:id="@+id/color_picker_swatch"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" />
+ <ImageView
+ android:id="@+id/color_picker_checkmark"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:src="@drawable/ic_colorpicker_swatch_selected"
+ android:scaleType="fitXY"
+ android:visibility="gone" />
+</merge>
\ No newline at end of file
diff --git a/src/com/android/colorpicker/ColorPickerDialog.java b/src/com/android/colorpicker/ColorPickerDialog.java
index c55a001..c389d1b 100644
--- a/src/com/android/colorpicker/ColorPickerDialog.java
+++ b/src/com/android/colorpicker/ColorPickerDialog.java
@@ -121,7 +121,12 @@
listener.onColorSelected(color);
}
- mSelectedColor = color;
+ if (color != mSelectedColor) {
+ mSelectedColor = color;
+ // Redraw palette to show checkmark on newly selected color before dismissing.
+ mPalette.drawPalette(mColors, mSelectedColor);
+ }
+
dismiss();
}
diff --git a/src/com/android/colorpicker/ColorPickerSwatch.java b/src/com/android/colorpicker/ColorPickerSwatch.java
index 875282d..250588c 100644
--- a/src/com/android/colorpicker/ColorPickerSwatch.java
+++ b/src/com/android/colorpicker/ColorPickerSwatch.java
@@ -17,22 +17,23 @@
package com.android.colorpicker;
import android.content.Context;
-import android.content.res.Resources;
import android.graphics.PorterDuff.Mode;
+import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
+import android.view.LayoutInflater;
import android.view.View;
+import android.widget.FrameLayout;
import android.widget.ImageView;
-import com.android.colorpicker.R;
-
/**
* Creates a circular swatch of a specified color. Adds a checkmark if marked as checked.
*/
-public class ColorPickerSwatch extends ImageView implements View.OnClickListener {
+public class ColorPickerSwatch extends FrameLayout implements View.OnClickListener {
private int mColor;
- private Drawable mColorDrawable;
- private Drawable mCheckmark;
+ private ImageView mSwatchImage;
+ private ImageView mCheckmarkImage;
private OnColorSelectedListener mOnColorSelectedListener;
/**
@@ -46,37 +47,31 @@
public void onColorSelected(int color);
}
- /**
- * @param context
- */
- public ColorPickerSwatch(Context context) {
- super(context);
- }
-
public ColorPickerSwatch(Context context, int color, boolean checked,
OnColorSelectedListener listener) {
super(context);
- setScaleType(ScaleType.FIT_XY);
- Resources res = context.getResources();
- mColorDrawable = res.getDrawable(R.drawable.color_picker_swatch);
- mCheckmark = res.getDrawable(R.drawable.ic_colorpicker_swatch_selected);
+ mColor = color;
mOnColorSelectedListener = listener;
+
+ LayoutInflater.from(context).inflate(R.layout.color_picker_swatch, this);
+ mSwatchImage = (ImageView) findViewById(R.id.color_picker_swatch);
+ mCheckmarkImage = (ImageView) findViewById(R.id.color_picker_checkmark);
setColor(color);
setChecked(checked);
setOnClickListener(this);
}
protected void setColor(int color) {
- mColor = color;
- mColorDrawable.setColorFilter(color, Mode.SRC_ATOP);
- setBackgroundDrawable(mColorDrawable);
+ Drawable[] colorDrawable = new Drawable[]
+ {getContext().getResources().getDrawable(R.drawable.color_picker_swatch)};
+ mSwatchImage.setImageDrawable(new ColorStateDrawable(colorDrawable, color));
}
private void setChecked(boolean checked) {
if (checked) {
- setImageDrawable(mCheckmark);
+ mCheckmarkImage.setVisibility(View.VISIBLE);
} else {
- setImageDrawable(null);
+ mCheckmarkImage.setVisibility(View.GONE);
}
}
diff --git a/src/com/android/colorpicker/ColorStateDrawable.java b/src/com/android/colorpicker/ColorStateDrawable.java
new file mode 100644
index 0000000..6b01cce
--- /dev/null
+++ b/src/com/android/colorpicker/ColorStateDrawable.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * 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 com.android.colorpicker;
+
+import android.graphics.Color;
+import android.graphics.PorterDuff;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
+
+/**
+ * A drawable which sets its color filter to a color specified by the user, and changes to a
+ * slightly darker color when pressed or focused.
+ */
+public class ColorStateDrawable extends LayerDrawable {
+
+ private static final float PRESSED_STATE_MULTIPLIER = 0.70f;
+
+ private int mColor;
+
+ public ColorStateDrawable(Drawable[] layers, int color) {
+ super(layers);
+ mColor = color;
+ }
+
+ @Override
+ protected boolean onStateChange(int[] states) {
+ boolean pressedOrFocused = false;
+ for (int state : states) {
+ if (state == android.R.attr.state_pressed || state == android.R.attr.state_focused) {
+ pressedOrFocused = true;
+ break;
+ }
+ }
+
+ if (pressedOrFocused) {
+ super.setColorFilter(getPressedColor(mColor), PorterDuff.Mode.SRC_ATOP);
+ } else {
+ super.setColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
+ }
+
+ return super.onStateChange(states);
+ }
+
+ /**
+ * Given a particular color, adjusts its value by a multiplier.
+ */
+ private int getPressedColor(int color) {
+ float[] hsv = new float[3];
+ Color.colorToHSV(color, hsv);
+ hsv[2] = hsv[2] * PRESSED_STATE_MULTIPLIER;
+ return Color.HSVToColor(hsv);
+ }
+
+ @Override
+ public boolean isStateful() {
+ return true;
+ }
+}