安卓颜色选择器

50

我正在寻找一个颜色选择器框架,可以在选择后返回颜色的十六进制值。

我已经查看了这个,想知道是否有其他库可供使用。

8个回答

44

我知道这个问题很老,但如果有人正在寻找一个使用材料设计的优秀Android颜色选择器,我从github上分叉了一个很棒的项目,并制作了一个简单易用的Android颜色选择器对话框。

这是该项目:Android Color Picker

Android颜色选择器对话框

enter image description here

如何使用它

将库添加到您的项目中

aar文件可以在jcenter存储库中获取。在您的build.gradle中声明存储库和依赖项。

(root)

repositories {
    jcenter()
}

(模块)

dependencies {
    compile 'com.pes.materialcolorpicker:library:1.0.2'
}

使用库

创建一个颜色选择器对话框对象

final ColorPicker cp = new ColorPicker(MainActivity.this, defaultColorR, defaultColorG, defaultColorB);

defaultColorR、defaultColorG和defaultColorB是三个整数(取值范围0-255),用于使用自定义颜色值初始化颜色选择器。如果您不想以颜色开始,则将它们设置为0或仅使用第一个参数。

然后显示对话框(在您需要的时间和位置),并保存所选的颜色。

/* Show color picker dialog */
cp.show();

/* On Click listener for the dialog, when the user select the color */
Button okColor = (Button)cp.findViewById(R.id.okColorButton);

okColor.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            
        /* You can get single channel (value 0-255) */
        selectedColorR = cp.getRed();
        selectedColorG = cp.getGreen();
        selectedColorB = cp.getBlue();
            
        /* Or the android RGB Color (see the android Color class reference) */
        selectedColorRGB = cp.getColor();

        cp.dismiss();
    }
});

就这样了 :)


2
太棒了,伙计 :D - Harsha M V
我两周前需要它,所以我自己做了。希望对其他人有所帮助。如果您有任何建议,请告诉我。 - Simone Pessotto
1
@SimonePessotto 在 buttonOk 上设置了 onClickListener,但是出现了空指针异常。请帮我解决。 - CLIFFORD P Y
@Clifford 我会检查一下...你具体做了什么? - Simone Pessotto
1
请@SimonePessotto更新依赖项: compile 'com.pes.materialcolorpicker:library:1.2.5' - nicomonjelat
显示剩余7条评论

22

链接已经失效。 - Massimo
1
这个库还包括一个漂亮的颜色选择器和调色板:https://github.com/eltos/SimpleDialogFragments - Philipp

17

你可以使用以下代码,它将使你的页面看起来与http://code.google.com/p/color-picker-view/相同。

public class ColorPickerDialog extends Dialog {

public interface OnColorChangedListener {
    void colorChanged(String key, int color);
}

private OnColorChangedListener mListener;
private int mInitialColor, mDefaultColor;
private String mKey;

private static class ColorPickerView extends View {
    private Paint mPaint;
    private float mCurrentHue = 0;
    private int mCurrentX = 0, mCurrentY = 0;
    private int mCurrentColor, mDefaultColor;
    private final int[] mHueBarColors = new int[258];
    private int[] mMainColors = new int[65536];
    private OnColorChangedListener mListener;

    ColorPickerView(Context c, OnColorChangedListener l, int color,
            int defaultColor) {
        super(c);
        mListener = l;
        mDefaultColor = defaultColor;

        // Get the current hue from the current color and update the main
        // color field
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        mCurrentHue = hsv[0];
        updateMainColors();

        mCurrentColor = color;

        // Initialize the colors of the hue slider bar
        int index = 0;
        for (float i = 0; i < 256; i += 256 / 42) // Red (#f00) to pink
                                                    // (#f0f)
        {
            mHueBarColors[index] = Color.rgb(255, 0, (int) i);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) // Pink (#f0f) to blue
                                                    // (#00f)
        {
            mHueBarColors[index] = Color.rgb(255 - (int) i, 0, 255);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) // Blue (#00f) to light
                                                    // blue (#0ff)
        {
            mHueBarColors[index] = Color.rgb(0, (int) i, 255);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) // Light blue (#0ff) to
                                                    // green (#0f0)
        {
            mHueBarColors[index] = Color.rgb(0, 255, 255 - (int) i);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) // Green (#0f0) to yellow
                                                    // (#ff0)
        {
            mHueBarColors[index] = Color.rgb((int) i, 255, 0);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) // Yellow (#ff0) to red
                                                    // (#f00)
        {
            mHueBarColors[index] = Color.rgb(255, 255 - (int) i, 0);
            index++;
        }

        // Initializes the Paint that will draw the View
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTextSize(12);
    }

