开发者

onDraw() for View

开发者 https://www.devze.com 2023-03-25 06:15 出处:网络
I just created one view in my layout file here: <?xml version=\"1.0\" encoding=\"utf-8\"?> <RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_height=

I just created one view in my layout file here:

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

And this is my view code:

    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()));


       }

}

And last is my main activity:

    public class Activity2 extends Activity{
    MyView mv;
    AttributeSet attributes;
    public 开发者_StackOverflowvoid 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();

            }

        });
    }
}

My question is after clicking the button, the view is not refreshed. And i also found that after clicking the button, the onDraw() is performing 2times(first for press down the button,second for loose the clicking) why that happened?


This is because you are creating the second instance of MyView. You need to make a reference to MyView which is in your layout:

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(); 

        } 

    }); 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消