diff options
author | steckbrief <steckbrief@chefmail.de> | 2015-04-11 23:00:10 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2015-04-11 23:00:10 +0200 |
commit | 17779e77002e99dabb73f4fa8f9596e9d220319b (patch) | |
tree | 7604484b69948e299c95a58f2f6be8fd78056fd7 /src/main/java/yuku/ambilwarna/widget | |
parent | 94a669fcb861d9cff4414d98abb289d1452c9300 (diff) |
Color choose dialog
Diffstat (limited to 'src/main/java/yuku/ambilwarna/widget')
-rw-r--r-- | src/main/java/yuku/ambilwarna/widget/AmbilWarnaPrefWidgetView.java | 34 | ||||
-rw-r--r-- | src/main/java/yuku/ambilwarna/widget/AmbilWarnaPreference.java | 134 |
2 files changed, 168 insertions, 0 deletions
diff --git a/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPrefWidgetView.java b/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPrefWidgetView.java new file mode 100644 index 00000000..6da10bb0 --- /dev/null +++ b/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPrefWidgetView.java @@ -0,0 +1,34 @@ +package yuku.ambilwarna.widget; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Style; +import android.util.AttributeSet; +import android.util.FloatMath; +import android.view.View; + +public class AmbilWarnaPrefWidgetView extends View { + Paint paint; + float rectSize; + float strokeWidth; + + public AmbilWarnaPrefWidgetView(Context context, AttributeSet attrs) { + super(context, attrs); + + float density = context.getResources().getDisplayMetrics().density; + rectSize = FloatMath.floor(24.f * density + 0.5f); + strokeWidth = FloatMath.floor(1.f * density + 0.5f); + + paint = new Paint(); + paint.setColor(0xffffffff); + paint.setStyle(Style.STROKE); + paint.setStrokeWidth(strokeWidth); + } + + @Override protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + canvas.drawRect(strokeWidth, strokeWidth, rectSize - strokeWidth, rectSize - strokeWidth, paint); + } +}
\ No newline at end of file diff --git a/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPreference.java b/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPreference.java new file mode 100644 index 00000000..f18ed00c --- /dev/null +++ b/src/main/java/yuku/ambilwarna/widget/AmbilWarnaPreference.java @@ -0,0 +1,134 @@ +package yuku.ambilwarna.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.os.Parcel; +import android.os.Parcelable; +import android.preference.Preference; +import android.util.AttributeSet; +import android.view.View; + +import eu.siacs.conversations.R; +import yuku.ambilwarna.AmbilWarnaDialog; + +public class AmbilWarnaPreference extends Preference { + private final boolean supportsAlpha; + int value; + + public AmbilWarnaPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AmbilWarnaPreference); + supportsAlpha = ta.getBoolean(R.styleable.AmbilWarnaPreference_supportsAlpha, false); + + setWidgetLayoutResource(R.layout.ambilwarna_pref_widget); + } + + @Override protected void onBindView(View view) { + super.onBindView(view); + + // Set our custom views inside the layout + final View box = view.findViewById(R.id.ambilwarna_pref_widget_box); + if (box != null) { + box.setBackgroundColor(value); + } + } + + @Override protected void onClick() { + new AmbilWarnaDialog(getContext(), value, supportsAlpha, new AmbilWarnaDialog.OnAmbilWarnaListener() { + @Override public void onOk(AmbilWarnaDialog dialog, int color) { + if (!callChangeListener(color)) return; // They don't want the value to be set + value = color; + persistInt(value); + notifyChanged(); + } + + @Override public void onCancel(AmbilWarnaDialog dialog) { + // nothing to do + } + }).show(); + } + + public void forceSetValue(int value) { + this.value = value; + persistInt(value); + notifyChanged(); + } + + @Override protected Object onGetDefaultValue(TypedArray a, int index) { + // This preference type's value type is Integer, so we read the default value from the attributes as an Integer. + return a.getInteger(index, 0); + } + + @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + if (restoreValue) { // Restore state + value = getPersistedInt(value); + } else { // Set state + int value = (Integer) defaultValue; + this.value = value; + persistInt(value); + } + } + + /* + * Suppose a client uses this preference type without persisting. We + * must save the instance state so it is able to, for example, survive + * orientation changes. + */ + @Override protected Parcelable onSaveInstanceState() { + final Parcelable superState = super.onSaveInstanceState(); + if (isPersistent()) return superState; // No need to save instance state since it's persistent + + final SavedState myState = new SavedState(superState); + myState.value = value; + return myState; + } + + @Override protected void onRestoreInstanceState(Parcelable state) { + if (!state.getClass().equals(SavedState.class)) { + // Didn't save state for us in onSaveInstanceState + super.onRestoreInstanceState(state); + return; + } + + // Restore the instance state + SavedState myState = (SavedState) state; + super.onRestoreInstanceState(myState.getSuperState()); + this.value = myState.value; + notifyChanged(); + } + + /** + * SavedState, a subclass of {@link android.preference.Preference.BaseSavedState}, will store the state + * of MyPreference, a subclass of Preference. + * <p> + * It is important to always call through to super methods. + */ + private static class SavedState extends BaseSavedState { + int value; + + public SavedState(Parcel source) { + super(source); + value = source.readInt(); + } + + @Override public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeInt(value); + } + + public SavedState(Parcelable superState) { + super(superState); + } + + @SuppressWarnings("unused") public static final Creator<SavedState> CREATOR = new Creator<SavedState>() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } +}
\ No newline at end of file |