【问题标题】:GraphView not refreshing unless view is touched?除非触摸视图,否则 GraphView 不会刷新?
【发布时间】:2026-01-18 23:05:01
【问题描述】:

我正在使用jjoe64's GraphView project,但我在动态添加数据时遇到了问题。我正在尝试在折线图上绘制麦克风音量,但该图拒绝更新。这是我所拥有的:

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

    LinearLayout layout = (LinearLayout) findViewById(R.id.GraphLayout);

    graph = new LineGraphView(this, "Sounds");
    data = new GraphViewSeries(new GraphView.GraphViewData[] { new GraphView.GraphViewData(X++, 0) });
    graph.addSeries(data);
    graph.setManualYAxis(true);
    graph.setManualYAxisBounds(32767, 0);
    graph.setViewPort(3, 100);
    graph.setScrollable(true);
    layout.addView(graph);

    final MediaRecorder media = new MediaRecorder();
    media.setAudioSource(MediaRecorder.AudioSource.MIC);
    media.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    media.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    media.setOutputFile("/dev/null");
    try {
        media.prepare();
    } catch (Throwable t) {}
    media.start();

    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            final int amp = media.getMaxAmplitude();
            MaxSoFar = Math.max(amp, MaxSoFar);
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    data.appendData(new GraphView.GraphViewData(X++, amp), true);
                    //Here I try few different things
                    TextView output = (TextView)findViewById(R.id.output);
                    String info = MaxSoFar + "" + nl;
                    info += amp + "";
                    output.setText(info);
                }
            });
        }
    }, 0, 100);
}

我在data.appendData(...) 之后立即在线上进行了很多实验,例如graph.redrawAll();graph.invalidate();graph.postInvalidate();,但没有。如果我放下手指并拖动图表,我只能重新绘制图表。任何类型的触摸输入都会导致图形正确绘制。这可能与它在runOnUiThread 中有关,但我想不出为什么这可能是一个问题而不是一个要求。

我正在运行 Cyanogenmod 10.1 Nightlies (Android 4.2.2) 的 Galaxy S3 上对此进行调试。

【问题讨论】:

    标签: android graph android-graphview


    【解决方案1】:

    经过一些研究和实验,我发现有this issue,Lauszus 说他有this fix。我进行了这些更改并重建了 GraphView,它运行得非常好。

    【讨论】:

      最近更新 更多