    // Get the current selected color from the hue bar
    private int getCurrentMainColor() {
        int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
        int index = 0;
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb(255, 0, (int) i);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb(255 - (int) i, 0, 255);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb(0, (int) i, 255);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb(0, 255, 255 - (int) i);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb((int) i, 255, 0);
            index++;
        }
        for (float i = 0; i < 256; i += 256 / 42) {
            if (index == translatedHue)
                return Color.rgb(255, 255 - (int) i, 0);
            index++;
        }
        return Color.RED;
    }

    // Update the main field colors depending on the current selected hue
    private void updateMainColors() {
        int mainColor = getCurrentMainColor();
        int index = 0;
        int[] topColors = new int[256];
        for (int y = 0; y < 256; y++) {
            for (int x = 0; x < 256; x++) {
                if (y == 0) {
                    mMainColors[index] = Color.rgb(
                            255 - (255 - Color.red(mainColor)) * x / 255,
                            255 - (255 - Color.green(mainColor)) * x / 255,
                            255 - (255 - Color.blue(mainColor)) * x / 255);
                    topColors[x] = mMainColors[index];
                } else
                    mMainColors[index] = Color.rgb(
                            (255 - y) * Color.red(topColors[x]) / 255,
                            (255 - y) * Color.green(topColors[x]) / 255,
                            (255 - y) * Color.blue(topColors[x]) / 255);
                index++;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
        // Display all the colors of the hue bar with lines
        for (int x = 0; x < 256; x++) {
            // If this is not the current selected hue, display the actual
            // color
            if (translatedHue != x) {
                mPaint.setColor(mHueBarColors[x]);
                mPaint.setStrokeWidth(1);
            } else // else display a slightly larger black line
            {
                mPaint.setColor(Color.BLACK);
                mPaint.setStrokeWidth(3);
            }
            canvas.drawLine(x + 10, 0, x + 10, 40, mPaint);
            // canvas.drawLine(0, x+10, 40, x+10, mPaint);
        }

        // Display the main field colors using LinearGradient
        for (int x = 0; x < 256; x++) {
            int[] colors = new int[2];
            colors[0] = mMainColors[x];
            colors[1] = Color.BLACK;
            Shader shader = new LinearGradient(0, 50, 0, 306, colors, null,
                    Shader.TileMode.REPEAT);
            mPaint.setShader(shader);
            canvas.drawLine(x + 10, 50, x + 10, 306, mPaint);
        }
        mPaint.setShader(null);

        // Display the circle around the currently selected color in the
        // main field
        if (mCurrentX != 0 && mCurrentY != 0) {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.BLACK);
            canvas.drawCircle(mCurrentX, mCurrentY, 10, mPaint);
        }

        // Draw a 'button' with the currently selected color
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mCurrentColor);
        canvas.drawRect(10, 316, 138, 356, mPaint);

        // Set the text color according to the brightness of the color
        if (Color.red(mCurrentColor) + Color.green(mCurrentColor)
                + Color.blue(mCurrentColor) < 384)
            mPaint.setColor(Color.WHITE);
        else
            mPaint.setColor(Color.BLACK);
        canvas.drawText(
                getResources()
                        .getString(R.string.settings_bg_color_confirm), 74,
                340, mPaint);

        // Draw a 'button' with the default color
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mDefaultColor);
        canvas.drawRect(138, 316, 266, 356, mPaint);

        // Set the text color according to the brightness of the color
        if (Color.red(mDefaultColor) + Color.green(mDefaultColor)
                + Color.blue(mDefaultColor) < 384)
            mPaint.setColor(Color.WHITE);
        else
            mPaint.setColor(Color.BLACK);
        canvas.drawText(
                getResources().getString(
                        R.string.settings_default_color_confirm), 202, 340,
                mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(276, 366);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN)
            return true;
        float x = event.getX();
        float y = event.getY();

