【发布时间】:2011-04-12 12:49:19
【问题描述】:
@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);
}
更多代码
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");
}
}
}
【问题讨论】:
-
你怎么知道它不起作用?
-
如果我将最后一行更改为 canvas.drawText("HELLO CANVAS",200,300,paint);有用。但我想知道为什么上面的那个不起作用。从某种意义上说不起作用,它不会在屏幕上打印任何字符!!!
-
因为屏幕使用
canvas,而不是c...这就是系统为您提供特定画布以供您绘制的原因。 -
从实际的角度来看,您的提问没有意义。为什么需要两张画布?
-
因为我在 onDraw() 之外进行计算,而这个计算函数是从 onCreate() 调用的。所以在 onDraw() 我显示一个图形对象,然后我想打印计算值。因为我没有这个画布对象作为 onDraw() 的本地对象
标签: android graphics canvas drawtext