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.
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.