简体   繁体   English

画布未在Java Android中绘制

[英]canvas not drawing in java android

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

The uppercase OnDraw() method you specified will not be called that way. 您指定的大写OnDraw()方法不会那样调用。 You actually need to override the onDraw() method when using a custom view, eg: 使用自定义视图时,实际上需要重写onDraw()方法,例如:

@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. 请参阅此处有关“自定义工程图”的文档,特别是“ 覆盖onDraw()”部分。

have you tried to add, in your constructor redPaint.setStyle(...) for example: 您是否尝试过在构造函数redPaint.setStyle(...)中添加例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM