简体   繁体   English

水平绘制多个圆android

[英]Draw Multiple Circles horizontally android

How to draw multiple circles horizontally in android, with some filled color. 如何在Android中水平绘制多个填充颜色的圆形。
And I want to change the color based on some server response. 我想根据一些服务器响应来更改颜色。

Can anybody tell how to do this? 谁能告诉我该怎么做? I implemented like this to draw a circle. 我这样实现画一个圆。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));

    }
    public class SampleView extends View
    {
        public SampleView(Context context)
        {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
             Paint mPaint = new Paint();
             mPaint.setColor(Color.RED);
             mPaint.setStyle(Paint.Style.FILL);
             canvas.drawCircle(30, 30, 10, mPaint);

        }
     }

I'm not able to get how to draw three more circles in the same line. 我不知道如何在同一条直线上再画三个圆。
And how to change the color based on condition if I write like this. 以及如何根据情况更改颜色(如果我这样写的话)。

You can choose two option, either have multiple view, each having a single circle or draw multiple circle in a single view. 您可以选择两个选项,要么具有多个视图,每个视图具有一个圆圈,要么在单个视图中绘制多个圆圈。 I'd prefer the first option, but since your need may vary, so I am explaining both the option. 我更喜欢第一种选择,但是由于您的需求可能会有所不同,因此我将同时解释这两种选择。

Option 1: 选项1:
Have a class variable named as color, which can work as a property where you can change the color based on default color of the view. 有一个名为color的类变量,可以用作属性,在其中您可以根据视图的默认颜色更改颜色。 Run a loop outside the view, may be in onCreate() to increament x coordinate, which can be passed to the drawCircle() method. 在视图外运行循环,可能在onCreate()中使x坐标保持不变,可以将其传递给drawCircle()方法。

Option 2 选项2
You can have an ArrayList of color for every circle. 每个圆都可以有一个颜色的ArrayList。 Run a loop in onDraw() method, which changes the color and increment the x coordinate based on loop counter. onDraw()方法中运行一个循环,该循环根据循环计数器更改颜色并增加x坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM