2013-05-13 3 views
0

Im GraphView 라이브러리를 사용하여 실시간 그래프를 시각화합니다. 라이브러리 예제는 2.X 안드로이드 장치에서 잘 작동하지만 4.X 장치에서는 제대로 작동하지 않습니다. 화면을 클릭하면 그래프가 새로 고쳐집니다. 안드로이드에서 나는 초보자지만 문제는 runnable 및 postDelayed의 사용이 될 수 있다고 생각합니다. android 4.0 (실시간 GraphView 라이브러리)에서 실행 가능 및 지연 됨

는 예 코드 :

package com.example.realtime; 

import com.jjoe64.graphview.BarGraphView; 
import com.jjoe64.graphview.GraphView; 
import com.jjoe64.graphview.GraphView.GraphViewData; 
import com.jjoe64.graphview.GraphView.LegendAlign; 
import com.jjoe64.graphview.GraphViewSeries; 
import com.jjoe64.graphview.LineGraphView; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.LinearLayout; 




public class RealTimeGraph extends Activity { 
    private final Handler mHandler = new Handler(); 
    private Runnable mTimer1; 
    private Runnable mTimer2; 
    private GraphView graphView; 
    private GraphViewSeries exampleSeries1; 
    private GraphViewSeries exampleSeries2; 
    private double graph2LastXValue = 5d; 

private double getRandom() { 
    double high = 3; 
    double low = 0.5; 
    return Math.random() * (high - low) + low; 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.graphs); 

    // init example series data 
    exampleSeries1 = new GraphViewSeries(new GraphViewData[] { 
      new GraphViewData(1, 2.0d) 
      , new GraphViewData(2, 1.5d) 
      , new GraphViewData(2.5, 3.0d) // another frequency 
      , new GraphViewData(3, 2.5d) 
      , new GraphViewData(4, 1.0d) 
      , new GraphViewData(5, 3.0d) 
    }); 

    // graph with dynamically genereated horizontal and vertical labels 
    if (getIntent().getStringExtra("type").equals("bar")) { 
     graphView = new BarGraphView(
       this // context 
       , "GraphViewDemo" // heading 
     ); 
    } else { 
     graphView = new LineGraphView(
       this // context 
       , "GraphViewDemo" // heading 
     ); 
    } 
    graphView.addSeries(exampleSeries1); // data 

    LinearLayout layout = (LinearLayout) findViewById(R.id.graph1); 
    layout.addView(graphView); 

    // ---------- 
    exampleSeries2 = new GraphViewSeries(new GraphViewData[] { 
      new GraphViewData(1, 2.0d) 
      , new GraphViewData(2, 1.5d) 
      , new GraphViewData(2.5, 3.0d) // another frequency 
      , new GraphViewData(3, 2.5d) 
      , new GraphViewData(4, 1.0d) 
      , new GraphViewData(5, 3.0d) 
    }); 

    // graph with custom labels and drawBackground 
    if (getIntent().getStringExtra("type").equals("bar")) { 
     graphView = new BarGraphView(
       this 
       , "GraphViewDemo" 
     ); 
    } else { 
     graphView = new LineGraphView(
       this 
       , "GraphViewDemo" 
     ); 
     ((LineGraphView) graphView).setDrawBackground(true); 
    } 
    graphView.addSeries(exampleSeries2); // data 
    graphView.setViewPort(1, 4); 
    graphView.setScalable(true); 

    layout = (LinearLayout) findViewById(R.id.graph2); 
    layout.addView(graphView); 
} 

@Override 
protected void onPause() { 
    mHandler.removeCallbacks(mTimer1); 
    mHandler.removeCallbacks(mTimer2); 
    super.onPause(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    mTimer1 = new Runnable() { 
     @Override 
     public void run() { 
      exampleSeries1.resetData(new GraphViewData[] { 
        new GraphViewData(1, getRandom()) 
        , new GraphViewData(2, getRandom()) 
        , new GraphViewData(2.5, getRandom()) // another frequency 
        , new GraphViewData(3, getRandom()) 
        , new GraphViewData(4, getRandom()) 
        , new GraphViewData(5, getRandom()) 
      }); 
      mHandler.postDelayed(this, 300); 
     } 
    }; 
    mHandler.postDelayed(mTimer1, 300); 

    mTimer2 = new Runnable() { 
     @Override 
     public void run() { 
      graph2LastXValue += 1d; 
      exampleSeries2.appendData(new GraphViewData(graph2LastXValue, getRandom()), true); 
      mHandler.postDelayed(this, 1000); 
     } 
    }; 
    mHandler.postDelayed(mTimer2, 1000); 
} 

}`

중요한 코드는 함수 내부 OnResume.

이드 정말 사전에 ...

덕분에 어떤 힌트를 주셔서 감사합니다!

답변

0

나는 돈이 -

그러나, 업데이트 된 후에는()보기를 무효화하려고 ... 한 번 실행 가능한 내부에 한 번 실행 가능한을 정의한 후 t 당신이 두 번 핸들러에 posdelayed 전화를 왜 이해합니다. 나는 이런 식으로 할 것 :

@Override 
protected void onResume() { 
    super.onResume(); 
    mTimer1 = new Runnable() { 
     @Override 
     public void run() { 
      exampleSeries1.resetData(new GraphViewData[] { 
        new GraphViewData(1, getRandom()) 
        , new GraphViewData(2, getRandom()) 
        , new GraphViewData(2.5, getRandom()) // another frequency 
        , new GraphViewData(3, getRandom()) 
        , new GraphViewData(4, getRandom()) 
        , new GraphViewData(5, getRandom()) 
      }); 
      exampleSeries1.invalidate(); 
     } 
    }; 
    mHandler.postDelayed(mTimer1, 300); 

    mTimer2 = new Runnable() { 
     @Override 
     public void run() { 
      graph2LastXValue += 1d; 
      exampleSeries2.appendData(new GraphViewData(graph2LastXValue, getRandom()), true); 
      exampleSeries2.invalidate(); 
     } 
    }; 
    mHandler.postDelayed(mTimer2, 1000); 
} 

이() 메소드가 순환하고 계속 반복 것 때문에 실행 가능한 것입니다 실행 내부 postDelayed()에서 호출 해

+0

이유에 대해 작동하는지 알려줘 그 자체로 300ms 지연이 ... 실행 메소드에서 postDelayed 호출을 제거하면이 실행 파일은 한 번만 실행됩니다. – FoamyGuy

+0

그게 바로 이유 야! 다른 아이디어? – Rafag