Improve color picker layout / cleaned up some of the code

- Fixed possible NPE with calling getWindow()
- Remove unnecessary panel of preset colors underneath the color picker
- Changed dialog to AlertDialog to match LED color picker

While this doesn't look EXACTLY like the LED color picker, this along with
the commit below bring it closer than ever before.

http://gerrit.dirtyunicorns.com/#/c/1199/

Change-Id: I13bc40d1882676d92e5c4ae4bfcf25865e88dad1

Signed-off-by: xyyx <xyyx@mail.ru>
This commit is contained in:
Alex Cruz
2017-11-26 00:14:37 -05:00
committed by hungphan2001
parent 8dc8247882
commit 0950761101
3 changed files with 66 additions and 228 deletions

View File

@@ -19,106 +19,54 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:padding="@dimen/alert_dialog_padding_material"
android:orientation="horizontal">
<net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:tag="landscape" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="@string/hex"
android:textSize="20sp"
android:layout_marginRight="10dp" />
android:textSize="20sp" />
<EditText
android:id="@+id/hex"
android:hint="@string/hex_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:hint="@string/hex_hint" />
<ImageButton
android:id="@+id/enter"
android:src="@drawable/ic_action_set"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="2dp">
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/white_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/black_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/cyan_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/red_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/green_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/yellow_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
android:layout_marginStart="10dp"
android:background="@android:color/transparent"
android:gravity="center"
android:src="@drawable/ic_action_set" />
</LinearLayout>
<TextView
@@ -126,8 +74,8 @@
android:layout_height="wrap_content"
android:text="@string/press_color_to_apply"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
@@ -156,7 +104,6 @@
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
</LinearLayout>

View File

@@ -19,97 +19,46 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:padding="@dimen/alert_dialog_padding_material"
android:orientation="vertical">
<net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="portrait" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="@string/hex"
android:textSize="20sp"
android:layout_marginRight="10dp" />
android:textSize="20sp" />
<EditText
android:id="@+id/hex"
android:hint="@string/hex_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:hint="@string/hex_hint" />
<ImageButton
android:id="@+id/enter"
android:src="@drawable/ic_action_set"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center" />
</LinearLayout>
<net.margaritov.preference.colorpicker.ColorPickerView
android:id="@+id/color_picker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:tag="portrait" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="2dp">
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/white_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/black_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/cyan_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/red_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/green_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:layout_weight="0.5" />
<net.margaritov.preference.colorpicker.ColorPickerPanelView
android:id="@+id/yellow_panel"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
android:layout_marginStart="10dp"
android:background="@android:color/transparent"
android:gravity="center"
android:src="@drawable/ic_action_set" />
</LinearLayout>
@@ -118,8 +67,8 @@
android:layout_height="wrap_content"
android:text="@string/press_color_to_apply"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout

View File

