onDraw()for View

时间:2011-08-05 06:59:17

标签: android

我刚在布局文件中创建了一个视图:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent">
    <com.shawntesting.MyView android:layout_above="@+id/button1" android:layout_alignParentTop="true" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/myview1"></com.shawntesting.MyView>
    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true" ></Button>
    <Button android:id="@+id/button2" android:text="button2" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</RelativeLayout>

这是我的观看代码:

    public class MyView extends View{



   Paint paint;


   public MyView(Context context, AttributeSet attrs){ 
       super(context,attrs);
       paint=new Paint();
       paint.setColor(Color.WHITE);

   }


   public void setblue(){

      paint.setColor(Color.BLUE);
      this.postInvalidate();

   }
   public void setgreen(){

       paint.setColor(Color.GREEN);
       this.postInvalidate();
   }

  public void onDraw(Canvas canvas){     
          super.onDraw(canvas);
            canvas.drawRect(x, y, 100, 50, paint);
            String i=new String();
            Log.i("getColor",i.valueOf(paint.getColor()));


       }

}

最后是我的主要活动:

    public class Activity2 extends Activity{
    MyView mv;
    AttributeSet attributes;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        XmlPullParser parser = getResources().getXml(R.layout.main);
         attributes = Xml.asAttributeSet(parser);
         mv=new MyView(this,attributes);

        setContentView(R.layout.main);
        Button b1=(Button)findViewById(R.id.button1);
        Button b2=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mv.setblue();
                //mv=(MyView)findViewById(R.id.myview1);
                //mv.invalidate();

            }

        });
        b2.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mv.setgreen();
                //mv=(MyView)findViewById(R.id.myview1);
                //mv.invalidate();

            }

        });
    }
}

我的问题是点击按钮后,视图没有刷新。 我还发现,点击按钮后,onDraw()执行2次(第一次按下按钮,第二次按下松开点击)为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

这是因为您正在创建MyView的第二个实例。您需要引用布局中的MyView:

MyView mv;  
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main);

    mv = (MyView) findViewById(R.id.myview1);
    Button b1=(Button)findViewById(R.id.button1); 
    Button b2=(Button)findViewById(R.id.button2); 

    b1.setOnClickListener(new Button.OnClickListener(){ 

        public void onClick(View arg0) { 

            mv.setblue(); 

        } 

    }); 
    b2.setOnClickListener(new Button.OnClickListener(){ 

        public void onClick(View arg0) { 

            mv.setgreen(); 

        } 

    }); 
}