子类化视图并覆​​盖onDraw(画布)

时间:2010-07-23 09:27:40

标签: android

当我在Activity中实例化myView时,为什么需要传递上下文,为什么需要将上下文传递给View的构造函数?有什么意义?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.graphics.Canvas;

public class POCII extends Activity {

   myView mv = new myView(this);
   /** Called when the activity is first created. */

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(mv);
   }
}

class myView extends View {

   public myView(Context context) {
      super(context);
   }

   @Override
   public void onDraw(Canvas canvas) {
      Paint paint = new Paint();
      canvas.drawRect(0,0,100,100, paint);
   }
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)