I am trying to display the triangle with nodes similar to this picture in my Android app
And I have only two drawables. First is of circle and second one is of line. I have searched a lot on internet but not getting any solution to which layout I should use and how I can achieve display this triangle on Android.
I cannot take an entire image of this triangle as in the place of nodes that would be some dynamic data displayed at run time.
Use a custom view with the help of Android Canvas
and its methods drawLines()
and drawCircle()
Here is how to create Custom Views and here is a very nice, short tutorial on how to use these methods to draw any shape.
Sample
// 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..
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.