@Override
protected void onDraw(Canvas canvas)
{
//Note:I do not want to use the canvas object from this function param
//If i do so its working , But i would like to understand why the following is not working
Canvas c =new Canvas();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
c.drawText("HELLO CANVAS",200,300,paint);
}
更多的代碼安卓:寫作使用的drawText()文本不可見
public class graphicProj extends Activity {
private Canvas canvas;
@Override
public void onCreate(Bundle savedInstanceState) {
{
....
SimpleView simpleview_obj = new SimpleView(this);
setContentView(simpleview_obj);
simpleview_obj.onDraw(canvas);
.....
new GetData().execute();
}
private static class SimpleView extends View {
private ShapeDrawable mDrawable = new ShapeDrawable();
....
protected void onDraw(Canvas canvas) {
//draw graphic objects
....
}
}
public class GetData extends AsyncTask<Void, String, Void> {
@Override
protected void onPreExecute() {
Log.d("PROJ","STARTIN");
}
@Override
protected Void doInBackground(Void... unused) {
////My calculation and reading frm DataStream
}
@Override
protected void onProgressUpdate(String... data) {
//I Keep updating the result...
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
canvas.drawText(result, 200, 300, paint);
}
@Override
protected void onPostExecute(Void unused) {
Log.d("PROJ","END");
}
}
}
你怎麼知道它不工作? – rajath 2011-04-12 12:58:07
如果我將最後一行更改爲canvas.drawText(「HELLO CANVAS」,200,300,paint);有用。但我想知道爲什麼上述一個不工作。沒有在這個意義上工作,它不會在屏幕上打印任何字符! – m4n07 2011-04-12 12:59:19
因爲屏幕使用'canvas',而不是'c' ......這就是爲什麼系統爲您提供特定的畫布以供繪製。 – rajath 2011-04-12 13:01:48