在视图上使用 Canvas 进行绘制,但未绘制任何内容

发布于 2024-12-16 20:56:33 字数 1705 浏览 6 评论 0原文

我是初学者。我遇到问题了。 这是关于视图上画布的示例。

应该可以看到一个圆圈和上面的文字。

(http://goo.gl/6ZPvQ) 我的声誉不足以拍摄照片。

但什么也没发生。

这是我在其上绘制画布的视图。

public class TestCanvasActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGraphics(this));
    }
}

class MyGraphics extends View {
    private Paint cPaint,tPaint;
    private Path circle;
    private String text;

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        cPaint = new Paint(Color.GRAY);
        tPaint = new Paint(Color.BLACK);
        circle = new Path();
        text = "Welcome to Android!!";

        circle.addCircle(150, 150, 100, Direction.CW);
        canvas.drawPath(circle, cPaint);
        canvas.drawTextOnPath(text, circle, 0, 20, tPaint);
    }

    public MyGraphics(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setBackgroundColor(R.drawable.background);
    }

}

这是背景图像代码!!

背景.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFFFFF"
android:endColor="#808080"
android:angle="270" />
</shape>

谢谢!!

我犯了一个低级错误。我将 cPaint = new Paint(Color.GRAY) 更改为接下来的两行。有用!!

 cPaint = new Paint();
 cPaint.setColor(Color.WHITE);

还是谢谢大家了。

I'm a beginner. I got a problem.
This is an example about canvas on a view.

A circle and text on it are supposed to be seen.

(http://goo.gl/6ZPvQ) My reputation isn't enough to get a picture.

But Nothing happened.

This is the view I draw canvas on.

public class TestCanvasActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGraphics(this));
    }
}

class MyGraphics extends View {
    private Paint cPaint,tPaint;
    private Path circle;
    private String text;

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        cPaint = new Paint(Color.GRAY);
        tPaint = new Paint(Color.BLACK);
        circle = new Path();
        text = "Welcome to Android!!";

        circle.addCircle(150, 150, 100, Direction.CW);
        canvas.drawPath(circle, cPaint);
        canvas.drawTextOnPath(text, circle, 0, 20, tPaint);
    }

    public MyGraphics(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setBackgroundColor(R.drawable.background);
    }

}

This is the background image code!!

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFFFFF"
android:endColor="#808080"
android:angle="270" />
</shape>

Thank you!!

I Made a low level mistake. I change cPaint = new Paint(Color.GRAY) to the next two line. It works!!

 cPaint = new Paint();
 cPaint.setColor(Color.WHITE);

Thank you all the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

圈圈圆圆圈圈 2024-12-23 20:56:33

行之后

1.invalidate(); 放在 canvas.drawTextOnPath(text, Circle, 0, 20, tPaint); 2 . 尽量不要在构造函数中设置Background,看看它是否会绘制。

1. Put invalidate(); after the line canvas.drawTextOnPath(text, circle, 0, 20, tPaint);

2. Try not to set Background in the constructor and see if it draws.

孤独难免 2024-12-23 20:56:33

我在《Hello Android 3rd》中看到了一个例子。我发现 cPaint = new Paint(Color.GRAY); 是错误的。我将其更改为 cPaint = new Paint(); cPaint.setColor(Color.WHITE); 它起作用了。

I saw an example in 《Hello Android 3rd》. I found that cPaint = new Paint(Color.GRAY); is wrong. I changed it to cPaint = new Paint(); cPaint.setColor(Color.WHITE); and it worked.

梦幻的心爱 2024-12-23 20:56:33

这只是因为您在 Paint Constructor *cPaint = new Paint(VALUES) * 中传递值,它返回 null!当您在画布函数中传递 null 作为绘制对象时,它会为您提供默认颜色(黑色)。初始化绘画对象的正确方法如下......

    cPaint = new Paint();
    cPaint.setColor(Color.GRAY);
    tPaint = new Paint();
    tPaint.setColor(Color.BLACK);

Its just because you pass values in paint Construtor *cPaint = new Paint(VALUES) * it returns null! when you pass null in canvas functions as paint object, it gives u default color (BLACK). Correct way of initializing paint object is as below...

    cPaint = new Paint();
    cPaint.setColor(Color.GRAY);
    tPaint = new Paint();
    tPaint.setColor(Color.BLACK);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文