Canvas does not draw in Custom View
NickName:iTurki Ask DateTime:2012-09-04T18:20:55

Canvas does not draw in Custom View

I created a Custom View CircleView like this:

public class CircleView extends LinearLayout {

    Paint paint1;
    public CircleView(Context context) {
        super(context);
        init();
    }   
    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public void init() {
        paint1 = new Paint();
        paint1.setColor(Color.RED); 
    }       
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);         
        canvas.drawCircle(50, 50, 25, paint1);
        this.draw(canvas);  
    }
}

Then I included it on my Activity's layout root <RelativeLayout>:

  <com.turkidroid.test.CircleView
      android:id="@+id/circle_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:layout_centerInParent="true"  />  

However, nothing was drawn!

  • Am I implementing the Custom View right?
  • Or is it how I used the Custom View?

Some Info:

  • Both CircleView and MyActivity are in the same package: com.turkidroid.test.
  • In onDraw() method, I tried including super.onDraw() and commenting it.
  • I know I can draw a circle with much simpler approaches but my CircleView will contain more than drawing a circle. I need to make it a Custom View.

Copyright Notice:Content Author:「iTurki」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12261435/canvas-does-not-draw-in-custom-view

Answers
Aitor Calderon 2012-09-04T10:41:38

Your onDraw method is never called, you need to call setWillNotDraw(false) on the constructor of your Custom View in order to get onDraw actually called.\nAs stated on Android SDK:\n\nIf this view doesn't do any drawing on its own, set this flag to allow\nfurther optimizations. By default, this flag is not set on View, but\ncould be set on some View subclasses such as ViewGroup. Typically, if\nyou override onDraw(android.graphics.Canvas) you should clear this\nflag.\n",


More about “Canvas does not draw in Custom View” related questions

Canvas does not draw in Custom View

I created a Custom View CircleView like this: public class CircleView extends LinearLayout { Paint paint1; public CircleView(Context context) { super(context); init(); ...

Show Detail

Android: Custom view does draw

So I know there are a lot of questions like this, but none of the answers have had any effect. I have a custom View that I am trying to put into a ScrollView (all programmatically). The ScrollView

Show Detail

Cannot draw on canvas with custom view

I have a class GameActivityas follows (I just post the relevant part): public class GameActivity extends AppCompatActivity { // initiate variables private GameView mGameView; private Display mDis...

Show Detail

Can we use and draw view inside custom view using canvas on Android?

I would like to know if it possible to use and draw a view such as a TextView or Chronometer inside my custom View which uses the canvas to perform drawing. My goal is to reuse the Chonometer view ...

Show Detail

How to draw Custom View Extending Relative Layout to a canvas

I have my main activity which creates an instance of SurfaceView. In the SurfaceView I am creating several simple objects from bitmaps. I pass a canvas element to them (objectX.draw(canvas)...etc) ...

Show Detail

How to draw drawable on canvas custom view in android

I'm working on audio recording app.During recording i'm using visualizer like normal audio recording apps.But on specific time a want to draw a drawable on canvas visualizer(that is custom class ex...

Show Detail

Draw custom view inside custom view

I am working on a custom view, TreeView, which would display a tree given a root node. I am using the Kotlin language. Here's what part of it looks like at the moment: override fun onDraw(canvas:

Show Detail

Bounce animation for custom view drawn with canvas

I am drawing different view in a custom view with canvas, and want to add custom animation to on of the view (infinite bounce) not sure how this can be achieved. Would appreciate any suggestions. H...

Show Detail

invalidate() on custom View does not cause onDraw(Canvas c) to be called

For my activity i use 3 custom views stacked. the lower one is a SurfaceView fullscreen, the middle one is derived from GridView and overrides onDraw to add custom graphic. The top one is derived

Show Detail

How to draw polygon with click listener on canvas using custom view?

Please give me any suggestions or need your help for set click listener of polygon, I have implemented below code for draw polygon using custom view. public class CustomView extends View { ...

Show Detail