在Android中使用Canvas连续画线

时间:2014-09-07 10:36:02

标签: android android-layout android-activity

我正在使用画布连续画线。我将以相对布局绘制线条。对于静态线,我完成了。对于实际情况,我将从传感器接收数据。所以我不知道如何连续划线,如下图所示。你能告诉我一个算法吗?非常感谢。这是我的代码

//XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/canvas"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:background="#d3d3d3" >
    </RelativeLayout>

</RelativeLayout>

这是我的静态代码

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new HeartGraphView (this));
        setContentView(R.layout.activity_main);
        RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.canvas);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.FILL_PARENT); 
        HeartGraphView v = new HeartGraphView(this);
        v.setLayoutParams(lp);
        relativeLayout.addView(v);          
    }    
public class HeartGraphView  extends View {
        public HeartGraphView (Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        int[] dataX=new int[30];
        int[]  dataY=new int[30];
        public int generatRandomPositiveNegitiveValue(int max , int min) {
            Random r = new Random();
            int ii = r.nextInt(max - min + 1) + min;
            return (ii-140);
        }

    int[] dataX=new int[30];        
    int[]  dataY=new int[30];
    public int generatRandomPositiveNegitiveValue(int max , int min) {
        Random r = new Random();
        int ii = r.nextInt(max - min + 1) + min;
        return (ii-140);
    }

    @Override 
    public void onDraw (Canvas canvas) {
        int w;
        int h;
        h=280;
        w=600;

        //Generate random
        int min = 0;
        int max = 280;

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(6);
        dataX[0]=0;
        for (int i = 0; i <  w/20 - 1; i++) {
        dataX[i+1]=(i+1)*w/20;
        dataY[w/20 - 1]=generatRandomPositiveNegitiveValue(max ,min);
        dataY[i]=dataY[i+1];
        }

        for (int i = 0; i <  w/20 -1; i++) {
            // apply some transformation on data in order to map it correctly
            // in the coordinates of the canvas
            canvas.drawLine(dataX[i], h/2-dataY[i], dataX[i+1], h/2-dataY[i+1],paint);
            canvas.drawColor(Color.TRANSPARENT, Mode.MULTIPLY);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            invalidate();              
        }        

    }
}

} 这是我的结果

enter image description here

编辑&gt; 作为Frank N. Stei的建议。我修改一些东西: +首先,初始X轴的所有元素,固定值[0,30,60,90 ....]和Y轴为零

    int w=600;
    for (int i = 0; i <  w/20 - 1; i++) {
    dataX[i]=i*w/20;
    dataY[i]=0;
    }

+第二,当一个新值到达Y时,我们将它放在数组数据的末尾并删除第一个位置:

 for (int i = 0; i <  w/20 - 1; i++) {
      dataY[i]=dataY[i+1];

    }
  dataY[w/20 - 1]=newValue;

+第三,重绘dataX和dataY 是否正确???

0 个答案:

没有答案