【问题标题】:onDraw not called in custom viewonDraw 未在自定义视图中调用
【发布时间】:2014-03-26 18:50:22
【问题描述】:

我想在 LinearLayout 中使用我的自定义视图,但从未调用过 onDraw()。 我已经阅读了几篇文章,但没有找到描述从服务更新视图的解决方案。

我的服务控制带有附加可视化器的媒体播放器。 Androids 可视化器将字节数组中的更新报告给我正在调用 invalidate() 并且我希望 onDraw() 被调用的自定义视图,但这永远不会发生。

我正在使用 Fragment 启动媒体播放器服务并获取我的自定义视觉视图的实例并以这种方式进行更新:

媒体播放器服务:

 LayoutInflater layoutInflater = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);
 View layout = layoutInflater.inflate(R.layout.player, null);
 this.visualizerView = (VisualizerView) layout.findViewById(R.id.visualizerView);

                                   ...

 this.visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
 public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                                                      int samplingRate) {
                        visualizerView.updateVisualizer(bytes);
 }

 public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
                    }
  }, Visualizer.getMaxCaptureRate() / 2, true, false);

自定义视图类:

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

    public void setValues(float[] values){

    }

    private void init() {
        this.setWillNotDraw(false);
        mBytes = null;
    }

    public void updateVisualizer(byte[] bytes) {
        mBytes = bytes;
        invalidate();
    }

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

【问题讨论】:

  • 你为什么要在服务中夸大你的布局???尝试在它所属的 Fragment 中执行它。

标签: android


【解决方案1】:

Services 不支持可视化。它们用于长时间运行的后台操作。

您应该将一个活动组件附加到您的应用程序,然后使用它的 setContentView 附加您的视图/布局。 然后,您可以使用意图在服务和活动之间进行通信。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2016-09-10
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多