繁体   English   中英

如何创建布局?

[英]How to create a layout?

我正在尝试在我的Android应用程序中显示带有类似于此图片的节点的三角形

在此处输入图片说明

而且我只有两个可绘制对象。 第一个是圆,第二个是线。 我在互联网上进行了很多搜索,但没有得到解决方案,我应该使用哪种布局以及如何实现在Android上显示此三角形。

我无法拍摄该三角形的完整图像,就像在运行时显示的动态数据的节点位置那样。

借助Android Canvas及其方法drawLines()drawCircle()使用自定义视图

这是创建自定义视图的方法 ,这是一个非常不错的简短教程 ,介绍如何使用这些方法绘制任何形状。

样品

// somewhere in the constructor, call this
private void init() {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.STROKE);
    // mPaint.setStrokeWidth(SOME_VALUE);
    ...
}

// override onSizeChanged() to make your measurements.
// if you need 'finer' control, override onMeasure(), but be careful with that one
// (read its javadocs before you override it).
// measurements include shape and text locations and sizes, etc.
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mTriangleVertices[0] = 0.2  * w; // x0
    mTriangleVertices[1] = 0.15 * h; // y0

    mTriangleVertices[2] = 0.5  * w; // x1
    mTriangleVertices[3] = 0.85 * h; // y1

    mTriangleVertices[4] = 0.8  * w; // x2
    mTriangleVertices[5] = 0.15 * h; // y2
    // other calculations...
}

// after you save your measurements to some fields, override onDraw().
// use all the tools you created and the info you gathers above here.
// avoid creating objects at all cost. read the docs for more info.
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); // include this first, last or you can even omit it sometimes

    canvas.drawLines(mTriangleVertices, 0, mTriangleVertices.length, mPaint);

    // canvas.drawCircle(); canvas.drawOval(); etc..
}

暂无
暂无

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

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