Android 自定义控件3

来源:互联网 发布:hgo数据处理软件 编辑:程序博客网 时间:2024/06/16 04:03

创建新的控件:

作为一个有创意的开发者,你经常会遇到安卓原生控件无法满足你的需求。

为了优化你的界面和工作流程,安卓允许你去继承已经存在的控件或者实现你自己的控件。

 

那么最好的方式去创建一个新的控件是什么?  这主要取决你想要完成什么。

1.有些基本功能原生控件都能提供,所以这个时候你只需要继承并对控件进行扩展。通过重写它的事件,onDraw,但是始终都保持都父类方法的调用。

2.组合控件 就是通过合并几个控件的功能来生成一个控件。

3.完完整整创建一个新的控件。

 

1.修改存在的控件

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
publicclass MyTextView extendsTextView {
 
    publicMyTextView(Context context, AttributeSet ats, intdefStyle) {
        super(context, ats, defStyle);
    }
 
    publicMyTextView(Context context) {
        super(context);
    }
 
    publicMyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    publicvoid onDraw(Canvas canvas) {
        // 在画布上画文本之下的内容
         
        // 保证默认的文本渲染
        super.onDraw(canvas);
         
        // 在画布上画文本之上的内容
 
    }
 
    @Override
    publicboolean onKeyDown(intkeyCode, KeyEvent keyEvent) {
        // 写自己的控制
         
        // 保持父类默认的控制
        returnsuper.onKeyDown(keyCode, keyEvent);
    }
}

 

2.组合控件

1.最简单的方式,是定义了XML布局文件,然后用include实现重用。(。。。这也算啊。。。)

2.去合并一个控件 通常你自定义的控件需要继承一个ViewGroup(通常就是Layout),就像:

?
1
2
3
4
5
6
7
8
9
publicclass MyCompoundView extendsLinearLayout {
    publicMyCompoundView(Context context) {
        super(context);
    }
 
    publicMyCompoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

 

         就像activity,比较好的设计一个混合的控件UI布局是使用一个外部的layout资源。

         这里我们模拟定义一个:

 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xmlversion=”1.0” encoding=”utf-8”?>
  <LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
   android:orientation=”vertical”
   android:layout_width=”match_parent”
   android:layout_height=”wrap_content”>
   <EditText
      android:id=”@+id/editText”
      android:layout_width=”match_parent”
      android:layout_height=”wrap_content”
   />
   <Button
      android:id=”@+id/clearButton”
      android:layout_width=”match_parent”
      android:layout_height=”wrap_content”
      android:text=”Clear”
   />
 </LinearLayout>

      然后在构造函数初始化的时候:

     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
publicclass ClearableEditText extendsLinearLayout {
 
    EditText editText;
    Button clearButton;
 
    publicClearableEditText(Context context) {
        super(context);
 
        // Inflate the view from the layout resource.
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
 
        li = (LayoutInflater) getContext().getSystemService(infService);
                /*这句很关键,解析反射资源文件,然后将布局附加到当前的控件,也就是this*/
        li.inflate(R.layout.clearable_edit_text,this,true);
 
        /* 因为反射成功后的布局已经附加上了,那么直接可以findViewById*/
        editText = (EditText) findViewById(R.id.editText);
        clearButton = (Button) findViewById(R.id.clearButton);
 
        // 下面自定义的方法就是为控件注册监听,不解释了
        hookupButton();
    }
}

使用:在activity_main.xml

 

<com.example.customview.MyCompoundView android:layout_width="match_parent" android:layout_height="wrap_content" />

 3. 完完全全自定义控件

通常是继承View或者SurfaceView ,View类提供一个Canvas(画布)和一系列的画的方法,还有Paint(画笔)。使用它们去创建一个自定义的UI。你可以重写事件,包括屏幕接触或者按键按下等等,用来提供与用户交互。

1.如果你不需要快速重画和3D图像的效果,那么让View作为父类提供一个轻量级的解决方案。

2.如若不然,就需要使用SurfaceView作为父类,这样你就可以提供一个后台线程去画和使用OPENGL去实现你的图像。这个就相对重量级了,如果你的视图需要经常更新,然后由需要显示比较复杂的图像信息(尤其是在游戏和3D可视化),SurfaceView将是更好的选择。

在这里我们讨论前者,后者后期再讨论。

 一般你需要重写2个方法: 
1.onMeasure

什么是onMeasure?

下面转载一段文章:

View在屏幕上显示出来要先经过measure(计算)和layout(布局). 
1、什么时候调用onMeasure方法?  
当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec. 
两个参数指明控件可获得的空间以及关于这个空间描述的元数据.

更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.

    widthMeasureSpec和heightMeasureSpec这2个参数都是整形是出于效率的考虑,所以经常要做的就是对其解码=>

  1. int specMode = MeasureSpec.getMode(measureSpec);
  2. int specSize = MeasureSpec.getSize(measureSpec);

  

  1. 依据specMode的值,(MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST
  2. 如果是AT_MOST,specSize 代表的是最大可获得的空间; 
    如果是EXACTLY,specSize 代表的是精确的尺寸; 
    如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。 
    2、那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢? 
    经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。 
    而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式表示你没有指定大小。
  3. View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。  
    有个观念需要纠正的是,fill_parent应该是子view会占据剩下容器的空间,而不会覆盖前面已布局好的其他view空间,当然后面布局子 view就没有空间给分配了,所以fill_parent属性对布局顺序很重要。以前所想的是把所有容器的空间都占满了,难怪google在2.2版本里 把fill_parent的名字改为match_parent. 
    在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。 
    接下来的框架代码给出了处理View测量的典型实现:

      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Override
    protectedvoid onMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
        intmeasuredHeight = measureHeight(heightMeasureSpec);
        intmeasuredWidth = measureWidth(widthMeasureSpec);
 
        setMeasuredDimension(measuredHeight, measuredWidth); // 记住这句可不能省。
    }
 
    privateint measureHeight(intmeasureSpec) {
        intspecMode = MeasureSpec.getMode(measureSpec);
        intspecSize = MeasureSpec.getSize(measureSpec);
 
        // Default size if no limits are specified.
        intresult = 500;
 
        if(specMode == MeasureSpec.AT_MOST) {
            // Calculate the ideal size of your
            // control within this maximum size.
            // If your control fills the available
            // space return the outer bound.
            result = specSize;
        }elseif (specMode == MeasureSpec.EXACTLY) {
            // If your control can fit within these bounds return that value.
            result = specSize;
        }
        returnresult;
    }
 
    privateint measureWidth(intmeasureSpec) {
        // 代码基本类似measureHeight
    }

       总结:

通过

            int specMode = MeasureSpec.getMode(measureSpec); 
            int specSize = MeasureSpec.getSize(measureSpec);

这2个值,然后计算自己想要占有的宽和高。

2.onDraw

这个不解释了。(后期会细说canvas和paint)

0 0
原创粉丝点击