瀏覽代碼

The color chooser dialog in the sample project is now fully functional.

Aidan Follestad 10 年之前
父節點
當前提交
0a8f7e5ae2

+ 74 - 12
sample/src/main/java/com/afollestad/materialdialogssample/ColorChooserDialog.java

@@ -3,12 +3,18 @@ package com.afollestad.materialdialogssample;
 import android.app.Activity;
 import android.app.Dialog;
 import android.app.DialogFragment;
+import android.content.res.ColorStateList;
 import android.content.res.TypedArray;
+import android.graphics.Color;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.RippleDrawable;
 import android.graphics.drawable.ShapeDrawable;
+import android.graphics.drawable.StateListDrawable;
 import android.graphics.drawable.shapes.OvalShape;
 import android.os.Build;
 import android.os.Bundle;
 import android.view.View;
+import android.widget.FrameLayout;
 import android.widget.GridLayout;
 
 import com.afollestad.materialdialogs.MaterialDialog;
@@ -16,12 +22,22 @@ import com.afollestad.materialdialogs.MaterialDialog;
 /**
  * @author Aidan Follestad (afollestad)
  */
-public class ColorChooserDialog extends DialogFragment {
+public class ColorChooserDialog extends DialogFragment implements View.OnClickListener {
 
     private Callback mCallback;
+    private int[] mColors;
+
+    @Override
+    public void onClick(View v) {
+        if (v.getTag() != null) {
+            Integer index = (Integer) v.getTag();
+            mCallback.onColorSelection(index, mColors[index], shiftColor(mColors[index]));
+            dismiss();
+        }
+    }
 
     public static interface Callback {
-        void onColorSelection(int primary);
+        void onColorSelection(int index, int color, int darker);
     }
 
     public ColorChooserDialog() {
@@ -35,25 +51,71 @@ public class ColorChooserDialog extends DialogFragment {
                 .customView(R.layout.dialog_color_chooser, false)
                 .build();
 
-        TypedArray ta = getActivity().getResources().obtainTypedArray(R.array.material_colors_500);
-        int[] mColors = new int[ta.length()];
+        final TypedArray ta = getActivity().getResources().obtainTypedArray(R.array.material_colors_500);
+        mColors = new int[ta.length()];
         for (int i = 0; i < ta.length(); i++)
             mColors[i] = ta.getColor(i, 0);
         ta.recycle();
-        GridLayout list = (GridLayout) dialog.getCustomView().findViewById(R.id.grid);
+        final GridLayout list = (GridLayout) dialog.getCustomView().findViewById(R.id.grid);
+        final int preselect = getArguments().getInt("preselect", -1);
+
         for (int i = 0; i < list.getChildCount(); i++) {
-            View child = list.getChildAt(i);
-            ShapeDrawable circle = new ShapeDrawable(new OvalShape());
-            circle.getPaint().setColor(mColors[i]);
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
-                child.setBackground(circle);
-            else child.setBackgroundDrawable(circle);
+            FrameLayout child = (FrameLayout) list.getChildAt(i);
+            child.setTag(i);
+            child.setOnClickListener(this);
+            child.getChildAt(0).setVisibility(preselect == i ? View.VISIBLE : View.GONE);
+
+            Drawable selector = createSelector(mColors[i]);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                int[][] states = new int[][]{
+                        new int[]{-android.R.attr.state_pressed},
+                        new int[]{android.R.attr.state_pressed}
+                };
+                int[] colors = new int[]{
+                        shiftColor(mColors[i]),
+                        mColors[i]
+                };
+                ColorStateList rippleColors = new ColorStateList(states, colors);
+                setBackgroundCompat(child, new RippleDrawable(rippleColors, selector, null));
+            } else {
+                setBackgroundCompat(child, selector);
+            }
+
+
         }
         return dialog;
     }
 
-    public void show(Activity context, Callback callback) {
+    private void setBackgroundCompat(View view, Drawable d) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
+            view.setBackground(d);
+        else view.setBackgroundDrawable(d);
+    }
+
+    private int shiftColor(int color) {
+        float[] hsv = new float[3];
+        Color.colorToHSV(color, hsv);
+        hsv[2] *= 0.9f; // value component
+        return Color.HSVToColor(hsv);
+    }
+
+    private Drawable createSelector(int color) {
+        ShapeDrawable coloredCircle = new ShapeDrawable(new OvalShape());
+        coloredCircle.getPaint().setColor(color);
+        ShapeDrawable darkerCircle = new ShapeDrawable(new OvalShape());
+        darkerCircle.getPaint().setColor(shiftColor(color));
+
+        StateListDrawable stateListDrawable = new StateListDrawable();
+        stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, coloredCircle);
+        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, darkerCircle);
+        return stateListDrawable;
+    }
+
+    public void show(Activity context, int preselect, Callback callback) {
         mCallback = callback;
+        Bundle args = new Bundle();
+        args.putInt("preselect", preselect);
+        setArguments(args);
         show(context.getFragmentManager(), "COLOR_SELECTOR");
     }
 }

+ 10 - 3
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -1,6 +1,8 @@
 package com.afollestad.materialdialogssample;
 
 import android.content.DialogInterface;
+import android.graphics.drawable.ColorDrawable;
+import android.os.Build;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.text.Editable;
@@ -407,11 +409,16 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
         dialog.show();
     }
 
+    static int selectedColorIndex = -1;
+
     private void showCustomColorChooser() {
-        new ColorChooserDialog().show(this, new ColorChooserDialog.Callback() {
+        new ColorChooserDialog().show(this, selectedColorIndex, new ColorChooserDialog.Callback() {
             @Override
-            public void onColorSelection(int primary) {
-
+            public void onColorSelection(int index, int color, int darker) {
+                selectedColorIndex = index;
+                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
+                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
+                    getWindow().setStatusBarColor(darker);
             }
         });
     }

二進制
sample/src/main/res/drawable-hdpi/ic_check.png


二進制
sample/src/main/res/drawable-mdpi/ic_check.png


二進制
sample/src/main/res/drawable-xhdpi/ic_check.png


二進制
sample/src/main/res/drawable-xxhdpi/ic_check.png


+ 221 - 42
sample/src/main/res/layout/dialog_color_chooser.xml

@@ -13,109 +13,288 @@
         android:rowCount="5"
         android:orientation="vertical"
         tools:ignore="UselessParent"
-        android:paddingTop="4dp"
-        android:paddingBottom="16dp"
+        android:paddingBottom="20dp"
         android:clipToPadding="false">
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
 
-        <View
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
 
-        <View
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
 
-        <View
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
 
-        <View
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
 
-        <View
+        </FrameLayout>
+
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
-        <View
+        <FrameLayout
             android:layout_width="56dp"
             android:layout_height="56dp"
-            android:layout_margin="4dp" />
+            android:layout_margin="4dp"
+            tools:background="#000">
+
+            <ImageView
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:src="@drawable/ic_check"
+                android:layout_gravity="center" />
+
+        </FrameLayout>
 
     </GridLayout>