0

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

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

正如你所看到的,我有一个 compareTo 方法,它只返回 int1-int2 (其中 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);
    }
}
4

1 回答 1

0

您可能希望将 RGB 颜色值转换为 HSL/HSV,然后按 H=Hue、S=Saturation、(L=Lightness | V=Value) 的每个分量进行排序。我会尽快用代码更新这篇文章。

代码更新:

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;
        }
    }
}
于 2013-06-21T22:44:52.410 回答