        // If the touch event is located in the hue bar
        if (x > 10 && x < 266 && y > 0 && y < 40) {
            // Update the main field colors
            mCurrentHue = (255 - x) * 360 / 255;
            updateMainColors();

            // Update the current selected color
            int transX = mCurrentX - 10;
            int transY = mCurrentY - 60;
            int index = 256 * (transY - 1) + transX;
            if (index > 0 && index < mMainColors.length)
                mCurrentColor = mMainColors[256 * (transY - 1) + transX];

            // Force the redraw of the dialog
            invalidate();
        }

        // If the touch event is located in the main field
        if (x > 10 && x < 266 && y > 50 && y < 306) {
            mCurrentX = (int) x;
            mCurrentY = (int) y;
            int transX = mCurrentX - 10;
            int transY = mCurrentY - 60;
            int index = 256 * (transY - 1) + transX;
            if (index > 0 && index < mMainColors.length) {
                // Update the current color
                mCurrentColor = mMainColors[index];
                // Force the redraw of the dialog
                invalidate();
            }
        }

        // If the touch event is located in the left button, notify the
        // listener with the current color
        if (x > 10 && x < 138 && y > 316 && y < 356)
            mListener.colorChanged("", mCurrentColor);

        // If the touch event is located in the right button, notify the
        // listener with the default color
        if (x > 138 && x < 266 && y > 316 && y < 356)
            mListener.colorChanged("", mDefaultColor);

        return true;
    }
}

public ColorPickerDialog(Context context, OnColorChangedListener listener,
        String key, int initialColor, int defaultColor) {
    super(context);

    mListener = listener;
    mKey = key;
    mInitialColor = initialColor;
    mDefaultColor = defaultColor;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    OnColorChangedListener l = new OnColorChangedListener() {
        public void colorChanged(String key, int color) {
            mListener.colorChanged(mKey, color);
            dismiss();
        }
    };

    setContentView(new ColorPickerView(getContext(), l, mInitialColor,
            mDefaultColor));
    setTitle(R.string.settings_bg_color_dialog);

    }
}

@jRowan,我可以问一下您是如何在您的活动中初始化“对话框”的吗? - Menelaos Kotsollaris
2
在这个链接中,@Try_me34,我在编辑中解释了它:https://dev59.com/hn7aa4cB1Zd3GeqPmSSR#22726100 - JRowan
7
这代码真糟糕,用起来真是让人头疼。 - Firzen
如果未来有人来到这里,这是我创建的一个类:https://github.com/carlosbar/AndroidColorPicker - Carlos Barcellos

14
我在寻找一款提供颜色透明度选择和十六进制值复制/粘贴功能的HSV颜色选择器时来到这里。然而,现有答案都无法满足我的需求,所以我编写了下面这个库: HSV-Alpha Color Picker for Android(GitHub链接)。
你也可以通过以下链接获取该应用程序的演示版本:HSV-Alpha Color Picker Demo(Google Play链接)。
希望这对其他人有用。
以下是该颜色选择器的对话框截图:Color Picker Dialog

这是一个很棒的颜色选择器库。 - Thracian
我们能在商业应用中自由使用这个库吗? - Samudra Ganguly
基本上是的。(许可证在 GitHub 存储库中 -- Apache 2.0。) - Martin Stone

8

简单易用。 - quilkin

5
这是另一个库:
https://github.com/eltos/SimpleDialogFragments 该库包含颜色选择器和调色板对话框。 enter image description here enter image description here

HSV颜色选择器用法:

从Maven添加库,然后在您的代码中调用:
SimpleColorWheelDialog.build()
                      .color(0xFFCF4747) // optional initial color
                      .alpha(true)
                      .show(Activity.this, COLOR_PICKER);

为了获得结果,让Activity或Fragment实现OnDialogResultListener

@Override
public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle extras) {
    if (COLOR_PICKER.equals(dialogTag) && which == BUTTON_POSITIVE){
        int color = extras.getInt(SimpleColorWheelDialog.COLOR);
        // ...
        return true;
    }
    return false;
}


4
如果你需要一个片段解决方案,我已经创建了一个分支的android-color-picker,在这个版本中使用了DialogFragment,并且在配置更改时会重新创建。以下是链接: https://github.com/lomza/android-color-picker

1
在 Android 参考文献中进行了一些搜索后,新手 QuadFlask Color Picker 似乎是一个技术和美学上都不错的选择。此外,它还具有透明度滑块并支持 HEX 编码颜色。
看一下:
QuadFlask Color Picker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接