androidandroid-studiodrawing

How to check if `onDraw` is called in preview mode


In my custom-drawn view I use the Canvas.drawPaint(Paint) method. But the preview in Android Studio displays this message:

The graphics preview in the layout editor may not be accurate: 
    - Canvas.drawPaint is not supported

It's easy to replace drawPaint with drawRect, but the javadoc to drawPaint says: This is equivalent (but faster) to drawing an infinitely large rectangle with the specified paint.

My question is: can I check, if I'm drawing in preview mode, and conditionally use either method? Or some other workaround to make preview working and keep the performance?


Solution

  • You can use View's built-in isInEditMode() function as documented here: http://developer.android.com/reference/android/view/View.html#isInEditMode%28%29

    You can also find other methods in this thread: Custom Android Views in Eclipse Visual Editor

    Good luck!