android组合自绘Widget(2D)

来源:互联网 发布:淘宝靠谱点的军品店 编辑:程序博客网 时间:2024/05/21 06:24

 

现在介绍另一种自绘方式,这种方式利用的原有的Widget,将這些Widget组合起来形成自己的Widget(如由EditButton等组成的下拉列表框)。

下面是如何创建组合Widget的步骤:

<!--[if !supportLists]-->1.   <!--[endif]-->创建一个从Layout继承过来的子类,比如LinearLayout

<!--[if !supportLists]-->2.      <!--[endif]-->在新创建的类里面的构造函数中,有三个构造函数,一个参数的构造函数主要是让使用者能动态建立,而两个参数的构造函数则是用于XML配置的

<!--[if !supportLists]-->3.      <!--[endif]-->在你创建的子WIDGET中可以创建事件监听以来响应相应的事件

<!--[if !supportLists]-->4.      <!--[endif]-->创建你自己的访问器和修改器

<!--[if !supportLists]-->5.      <!--[endif]-->虽然你继承了Layout,但你不一定需要重载onDrawonMeasureLayout会有一些默认的行为让继承的子类可以正常运行,当然,如果你需要重载改变某些行为的话,你也可以重载。

<!--[if !supportLists]-->6.      <!--[endif]-->你也可以重载onKeyDown之类的方法,实现相应的功能

 

下面是一个demo

建立自己的WIDGET:

package com.OwnWidget.OwnButton;

 

import android.content.Context;

importandroid.graphics.Rect;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

 

publicclass OwnLayoutextends LinearLayout

{

     private TextViewmText;

     private ButtonmButton;

     privateint mTag = 0;

    

    public OwnLayout(Context context)

    {

       super(context);

      

       InitLayoutWidget(context);

       //TODO Auto-generatedconstructor stub

    }

   

    public OwnLayout(Context context, AttributeSet attrs)

    {

       super(context, attrs);

      

       InitLayoutWidget(context);

       //TODO Auto-generatedconstructor stub

    }

   

 

   

    privatevoid InitLayoutWidget(Context context)

    {

       this.setOrientation(HORIZONTAL);

      

       mText =new TextView(context);

       mText.setText("JackLam");

       addView(mText,new LinearLayout.LayoutParams(

                LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

      

       mButton =new Button(context);

       mButton.setText("确定");

       addView(mButton,new LinearLayout.LayoutParams(

                LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

   

   

       mButton.setOnTouchListener(new OnTouchListener()

       {

 

           @Override

           publicboolean onTouch(View v, MotionEvent event)

           {

              switch(event.getAction())

              {

              case MotionEvent.ACTION_DOWN:

                  if(mTag == 0)

                  {

                     mText.setText("Love");

                     mTag++;

                  }

                  else if(mTag == 1)

                  {

                     mText.setText("Elaine");

                     mTag++;

                  }

                  else if(mTag == 2)

                  {

                     mText.setText("Love");

                     mTag++;

                  }

                  else

                  {

                     mText.setText("JackLam");

                     mTag = 0;

                  }

                  v.invalidate();

                  break;

                 

              case MotionEvent.ACTION_UP:

                  break;

                 

              default:

                  break;

              }

              returntrue;

           }

          

       });

    }

}

activityxml中加上自己的Widget

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   >

<TextView 

   android:layout_width="fill_parent"

   android:layout_height="wrap_content"

   android:text="@string/hello"

   />

<com.OwnWidget.OwnButton.OwnButton

    android:id="@+id/widget_myButton"

    android:layout_width ="wrap_content"

    android:layout_height ="wrap_content">

</com.OwnWidget.OwnButton.OwnButton>

<com.OwnWidget.OwnButton.OwnLayout

    android:id="@+id/widget_myLayout"

    android:layout_width ="wrap_content"

    android:layout_height ="wrap_content">

</com.OwnWidget.OwnButton.OwnLayout>

</LinearLayout>

 

Activity中使用:

package com.OwnWidget;

 

importcom.OwnWidget.OwnButton.OwnButton;

 

import android.app.Activity;

import android.os.Bundle;

importandroid.widget.LinearLayout;

 

publicclass OwnWidgetextends Activity {

    /** Calledwhen the activity is first created. */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        /*LinearLayout layout = newLinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);

        OwnButtonbtn = newOwnButton(this);

        layout.addView(btn);

        setContentView(layout);*/

        setContentView(R.layout.main);

    }

}

下一章,继续会介绍自绘WIDGET,通过更改一个已存在view

 

 

原创粉丝点击