在视图上使用 Canvas 进行绘制,但未绘制任何内容
我是初学者。我遇到问题了。 这是关于视图上画布的示例。
应该可以看到一个圆圈和上面的文字。
(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
行之后
1. 将
invalidate();
放在canvas.drawTextOnPath(text, Circle, 0, 20, tPaint);
2 . 尽量不要在构造函数中设置Background,看看它是否会绘制。1. Put
invalidate();
after the linecanvas.drawTextOnPath(text, circle, 0, 20, tPaint);
2. Try not to set Background in the constructor and see if it draws.
我在《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 tocPaint = new Paint(); cPaint.setColor(Color.WHITE);
and it worked.这只是因为您在 Paint Constructor *cPaint = new Paint(VALUES) * 中传递值,它返回 null!当您在画布函数中传递 null 作为绘制对象时,它会为您提供默认颜色(黑色)。初始化绘画对象的正确方法如下......
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...