[英]Draw a tree (literally) using Circles in Android - Not a Tree View Structure
I have been struggling to draw a tree shaped structure in Android. 我一直在努力在Android中绘制树形结构。 I need to draw a tree shaped structure with branches at the bottom alone.
我需要画一个树形结构,底部只有树枝。 The actual leaves of the tree will be represented in the form of circles.
树的实际叶子将以圆圈的形式表示。 I have already started to draw circles by getting the width and height of the screen.
我已经开始通过获取屏幕的宽度和高度来绘制圆圈。 My code is as follows:
我的代码如下:
Initially I've got the width and height of the screen: 最初我有屏幕的宽度和高度:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
System.out.println("Width is " + width + "Height is " + height);
scrWidth = width;
scrHeight = height;
And then I have created a Paint object with the same colors and iterated it through a for
loop to draw circle multiple times: 然后我创建了一个具有相同颜色的Paint对象,并通过
for
循环迭代它以多次绘制圆形:
class Panel extends View {
public Panel(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
paint.setStyle(styles[0]);
}
@Override
public void onDraw(Canvas canvas) {
setWillNotDraw(false);
for (int i = 0; i < name.length; i++) {
x = (int) ((scrWidth / 360.0) * (90 + longitude[i]));
y = (int) ((scrHeight / 180.0) * (90 - latitude[i]));
paint.setStyle(styles[0]);
canvas.drawCircle(x, y, 25, paint);
invalidate();
System.out.println(x + "x" + name[i]);
System.out.println(y + "y" + name[i]);
}
}
I have actually hard coded the values for lat and long as follows : 我实际上已经对lat和long的值进行了硬编码,如下所示:
Double longitude[] = new Double[]{0.0,62.2,122.2};
Double latitude[] = new Double[]{40.2, 40.2, 40.2};
I dont want to hard-code the values of the lat and long but I am not sure what kind of mathematical calculations are required to find that dynamically so I am completely in dark. 我不想硬编码lat和long的值,但我不确定需要什么样的数学计算才能动态地找到它,所以我完全处于黑暗状态。 Would be great if someone helps me out !!!
如果有人帮助我,那会很棒! Any references that would help me pave the way to the final piece is most welcomed because I know that its a bit hard to find libraries or readymade codes for these kinda things.
任何可以帮助我为最终作品铺平道路的参考文献都是最受欢迎的,因为我知道有点难以找到这些类型的库或现成的代码。
PS : I am actually not sure if OpenGL would help me out in these kinda things but I would love to get my hands on it so any reference to that is also welcomed. PS:我实际上不确定OpenGL是否能帮助我解决这些问题,但我很乐意抓住它,所以对此也提出任何参考。
Below depicted is a picture of what I actually I want : 下面描绘的是我真正想要的图片:
I ended up creating the above tree using Canvas
and Path
class. 我最终使用
Canvas
和Path
类创建了上面的树。 Canvas for the leaves and the Path for the branches. 叶子的画布和树枝的路径。 It was a tedious and time consuming and I advise others not to take this route as it takes ever long to load cause its trying to do too much work with Canvas and I had memory issues frequently.
这是一个单调乏味和耗时的工作,我建议其他人不要采取这条路线,因为它需要很长时间才能加载,因为它试图用Canvas做太多工作,而且我经常遇到内存问题。 I still face them.
我还是面对他们。 Hope it helps someone.
希望它可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.