Search code examples
androidandroid-canvasandroid-viewondraw

Custom view Canvas onDraw() doesnt draw anything


I am trying to draw a oval using canvas, but it never gets drawn. Here is my code for the custom view. I have also used setWillNotDraw(false) still nothing gets drawn on the screen.

public class Myview extends View {
    Paint paint;
    RectF rect;
    public Myview(Context context) {
        super(context);
        init();
        setWillNotDraw(false);
    }

    public Myview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        setWillNotDraw(false);
    }

    public Myview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
        setWillNotDraw(false);
    }

    private void init() {
        rect = new RectF(0.1 f, 0.1 f, getWidth(), getHeight());
        paint = new Paint();
        paint.setShader(new LinearGradient(0.40 f, 0.0 f, 100.60 f, 100.0 f,
            Color.parseColor("#ffffff"),
            Color.parseColor("#Ffffff"),
            Shader.TileMode.CLAMP));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension(200, 200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawOval(rect, paint);
    }
}

Any suggestions?


Solution

  • The problem is getWidth() and getHeight() is O. Change that to your requirement.

    You can use the below as a reference.

    public class MainActivity extends Activity
    {    
    
    MyView mv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mv= new MyView(this);
    setContentView(mv);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);
    mPaint.setShader(new LinearGradient(0.40f, 0.0f, 100.60f, 100.0f, 
          Color.RED,
          Color.RED,
          Shader.TileMode.CLAMP));
    
    }
    
    private Paint       mPaint;
    
    public class MyView extends View{
      Paint paint;
      RectF rect;
       public MyView(Context context) {
              super(context);
              rect = new RectF(20, 20, 100,100);
              //canvas.drawOval(new RectF(50, 50, 20, 40), p)
       }
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
          setMeasuredDimension(200, 200);
    
      }
    
    
    
      @Override
          protected void onDraw(Canvas canvas) {
              super.onDraw(canvas);
              canvas.drawOval(rect, mPaint);
    
          }
      }
    }
    

    Change the co-ordinates and color to your requirements. The above draws a circle but you can change the co-ordinates to draw oval something like canvas.drawOval(new RectF(50, 50, 20, 40), mPaint);

    enter image description here