SurfaceView越来越慢

时间:2017-02-10 14:57:22

标签: java android performance

我试图在SurfaceView上绘制一个信号,但我注意到它每秒都变得越来越慢。我认为这是因为我画了一遍又一遍地清理它。

我想绘制一个我将从USB端口接收的ECG信号,SurfaceView是一种很好的绘制方式,还是让我使用TextureView或其他东西?

如果有人可以指导我 感谢

这是我的代码:

public class signal extends AppCompatActivity {
OurView v;
Paint p;
Paint p2;
Paint pr;
float pstart = 0;
float pstop = 5;
int pos = 10;
int index_ecg = 0;
int index_pulso = 0;
int average = 0;
int average_pulso = 0;
long time_start = 0;
long time_stop = 0;

int tab_ecg[] = {-83,-84,-82,-80,-81,-83,-86,-86,-85,-85,-83,-80,-79,-80,-81,-82,-77,-76,-76,-74,-73,-75,-75,-77,-76,-74,-73,-71,-67,-65,-66,-66,-65,-63,-62,-61,-59,-60,-64,-67,-69,-68,-67,-66,-64,-63,-62,-62,-64,-63,-64,-67,-71,-73,-76,-77,-79,-81,-81,-81,-80,-79,-80,-82,-83,-86,-85,-85,-86,-84,-85,-86,-87,-89,-88,-88,-88,-87,-85,-86,-87,-90,-90,-89,-90,-90,-87,-87,-90,-93,-93,-89,-86,-84,-81,-80,-85,-90,-95,-94,-92,-87,-74,-57,-42,-23,-4,14,22,12,-3,-18,-34,-51,-69,-82,-90,-98,-108,-118,-125,-127,-123,-120,-115,-109,-104,-95,-88,-86,-89,-92,-100,-105,-105,-102,-98,-97,-99,-100,-101,-99,-97,-95,-93,-94,-95,-98,-101,-99,-97,-96,-92,-91,-92,-94,-96,-94,-93,-90,-87,-83,-83,-85,-87,-92,-92,-90,-90,-86,-85,-88,-91,-95,-93,-91,-89,-87,-84,-84,-86,-87,-86,-85,-82,-79,-76,-75,-77,-79,-79,-75,-72,-70,-66,-66,-67,-68,-69,-67,-65,-64,-61,-61,-63,-66,-65,-63,-58,-56,-57,-60,-61,-60,-53,-53,-58,-60,-55,-54,-51,-53,-64,-68,-72,-71,-72,-74,-79,-78,-76,-75,-79,-82,-86,-86,-81,-80,-84,-87,-86,-85,-82,-83,-87,-88,-85,-85,-82,-85,-89,-88,-86,-84,-85,-89,-92,-90,-88,-85,-85,-87,-84,-83,-78,-78,-77,-74,-60,-52,-28,-18,1,15,40,50,72,78,86,87,96,102,99,93,84,82,85,85,82,77,82,87,96,96,92,90,88,88,88,87,82,77,52,42,16,-1,-31,-42,-58,-66,-79,-84,-88,-91,-92,-92,-99,-103,-104,-104,-103,-107,-116,-117,-119,-120,-123,-127,-132,-131,-131,-132,-144,-147,-149,-150,-147,-147,-155,-160,-162,-163,-164,-168,-173,-174,-174,-174,-175,-178,-182,-181,-180,-178,-177,-178,-178,-177,-174,-171,-171,-170,-164,-163,-154,-153,-153,-152,-144,-138,-129,-127,-125,-123,-118,-113,-109,-110,-109,-107,-102,-99,-99,-100,-94,-93,-91,-91,-97,-99,-98,-96,-92,-93,-98,-98,-96,-93,-90,-91,-95,-93,-91,-87,-89,-90,-91,-90,-87,-87,-88,-90,-88,-86,-81,-80,-85,-87,-89,-90,-87,-89,-93,-87,-84,-83,-85,-88,-87,-84,-83,-83,-85,-85,-85,-81,-81,-84,-85,-82,-80,-77,-78,-86,-86,-87,-86,-85,-87,-89,-88,-86,-84,-81,-83,-84,-84,-82,-83,-85,-88,-87,-87,-84,-82,-82,-84,-85,-86,-85,-84,-82,-79,-79,-81,-84,-87,-87,-86,-85,-82,-82,-84,-87,-89,-88,-87,-86,-82,-79,-78,-80,-81,-82,-83,-83,-83,-82,-83,-87,-88,-89,-87,-85,-84,-81,-81,-82,-84,-86,-85,-85,-84,-81,-80,-81,-83,-85,-85,-84,-84,-82,-81,-82,-84,-84,-84,-83,-83,-81,-77,-78,-81,-86,-89,-87,-85,-84,-82,-80,-81,-83,-85,-84,-83,-82,-80,-77,-77,-80,-83,-85,-84,-83,-82,-80,-79,-81,-81,-82,-81,-80,-79,-76,-74,-74,-75,-75,-74,-73,-73,-71,-66,-65,-66,-67,-66,-62,-60,-59,-57,-58,-62,-65,-67,-66,-65,-65,-63,-61,-62,-63,-66,-67,-66,-68,-67,-66,-66,-69,-70,-72,-72,-74,-75,-76,-77,-81,-84,-86,-83,-82,-81,-79,-80,-83,-84,-86,-85,-85,-86,-85,-83,-83,-85,-87,-88,-88,-89,-88,-86,-84,-87,-88,-89,-86,-82,-78,-75,-75,-76,-80,-87,-91,-93,-92,-88,-78,-63,-50,-31,-8,14,28,23,12,-3,-22,-40,-57,-69,-78,-86,-96,-107,-119,-123,-120,-116,-111,-107,-102,-97,-93,-93,-94,-94,-94,-95,-93,-91,-90,-90,-91,-93,-92,-92,-90,-87,-88,-90,-93,-95,-94,-94,-94,-91,-87,-86,-88,-90,-89,-87,-87,-85,-83,-84,-86,-88,-89,-88,-87,-86,-82,-81,-83,-85,-86,-84,-82,-82,-79,-76,-75,-77,-80,-80,-77,-76,-75,-71,-71,-73,-73,-74,-71,-69,-67,-64,-63,-63,-64,-65,-63,-61,-60,-59,-57,-57,-59,-61,-60,-58,-55,-53,-50,-50,-50,-52,-54,-53,-52,-50,-48,-47,-49,-51,-53,-53,-53,-53,-52,-50,-52,-55,-58,-59};

int tab_moy[] = {0,0,0,0};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    v = new OurView(this, (SurfaceView) findViewById(R.id.surfaceView));

