簡體   English   中英

如何使用畫布繪制運行時圖形以延遲在android中繪制背景

[英]How to draw run time graph using canvas to paint the background in android with delay

我搜索了許多示例代碼,但找不到任何實時運行圖表。 我試圖創建一個示例圖,但我的代碼出了問題。 在創建畫布時,我只能畫2點,但我需要繪制一系列點。 所以我創建了一個Points數組。但代碼不能正常工作。

這是我的視圖類:

public class DemoView extends View {
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
int x = getHeight() / 2;
int i = 0;
//graph point
int[] a = {20, 50, 40, 65, 56, 43, 22, 23, 55, 77, 76,
        25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 150,
        150, 150, 150, 150, 150, 150, 20, 20, 20, 20, 20,
        20, 20, 150, 52, 52, 52, 52, 52, 52, 52, 52, 52,
        52, 52, 52, 52, 52, 52, 30, 30, 30, 30, 30, 30,
        30, 30, 30, 30, 30, 100, 100, 100, 100, 100, 100,
        100, 100, 100, 22, 22, 22, 22, 50, 50, 50};

public DemoView(Context context) {
    super(context);
    init();
}

public DemoView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DemoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();

}

public void init() {
    paint.setColor(Color.GREEN);
    paint1.setColor(Color.RED);
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    //Identification the graph current position
    canvas.drawRect(x, 0, x + 5, getHeight(), paint);
    if (i != 0) {
        //drawing the graph
        canvas.drawLine(x, (getHeight() / 2) - a[i], x + 1, (getHeight() / 2) - a[i - 1], paint1);
    }

    i++;
    // length of array value
    if (i == 79) {
        i = 0;
    }
    //repaint area
    if (x > getWidth() - 25) {
        if (x > getWidth() - 25) {
            x = 35;
        }
    }
    x += 2;
    invalidate();
}}

活動

public class LineView extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
   setContentView(R.layout.activity_line_view);
}}

視圖

  <LinearLayout android:orientation="vertical"
    android:layout_height="350dp"
    android:layout_width="fill_parent">

<com.akasmedical.android.linedrawing.DemoView
    android:layout_height="match_parent"
    android:layout_width="wrap_content"/>

`

我的輸出: 在此輸入圖像描述

    @Override
public synchronized void onDraw(final Canvas canvas) {

    int n = 35;
    int j = 1;

    canvas.drawRect(0, 0, getWidth(), getHeight(), paint);

    if (i != 0) {

        for (j = 1; j <= i; j++) {

            if (n > getWidth() - 25) {
                n = 36;
                k = n;
                //drawPulseIterator=k;
            } else {

                n += 1;

            }

            canvas.drawRect(k++, 0, n + 5, getHeight(), paint);
            canvas.drawLine(n, (getHeight() / 2) - a[j], n + 1, (getHeight() / 2) - a[j - 1], paint1);
        }
    }

    i++;
    if (j == a.length) {
        i = 0;
    }

    invalidate();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM