canvas not drawing in java android

て烟熏妆下的殇ゞ 提交于 2019-12-24 03:04:25

问题


I think the code is right, but I'm unable to find what's going wrong. Can anyone help please in figuring out what's incorrect ?

Thats the activity document:

package com.example.crazyeights;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class CrazyEightsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView view = new MyView(this);
        setContentView(view);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.crazy_eights, menu);
        return true;
    }

}

thats what i want to display in the screen:

package com.example.crazyeights;
import android.content.Context; 
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class MyView extends View{

    private Paint redPaint;
    private int circleX;
    private int circleY;
    private float radius;

    public MyView(Context context) {
        super(context);
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.RED);
        circleX = 100;
        circleY = 100;
        radius = 30;
    }
    protected void OnDraw(Canvas canvas){
             canvas.drawCircle(circleX, circleY, radius, redPaint);
    }
}

回答1:


The uppercase OnDraw() method you specified will not be called that way. You actually need to override the onDraw() method when using a custom view, e.g:

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawCircle(circleX, circleY, radius, redPaint);
}

I tested it and that draws a red circle. See documentation on Custom Drawing here, specifically the Override onDraw() section.




回答2:


have you tried to add, in your constructor redPaint.setStyle(...) for example:

redPaint.setStyle(Paint.Style.FILL) redpaint.setStyle(Paint.Style.STROKE);



来源:https://stackoverflow.com/questions/20556225/canvas-not-drawing-in-java-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!