[英]Draw with a Canvas on a view, but nothing painted
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. ( http://goo.gl/6ZPvQ )我的声誉不足以获取图片。
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 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. 我将cPaint = new Paint(Color.GRAY)更改为下两行。 It works!! 有用!!
cPaint = new Paint();
cPaint.setColor(Color.WHITE);
Thank you all the same. 谢谢你们
I saw an example in 《Hello Android 3rd》. 我在《 Hello Android 3rd》中看到了一个例子。 I found that cPaint = new Paint(Color.GRAY);
我发现cPaint = new Paint(Color.GRAY);
is wrong. 是错的。 I changed it to cPaint = new Paint(); cPaint.setColor(Color.WHITE);
我将其更改为cPaint = new Paint(); cPaint.setColor(Color.WHITE);
cPaint = new Paint(); cPaint.setColor(Color.WHITE);
and it worked. 而且有效。
Its just because you pass values in paint Construtor *cPaint = new Paint(VALUES) * it returns null! 这仅仅是因为您在paint Construtor中传递了值* cPaint = new Paint(VALUES)*它返回null! when you pass null in canvas functions as paint object, it gives u default color (BLACK). 当您在画布函数中将null作为绘画对象传递时,它将提供默认的颜色(BLACK)。 Correct way of initializing paint object is as below... 初始化绘画对象的正确方法如下:
cPaint = new Paint();
cPaint.setColor(Color.GRAY);
tPaint = new Paint();
tPaint.setColor(Color.BLACK);
1. Put invalidate();
1.放入invalidate();
after the line canvas.drawTextOnPath(text, circle, 0, 20, tPaint);
在canvas.drawTextOnPath(text, circle, 0, 20, tPaint);
2. Try not to set Background in the constructor and see if it draws. 2.尝试不要在构造函数中设置Background,看看它是否绘制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.