android - 在 MPAndroidChart 中绘制图形完成时如何接收回调?

标签 android multithreading mpandroidchart

我在通过更新数据和调用 mMyChart.invalidate() 来实时刷新 LineDataSet 图形时遇到问题。

一个线程从服务器请求数据,处理数据,然后通过消息将其发送到第二个线程,第二个线程是主要 Activity 线程,负责绘制图形。

问题是这个无效调用似乎在图表实际重绘之前完成,所以我请求数据的另一个线程关闭并请求更多数据并将其发送到线程以重新绘制图表。绘制图表的线程在一段时间后变得非常落后,开始花费很长时间来响应任何事情,它运行的时间越长,花费的时间就越长。

我的问题很直接,因为我想做到这一点,所以在绘图完成之前我不会继续请求数据。绘制完成后如何接收回调?或者,有没有办法检测它何时在绘制?

谢谢,

埃里克

最佳答案

你可以试试 ViewTreeObserver (相关问题here)

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to getViewTreeObserver() for more information.

或者您可以尝试扩展您正在使用的特定 Chart 类型并覆盖 onDraw(Canvas canvas) 以在绘制过程完成时调用监听器:

public class ExtendedBarChart extends BarChart {
    public ExtendedBarChart(Context context) {
        super(context);
    }

    public ExtendedBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExtendedBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // TODO: Drawing completed, execute callback...
    }
}

关于android - 在 MPAndroidChart 中绘制图形完成时如何接收回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675609/

相关文章:

3d 中的 Android ViewGroup 旋转

android - 如何使用 glide 库在 recyclerview 中加载更新图像而不是旧图像

java - 如何在 Java 中指定应应用注释的字段类型?

c# - BlockingCollection 在添加项目时会阻塞吗?如果是这样,为什么?

android - MPAndroid 条形图仅在点击图表区域后显示图形,否则显示 'No chart data available'

android - 更改背景水平和垂直网格线的颜色

android - 如何将 Android Studio 项目添加到 GitHub

Java:停止没有同步的线程

c++ - 为什么多线程中的 Local/Stack 变量不需要锁定

android - MPChart 条形图 : draw x axis line on zero value