    p = new Paint();
    p.setStrokeWidth(5);
    p.setColor(Color.GREEN);

    pr = new Paint();
    pr.setStyle(Paint.Style.FILL);
    pr.setColor(Color.BLACK);

}

@Override
protected void onResume() {
    super.onResume();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(true){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (index_ecg+1 >= tab_ecg.length) index_ecg = 0;
                        if (index_pulso+1 >= tab_pulso.length) index_pulso = 0;

                        average += tab_ecg[index_ecg]+200;
                        if(pstart == 0) time_start = System.currentTimeMillis();
                        index_ecg++;

                        if(index_ecg%4 == 0){
                            tab_moy[0] = tab_moy[1];
                            tab_moy[1] = average/4;
                            average = 0;

                            tab_moy[2] = tab_moy[3];
                            tab_moy[3] = tab_pulso[index_pulso];
                            index_pulso++;
                            average_pulso = 0;
                            v.Draw();
                        }
                    }
                });

            }
        }
    }).start();
}

public class OurView extends SurfaceView  {
    SurfaceHolder holder;
    SurfaceView surfaceView;
    boolean isItOK;
    Rect dirty  = new Rect();
    Canvas c;

    public OurView(Context context, SurfaceView sv) {
        super(context);
        this.surfaceView = sv;
        holder = surfaceView.getHolder();
    }

    public void pause() {
        isItOK = false;
    }

    public void resume() {
        isItOK = true;
    }

    public void Draw() {

        dirty.set((int) pstart, 0, (int) pstop, 700);
        c = holder.lockCanvas(dirty);
        c.drawRect(dirty, pr);

        c.drawLine(pstart, tab_moy[0], pstop, tab_moy[1], p);

        pstart += 4;
        pstop = pstart + 4;

        if (pstart > 1500) {
            pstart = 0;
            pstop = 4;
            time_stop = System.currentTimeMillis();
            Log.d("timer : ",Long.toString(time_stop-time_start));
        }

        holder.unlockCanvasAndPost(c);
    }
}}

0 个答案:

没有答案