排序颜色的整数表示 - android

时间:2013-06-18 17:45:05

标签: android sorting arraylist

下面是我使用的绘图工具类,就像画笔笔尖一样。对于绘图应用程序。

在我的主要活动中,我有一个这个对象类型的arrayList,我想按颜色对它们进行排序。

正如你所看到的,我有一个只返回int1-int2的compareTo方法(其中int是每个工具的颜色值..但是这并没有按照我预期的方式对它们进行排序。

我试图用颜色像彩虹一样对它们进行排序。

现在我知道如果它们是Color而不是Int类型可能会更容易,但数据源将返回Int值,如0xff000000为黑色。

public class KNDrawingTool implements Comparable<KNDrawingTool>{

    public int id;
    public String name;
    public int size;
    public int color;
    public int cost;
    public int capacity;
    public int amountLeft;
    public String type;
    public Paint mPaint;


    public KNDrawingTool(String toolType, Boolean flatTip, int paintId, String paintName, int paintSize,int paintColor, int paintCost, int canCapacity, int remainingAmount){
        type = toolType;
        id = paintId;
        name = paintName;
        size = paintSize;
        color =paintColor;
        cost = paintCost;
        capacity = canCapacity;
        amountLeft = remainingAmount;

        mPaint = new Paint();

        if(type == "paint"){
           mPaint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL));
        }
        /*if(flatTip){
            mPaint.setStrokeCap(Paint.Cap.SQUARE);  
        }else{
            mPaint.setStrokeCap(Paint.Cap.ROUND);
        }*/
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(size);



    }


    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(KNDrawingTool other) {
        Log.v("TOOL", "SOrting");
        return (color-other.color);
    }
}

1 个答案:

答案 0 :(得分:0)

您可能希望将RGB颜色值转换为HSL / HSV,然后按每个分量排序H = Hue,S =饱和度,(L =亮度| V =值)。我很快就会用代码更新这篇文章。

代码更新:

public class MainActivity extends Activity
{
    private static final int[] COLORS = {
        Color.BLACK, Color.MAGENTA, Color.RED,
        Color.BLUE, Color.DKGRAY, Color.YELLOW,
        Color.LTGRAY, Color.GREEN, Color.GRAY,
        Color.CYAN, Color.WHITE, 0xFFCC0000,
        0xFF00CC00, 0xFF0000CC, 0xFFCCCC00,
        0xFF00CCCC, 0xFFCC00CC,
    };

    private static final int[] FOREGROUNDS = {
        android.R.attr.textColorPrimary,
        android.R.attr.textColorPrimaryInverse,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ListView listView = new ListView(this);

        ArrayList<KNDrawingTool> tools = new ArrayList<KNDrawingTool>();
        for(int i = 0; i < COLORS.length; i++){
            tools.add(new KNDrawingTool(COLORS[i], "Color Entry #" + i));
        }
        Collections.sort(tools);

        final int foregrounds[] = new int[FOREGROUNDS.length];
        TypedArray taForegrounds = getTheme().obtainStyledAttributes(FOREGROUNDS);
        for(int i = 0; i < FOREGROUNDS.length; i++){
            foregrounds[i] = taForegrounds.getColor(i, Color.WHITE);
        }
        taForegrounds.recycle();

        listView.setAdapter(new ArrayAdapter<KNDrawingTool>(this, 0, tools)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                TextView text;
                if(convertView == null){
                    text = (TextView)LayoutInflater.from(parent.getContext())
                        .inflate(android.R.layout.simple_list_item_1, parent, false);
                }
                else{
                    text = (TextView)convertView;
                }
                KNDrawingTool item = getItem(position);
                text.setText(item.name);
                text.setBackgroundColor(item.color);
                if(item.hsv[2] > 0.5){
                    text.setTextColor(foregrounds[0]);
                }else{
                    text.setTextColor(foregrounds[1]);
                }
                return text;
            }
        });
        setContentView(listView);
    }


    private class KNDrawingTool implements Comparable<KNDrawingTool>
    {
        private int color;
        private String name;
        private float[] hsv = new float[3];

        public KNDrawingTool(int color, String name)
        {
            this.color = color;
            this.name  = name;
            Color.colorToHSV(color, hsv);
        }

        @Override
        public int compareTo(KNDrawingTool another)
        {
            for(int i = 0; i < 3; i++){
                if(hsv[i] != another.hsv[i]){
                    return (hsv[i] < another.hsv[i]) ? -1 : 1;
                }
            }
            return 0;
        }
    }
}