簡體   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