java - 如何使用canvas绘制运行时图以延迟在android中绘制背景

标签 java android android-studio view android-canvas

我搜索了很多示例代码,但找不到任何实时运行图。我尝试创建示例图,但我的代码出现问题。在创建 Canvas 时,我只能用 2 个点进行绘制,但我需要用一系列点进行绘制。所以我创建了一个 Points 数组。但是代码无法正常工作。

这是我的 View 类:

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();
}}

Activity

public class LineView extends Activity {

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

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"/>

`

我的输出: enter image description here

最佳答案

    @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();

}

关于java - 如何使用canvas绘制运行时图以延迟在android中绘制背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855431/

相关文章:

java - Facebook 图形 API 更改并且现在需要访问 key ,如何编写新的 URL

java - 迭代器不循环

尽管我删除了存储库,但 Android Studio 项目说项目已经在 GitHub 上了。

android - 添加西类牙语拼写检查android studio

android - 实现 edittext 的可绘制右键单击事件时出现问题

java - 如何在 Java 中播放 3gp 文件?

java - 从字符串中提取整数

java - Apache Tomcat 是否阻止了客户端 IP 地址?

Java 泛型 : cant call a function with said generics even though type matches

android - 渲染期间引发异常 : Unable to find the layout for Action Bar