在android中使用的颜色选择器

Posted

技术标签:

【中文标题】在android中使用的颜色选择器【英文标题】:Color picker with usage in android 【发布时间】:2013-04-02 15:04:11 【问题描述】:

谁能给我一些颜色选择器的代码以及它在android中的用法?

我在这里找到了一些例子Android Color Picker,但我不知道如何使用它:(

请帮帮我,谢谢

【问题讨论】:

到目前为止你做了什么? 【参考方案1】:

颜色选择器对话框。

public class ColorPickerDialog extends Dialog 

public interface OnColorChangedListener 
    void colorChanged(int color);


private OnColorChangedListener mListener;
private int mInitialColor;

private static class ColorPickerView extends View 
    private Paint mPaint;
    private Paint mCenterPaint;
    private final int[] mColors;
    private OnColorChangedListener mListener;

    ColorPickerView(Context c, OnColorChangedListener l, int color) 
        super(c);
        mListener = l;
        mColors = new int[] 
            0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
            0xFFFFFF00, 0xFFFF0000
        ;
        Shader s = new SweepGradient(0, 0, mColors, null);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setShader(s);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(32);

        mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCenterPaint.setColor(color);
        mCenterPaint.setStrokeWidth(5);
    

    private boolean mTrackingCenter;
    private boolean mHighlightCenter;

    @Override
    protected void onDraw(Canvas canvas) 
        float r = CENTER_X - mPaint.getStrokeWidth()*0.5f;

        canvas.translate(CENTER_X, CENTER_X);

        canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
        canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);

        if (mTrackingCenter) 
            int c = mCenterPaint.getColor();
            mCenterPaint.setStyle(Paint.Style.STROKE);

            if (mHighlightCenter) 
                mCenterPaint.setAlpha(0xFF);
             else 
                mCenterPaint.setAlpha(0x80);
            
            canvas.drawCircle(0, 0,
                              CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
                              mCenterPaint);

            mCenterPaint.setStyle(Paint.Style.FILL);
            mCenterPaint.setColor(c);
        
    

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
        setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
    

    private static final int CENTER_X = 100;
    private static final int CENTER_Y = 100;
    private static final int CENTER_RADIUS = 32;

    private int floatToByte(float x) 
        int n = java.lang.Math.round(x);
        return n;
    
    private int pinToByte(int n) 
        if (n < 0) 
            n = 0;
         else if (n > 255) 
            n = 255;
        
        return n;
    

    private int ave(int s, int d, float p) 
        return s + java.lang.Math.round(p * (d - s));
    

    private int interpColor(int colors[], float unit) 
        if (unit <= 0) 
            return colors[0];
        
        if (unit >= 1) 
            return colors[colors.length - 1];
        

        float p = unit * (colors.length - 1);
        int i = (int)p;
        p -= i;

        // now p is just the fractional part [0...1) and i is the index
        int c0 = colors[i];
        int c1 = colors[i+1];
        int a = ave(Color.alpha(c0), Color.alpha(c1), p);
        int r = ave(Color.red(c0), Color.red(c1), p);
        int g = ave(Color.green(c0), Color.green(c1), p);
        int b = ave(Color.blue(c0), Color.blue(c1), p);

        return Color.argb(a, r, g, b);
    

    private int rotateColor(int color, float rad) 
        float deg = rad * 180 / 3.1415927f;
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);

        ColorMatrix cm = new ColorMatrix();
        ColorMatrix tmp = new ColorMatrix();

        cm.setRGB2YUV();
        tmp.setRotate(0, deg);
        cm.postConcat(tmp);
        tmp.setYUV2RGB();
        cm.postConcat(tmp);

        final float[] a = cm.getArray();

        int ir = floatToByte(a[0] * r +  a[1] * g +  a[2] * b);
        int ig = floatToByte(a[5] * r +  a[6] * g +  a[7] * b);
        int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

        return Color.argb(Color.alpha(color), pinToByte(ir),
                          pinToByte(ig), pinToByte(ib));
    

    private static final float PI = 3.1415926f;

    @Override
    public boolean onTouchEvent(MotionEvent event) 
        float x = event.getX() - CENTER_X;
        float y = event.getY() - CENTER_Y;
        boolean inCenter = java.lang.Math.sqrt(x*x + y*y) <= CENTER_RADIUS;

        switch (event.getAction()) 
            case MotionEvent.ACTION_DOWN:
                mTrackingCenter = inCenter;
                if (inCenter) 
                    mHighlightCenter = true;
                    invalidate();
                    break;
                
            case MotionEvent.ACTION_MOVE:
                if (mTrackingCenter) 
                    if (mHighlightCenter != inCenter) 
                        mHighlightCenter = inCenter;
                        invalidate();
                    
                 else 
                    float angle = (float)java.lang.Math.atan2(y, x);
                    // need to turn angle [-PI ... PI] into unit [0....1]
                    float unit = angle/(2*PI);
                    if (unit < 0) 
                        unit += 1;
                    
                    mCenterPaint.setColor(interpColor(mColors, unit));
                    invalidate();
                
                break;
            case MotionEvent.ACTION_UP:
                if (mTrackingCenter) 
                    if (inCenter) 
                        mListener.colorChanged(mCenterPaint.getColor());
                    
                    mTrackingCenter = false;    // so we draw w/o halo
                    invalidate();
                
                break;
        
        return true;
    


public ColorPickerDialog(Context context,
                         OnColorChangedListener listener,
                         int initialColor) 
    super(context);

    mListener = listener;
    mInitialColor = initialColor;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    OnColorChangedListener l = new OnColorChangedListener() 
        public void colorChanged(int color) 
            mListener.colorChanged(color);
            dismiss();
        
    ;

    setContentView(new ColorPickerView(getContext(), l, mInitialColor));
    setTitle("Pick a Color");
 

用法

new ColorPickerDialog(FingerPaintActivity.this, FingerPaintActivity.this, mPaint.getColor()).show();

FingerPaintActivity.this - 是活动上下文 mPaint 是绘制对象。

无论用户选择什么颜色,对象都将具有该颜色。如果用户选择红色,mPaint 将呈现红色,可用于绘制。

显示颜色选择器

点击按钮时说你想显示颜色选择器对话框

Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()

    public void OnClick(View v)
    
        new ColorPickerDialog(FingerPaintActivity.this, FingerPaintActivity.this, mPaint.getColor()).show();
      
); 

【讨论】:

@Tünde 很高兴它有帮助。请采纳答案 如何对自定义颜色选择器使用相同的方法? @SiKni8 你设计了一个自定义颜色选择器吗? 我有一个带有调色板背景和一个小拇指的布局,用户可以拖动它,它应该提供 RGB 值但它不起作用......我的问题在这里***.com/questions/21567226/… . 我要做的是使用背景作为图像位图,当用户通过触摸拖动时,拇指移动,如果用户拖动到布局之外......拇指 X 和 Y 是离开布局前的最后一个位置。【参考方案2】:

我找到了这个:https://www.buzzingandroid.com/2012/11/hsv-color-picker-dialog

由于 FloatMath 被贬值,我做了以下更改:

import java.lang.Math;
 //import android.util.FloatMath;

selectedPoint.x = rect.left + (int) ((float)-Math.cos( hueInPiInterval ) * colorHsv[1] * innerCircleRadius + fullCircleRadius);
selectedPoint.y = rect.top + (int) ((float)-Math.sin( hueInPiInterval ) * colorHsv[1] * innerCircleRadius + fullCircleRadius);
//selectedPoint.x = rect.left + (int) (-FloatMath.cos( hueInPiInterval ) * colorHsv[1] * innerCircleRadius + fullCircleRadius);
//selectedPoint.y = rect.top + (int) (-FloatMath.sin( hueInPiInterval ) * colorHsv[1] * innerCircleRadius + fullCircleRadius);

` 这是我见过的最好的颜色选择器!非常易于使用且描述良好。希望它对你也有用。

【讨论】:

以上是关于在android中使用的颜色选择器的主要内容,如果未能解决你的问题,请参考以下文章

Android-Widget:使用 RemoteView 更改 ListView 选择器颜色

更改时间选择器的文本颜色 [android]

Android 颜色渲染 颜色选择器 ColorPickerDialog剖析

选择器按钮的颜色在 android 5.1 上没有改变

Android ListView 选择器颜色

android中具有背景颜色和imageview的自定义选择器