的onDraw()进行查看 [英] onDraw() for View

查看:252
本文介绍了的onDraw()进行查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在我的布局文件中创建一个视图位置:

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>

这是我的看法code:

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 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倍(第一为preSS按住按钮,第二松动的点击)为什么会发生?

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?

推荐答案

这是因为你正在创建MyView的的第二个实例。你需要做出MyView的参考这是您的布局:

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

        } 

    }); 
} 

这篇关于的onDraw()进行查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