@@ -17,11 +17,11 @@
package net.margaritov.preference.colorpicker;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
@@ -31,46 +31,31 @@ import android.widget.LinearLayout;
import com.android.settings.R;
public class ColorPickerDialog
extends
Dialog
implements
ColorPickerView.OnColorChangedListener,
View.OnClickListener {
public class ColorPickerDialog extends AlertDialog implements ColorPickerView.OnColorChangedListener, View.OnClickListener {
private ColorPickerView mColorPicker;
private ColorPickerPanelView mOldColor;
private ColorPickerPanelView mNewColor;
private ColorPickerPanelView mWhite;
private ColorPickerPanelView mBlack;
private ColorPickerPanelView mCyan;
private ColorPickerPanelView mRed;
private ColorPickerPanelView mGreen;
private ColorPickerPanelView mYellow;
private EditText mHex;
private ImageButton mSetButton;
private OnColorChangedListener mListener;
public interface OnColorChangedListener {
public void onColorChanged(int color);
void onColorChanged(int color);
}
public ColorPickerDialog(Context context, int initialColor) {
ColorPickerDialog(Context context, int initialColor) {
super(context);
init(initialColor);
}
private void init(int color) {
// To fight color branding.
getWindow().setFormat(PixelFormat.RGBA_8888);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setUp(color);
if (getWindow() != null) {
getWindow().setFormat(PixelFormat.RGBA_8888);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setUp(color);
}
}
private void setUp(int color) {
@@ -78,32 +63,18 @@ public class ColorPickerDialog
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
View layout = inflater.inflate(R.layout.dui_dialog_color_picker, null);
setContentView(layout);
mColorPicker = layout.findViewById(R.id.color_picker_view);
mOldColor = layout.findViewById(R.id.old_color_panel);
mNewColor = layout.findViewById(R.id.new_color_panel);
setTitle(R.string.dialog_color_picker);
mHex = layout.findViewById(R.id.hex);
ImageButton mSetButton = layout.findViewById(R.id.enter);
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);
mWhite = (ColorPickerPanelView) layout.findViewById(R.id.white_panel);
mBlack = (ColorPickerPanelView) layout.findViewById(R.id.black_panel);
mCyan = (ColorPickerPanelView) layout.findViewById(R.id.cyan_panel);
mRed = (ColorPickerPanelView) layout.findViewById(R.id.red_panel);
mGreen = (ColorPickerPanelView) layout.findViewById(R.id.green_panel);
mYellow = (ColorPickerPanelView) layout.findViewById(R.id.yellow_panel);
mHex = (EditText) layout.findViewById(R.id.hex);
mSetButton = (ImageButton) layout.findViewById(R.id.enter);
((LinearLayout) mOldColor.getParent()).setPadding(
Math.round(mColorPicker.getDrawingOffset()),
0,
Math.round(mColorPicker.getDrawingOffset()),
0
);
((LinearLayout) mOldColor.getParent()).setPadding(Math.round(mColorPicker.getDrawingOffset()),
0, Math.round(mColorPicker.getDrawingOffset()), 0);
mOldColor.setOnClickListener(this);
mNewColor.setOnClickListener(this);
@@ -111,30 +82,21 @@ public class ColorPickerDialog
mOldColor.setColor(color);
mColorPicker.setColor(color, true);
setColorAndClickAction(mWhite, Color.WHITE);
setColorAndClickAction(mBlack, Color.BLACK);
setColorAndClickAction(mCyan, 0xff33b5e5);
setColorAndClickAction(mRed, Color.RED);
setColorAndClickAction(mGreen, Color.GREEN);
setColorAndClickAction(mYellow, Color.YELLOW);
if (mHex != null) {
mHex.setText(ColorPickerPreference.convertToARGB(color));
}
if (mSetButton != null) {
mSetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mHex.getText().toString();
try {
int newColor = ColorPickerPreference.convertToColorInt(text);
mColorPicker.setColor(newColor, true);
} catch (Exception e) {
}
mSetButton.setOnClickListener(v -> {
String text = mHex.getText().toString();
try {
int newColor = ColorPickerPreference.convertToColorInt(text);
mColorPicker.setColor(newColor, true);
} catch (Exception ignored) {
}
});
}
setView(layout);
}
@Override
@@ -145,40 +107,20 @@ public class ColorPickerDialog
if (mHex != null) {
mHex.setText(ColorPickerPreference.convertToARGB(color));
}
} catch (Exception e) {
} catch (Exception ignored) {
}
/*
* if (mListener != null) { mListener.onColorChanged(color); }
*/
}
public void setAlphaSliderVisible(boolean visible) {
void setAlphaSliderVisible(boolean visible) {
mColorPicker.setAlphaSliderVisible(visible);
}
public void setColorAndClickAction(ColorPickerPanelView previewRect, final int color) {
if (previewRect != null) {
previewRect.setColor(color);
previewRect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mColorPicker.setColor(color, true);
} catch (Exception e) {
}
}
});
}
}
/**
* Set a OnColorChangedListener to get notified when the color selected by the user has changed.
*
* @param listener
*/
public void setOnColorChangedListener(OnColorChangedListener listener) {
void setOnColorChangedListener(OnColorChangedListener listener) {
mListener = listener;
}
@@ -196,6 +138,7 @@ public class ColorPickerDialog
dismiss();
}
@NonNull
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
@@ -205,10 +148,9 @@ public class ColorPickerDialog
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mOldColor.setColor(savedInstanceState.getInt("old_color"));
mColorPicker.setColor(savedInstanceState.getInt("new_color"), true);
}
}