简体   繁体   English

在自定义视图中未调用onDraw

[英]onDraw not called in custom view

I want to use my custom view in a LinearLayout, but onDraw() is never called. 我想在LinearLayout中使用自定义视图,但是从未调用过onDraw()。 I've read several posts but didn't found a solution which describes updating view from a service. 我已经阅读了几篇文章,但是没有找到描述从服务中更新视图的解决方案。

my service controls a mediaplayer with attached visualizer. 我的服务通过附带的可视化工具控制媒体播放器。 Androids visualizer reports updates in byte array to a custom View where I'm calling invalidate() and I expect onDraw() gets called, but that never happens. Android的可视化工具将字节数组中的更新报告给自定义视图,在该视图中我调用invalidate(),并且我希望调用onDraw(),但是那永远不会发生。

I'm using Fragment to start media player service and get instance of my custom visual view and doing updates this way: 我正在使用Fragment启动媒体播放器服务并获取我的自定义视觉视图的实例,并通过以下方式进行更新:

MediaPlayer Service: MediaPlayer服务:

 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);

Custom View class: 自定义视图类:

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);
        ...
    }

Services don't support visualization. 服务不支持可视化。 They are used for long-running background operations. 它们用于长时间运行的后台操作。

You should be attaching an activity component to your application and then use it's setContentView to attach your view/layout. 您应该将活动组件附加到应用程序,然后使用它的setContentView附加视图/布局。 You can then use intents for the communication between service and activity. 然后,您可以将意图用于服务和活动之间的通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM