android:如何组合来自不同类的多个数组列表?

时间:2011-11-24 19:21:31

标签: android arraylist

我的mainActivity中有四个绘图功能。这是圆函数。

private class MyViewCircle extends View {

        public MyViewCircle(Context context) {
            super(context);
            attrCircle = new ArrayList<featureCircle>();
            random = new Random();
            attrCircle.add(new featureCircle(Color.BLACK, 80, 70, 70));
            attrCircle.add(new featureCircle(Color.BLUE, 20, 30, 80));
            attrCircle.add(new featureCircle(Color.CYAN, 80, 90, 90));
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            int randomPosition = random.nextInt(attrCircle.size());
            featureCircle randomAttrCircle = attrCircle.get(randomPosition);
            ***if (evol)*** {
                //combine all arraylist
            } else {
                paint.setColor(randomAttrCircle.color);
                canvas.drawCircle(randomAttrCircle.x, randomAttrCircle.y,
                        randomAttrCircle.radius, paint);
            }

        }

    }

这是Triangle功能 私有类MyViewTri扩展了View {

    public MyViewTri(Context context) {
        super(context);
        attrTri = new ArrayList<featureTri>();
        random = new Random();
        attrTri.add(new featureTri(Color.RED, 190, 210, 150, 230, 140, 270));
        attrTri.add(new featureTri(Color.MAGENTA, 100, 45, 190, 230, 114,
                167));
        attrTri.add(new featureTri(Color.YELLOW, 222, 111, 123, 156, 289,
                209));
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        int randomPosition = random.nextInt(attrTri.size());
        featureTri randomAttrTri = attrTri.get(randomPosition);
        paint.setColor(randomAttrTri.color);
        Path pathTri = new Path();
        pathTri.moveTo(randomAttrTri.x1, randomAttrTri.y1);
        pathTri.lineTo(randomAttrTri.x2, randomAttrTri.y2);
        pathTri.lineTo(randomAttrTri.x3, randomAttrTri.y3);
        pathTri.close();
        canvas.drawPath(pathTri, paint);
    }

}

我有其他值的类,比如

public class featureTri {
    int x1,y1,x2,y2,x3,y3;
    int color;
    public featureTri(int color, int x1, int y1, int x2, int y2, int x3, int y3){
        this.color = color;
        this.x1 = x1;
        this.y2 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
    }
}

我的目标是当“evol”变为TRUE时,所有的arraylists将组合在一起,所以我可以随机选择它们的值。有人能告诉我我该怎么做?

            attrCircle.add(new featureCircle(Color.BLACK, 80, 70, 70));
            attrCircle.add(new featureCircle(Color.BLUE, 20, 30, 80));
            attrCircle.add(new featureCircle(Color.CYAN, 80, 90, 90));
        attrTri.add(new featureTri(Color.RED, 190, 210, 150, 230, 140, 270));
        attrTri.add(new featureTri(Color.MAGENTA, 100, 45, 190, 230, 114,
                167));
        attrTri.add(new featureTri(Color.YELLOW, 222, 111, 123, 156, 289,
                209));

(我希望将所有颜色组合到一个新列表中并将它们全部随机化。)

1 个答案:

答案 0 :(得分:0)

使featureCircle和featureTri从处理int color;的新类featureColour扩展 - 然后您可以添加圆圈&amp; tris进入相同的ArrayList。