自定义控件

来源:互联网 发布:正元恒邦数据是假的吗 编辑:程序博客网 时间:2024/06/05 13:26
自定义控件
刚接触自定义控件的时候觉得就是继承已存在的控件,再次基础上添加一些属性什么的就可以实现自己想要的效果。后来发现实际开发中自定义控件极大的方便,和多样化例如我们在app的标题栏,例如股票显示的各种图线,样式新颖的popWindow等等,今天我就来讲一下我自己对自定义控件的应用
首先
自定义控件是基于view的,其基本的函数操作为onMeasure()、onLayout()、onDraw()三个子方法。
onMeasure()方法实现自己的计算视图大小的方式,并通过setMeasuredDimension(width, height)保存计算结果。
onLayout()用于设置视图在屏幕中显示的位置
  (1)setFrame(l,t,r,b),l,t,r,b即子视图在父视图中的具体位置,该函数用于将这些参数保存起来;
      (2)onLayout(),在View中这个函数什么都不会做,提供该函数主要是为viewGroup类型布局子视图用的;
onDraw()利用前两部得到的参数,将视图显示在屏幕上,到这里也就完成了整个的视图绘制工作。


自定义控件的构造方法
创建自定义控件的3种主要实现方式:
1)继承已有的控件来实现自定义控件: 主要是当要实现的控件和已有的控件在很多方面比较类似, 通过对已有控件的扩展来满足要求。
2)通过继承一个布局文件实现自定义控件,一般来说做组合控件时可以通过这个方式来实现。
    注意此时不用onDraw方法,在构造广告中通过inflater加载自定义控件的布局文件,再addView(view),自定义控件的图形界面就加载进来了。
3)通过继承view类来实现自定义控件,使用GDI绘制出组件界面,一般无法通过上述两种方式来实现时用该方式

自定义控件添加属性

1)在View类中定义。通过构造函数中引入的AttributeSet 去查找XML布局的属性名称,然后找到它对应引用的资源ID去找值。

<span style="font-family:Microsoft YaHei;">public class DefineView extends View {        private String mtext;    public DefineView(Context context) {        super(context);    }    public DefineView(Context context, AttributeSet attrs) {        super(context, attrs);        int resourceId = 0;        int textId = attrs.getAttributeResourceValue(null, "Text",0);        mtext = context.getResources().getText(textId).toString();    }        @Override    protected void onDraw(Canvas canvas) {        Paint paint = new Paint();        paint.setColor(Color.RED);        canvas.drawText(mtext, bw/2, 30, paint);    }}</span>

布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.example.defineview.Defineiew        android:id="@+id/myView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         Text="@string/hello_world"/></LinearLayout>

2)通过XML为View注册属性。与Android提供的标准属性写法一样。

public class DefineImageView extends LinearLayout {    public DefineImageView(Context context) {        super(context);    }    public DefineImageView(Context context, AttributeSet attrs) {        super(context, attrs);        int resourceId = -1;        TypedArray typedArray = context.obtainStyledAttributes(attrs,                R.styleable.DefineImageView);        ImageView iv = new ImageView(context);        TextView tv = new TextView(context);        int N = typedArray.getIndexCount();        for (int i = 0; i < N; i++) {            int attr = typedArray.getIndex(i);            switch (attr) {            case R.styleable.DefineImageView_Text:                resourceId = typedArray.getResourceId(                        R.styleable.DefineImageView_Text, 0);                tv.setText(resourceId > 0 ? typedArray.getResources().getText(                        resourceId) : typedArray                        .getString(R.styleable.DefineImageView_Text));                break;            case R.styleable.DefineImageView_Src:                resourceId = typedArray.getResourceId(                        R.styleable.DefineImageView_Src, 0);                iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);                break;               }        }        addView(iv);        addView(tv);        typedArray.recycle();    }}

attrs.xml进行属性声明, 文件放在values目录下

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="DefineImageView">        <attr name="Text" format="string"></attr>        <attr name="Src" format="integer"></attr>    </declare-styleable></resources>

MainActivity的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:uview="http://schemas.android.com/apk/res/com.example.Defineimageview2"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <com.example.defineimageview.DefineImageView        android:id="@+id/myImageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        uview:Text="这是一个图片说明"         uview:Src="@drawable/tw"        uview:Oriental="Vertical">    </com.example.myimageview2.MyImageView></LinearLayout>
控件绘制onDraw()
通过画布canves画笔paint绘制自己需要的
自定义的方法
<div></div><div>onMeasure() 检测View组件及其子组件的大小</div><div></div><div>onLayout() 当该组件需要分配其子组件的位置、大小时</div><div></div><div>onSizeChange() 当该组件的大小被改变时</div><div></div><div>onDraw() 当组件将要绘制它的内容时</div><div></div><div>onTouchEvent 当发生触屏事件时</div><div></div><div>onWindowFocusChanged(boolean)  当该组件得到、失去焦点时</div><div></div>

0 0