nukleos
nukleos

Reputation: 419

onDraw not called in custom view

I want to use my custom view in a LinearLayout, but onDraw() is never called. 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.

I'm using Fragment to start media player service and get instance of my custom visual view and doing updates this way:

MediaPlayer Service:

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

Upvotes: 0

Views: 322

Answers (1)

Steven De Bock
Steven De Bock

Reputation: 429

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. You can then use intents for the communication between service and activity.

Upvotes: 1

Related Questions