Android 自定义控件开发入门 (三)

来源:互联网 发布:windows ce6.0刷机包 编辑:程序博客网 时间:2024/06/07 03:54

上两次我们从如何自定义控件讲起,列举了View的一些Api,说明了一些在自定义的时候,可以进行重写的方法,然后通过一个例子的两种写法向大家展示了最基本的自定义控件和我们要充分了解并积极重写View方法的精神,这次我们将继续进行学习!


 现在请大家回想一下我们使用安卓原生控件时的感受,一个好的控件是可以在xml中进行各种属性的操作的,而自定义控件往往有一些特殊的需求,今天我要讲的就是安卓给自定义控件添加自定义的属性。


下面再给大家具体介绍一下如果自定义的View需要有自定义的属性我们该如何处理:


我们还是用这个例子,继续扩展,比如我想在xml中设置球体的半径,我该怎么办呢?

让我们先来具体了解一下自定义属性的一些简单基础,然后通过实例大家就会很容易掌握了!


首先看一下属性文件中format可选项 

    如果自定义控件需要自定义该控件的属性。在开始前,我们需要检查在values目录下是否有attrs.xml,如果没有则创建。下面我们先来了解一下format属性类型都有哪些。


format可选项

"reference" //引用

"color" //颜色

 "boolean" //布尔值

 "dimension" //尺寸值

 "float" //浮点值

 "integer" //整型值

 "string" //字符串

 "fraction" //百分数比如200%

 

这里我们的例子就多用几个自定义属性,帮助大家理解,我们就定义半径,颜色,以及小球刚开始停止的位置就好了,

我们先创建attrs.xml:

代码如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MoveBallversion3">        <attr name="BallColor" format="color" />        <attr name="BallRadius" format="float" />        <attr name="BallStartX" format="float" />        <attr name="BallStartY" format="float" />    </declare-styleable>     </resources>


MoveBallversion3是用来在java代码中找到这些数据的名字,下面每一项前部是属性名,后部是类型。


之后我们在创建自定义控件的时候就可以使用自定义属性了,但是设置的时候要注意:

1)其中com.example.customcomponentsdemo.component.DrawView3这句是需要显示的控件所代表的类。
 2
)com.example.customcomponentsdemo是类的包名,

就是manifast里面package地址


 3
)DrawView3是类名。这个类肯定是继承自View的自定义类,可以是在工程中直接源码添加xxxx.java的,也可以是在libs目录下自己新添加的jar包里面的。如果是jar包里面的一个类,则路径就是jar包里面,

Xml如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:myball="http://schemas.android.com/apk/res/com.example.customcomponentsdemo"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是MoveBall的Demo 版本3"        android:textColor="@color/white" >    </TextView>    <com.example.customcomponentsdemo.component.DrawView3        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="10dp"        myball:BallColor = "@color/white"        myball:BallRadius = "20"        myball:BallStartX = "50"        myball:BallStartY = "200" >    </com.example.customcomponentsdemo.component.DrawView3>        </LinearLayout>


之后在重写的时候获取就可以了。

 

不过有时候会出现这种错误。

有时候使用控件的自定义属性,使用时填写命名空间。 

按照老样子:http://schemas.android.com/apk/控件地址  

在编译的时候报错error: No resource identifier found for attribute 'xxxxt' in package

 

解决:代码看了n遍也没找出问题,最后再网上看了一个老外的回答。ADT升级以后,自定义控件的命名空间的路径访问给优化了改成

http://schemas.android.com/apk/res-auto 这样填充之后ADT会自动去相应的包路径下寻找。


我们在继续向下看:


我们在DrawView3.java 代码编写如下,其中下面的构造方法是重点,我们获取定义的属性,获取方法中后面通常设定默认值如:(floattextSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!

MoveBallversion3就是定义在<declare-styleablename="MoveBallversion3 "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!


好,看一下具体代码:

package com.example.customcomponentsdemo.component;import com.example.customcomponentsdemo.R;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;public class DrawView3 extends View {private float CircleX;private float CircleY;private float CircleR;private int BallColor;public DrawView3(Context context, AttributeSet attrs) {super(context, attrs);// 获取自定义属性TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MoveBallversion3);// 获取开始的X位置属性值,默认大小为:10CircleX = a.getFloat(R.styleable.MoveBallversion3_BallStartX, 10);// 获取开始的位置属性值,默认大小为:10CircleY = a.getFloat(R.styleable.MoveBallversion3_BallStartY, 10);// 获取小球的半径属性值,默认大小为:10CircleR = a.getFloat(R.styleable.MoveBallversion3_BallRadius, 10);// 获取颜色属性值,默认颜色为:0x990000FFBallColor = a.getColor(R.styleable.MoveBallversion3_BallColor, 0x990000FF);a.recycle();}@Overridepublic void onDraw(Canvas canves) {Paint paint = new Paint();paint.setColor(BallColor);canves.drawCircle(CircleX, CircleY, CircleR, paint);}@Overridepublic boolean onTouchEvent(MotionEvent motionevent) {CircleX = motionevent.getX();CircleY = motionevent.getY();this.invalidate();return true;}}

具体的注释也写了,大家好好看看代码就好了,接下来是activity的代码,也就没什么好说的了:

package com.example.customcomponentsdemo.Activity;import com.example.customcomponentsdemo.R;import android.app.Activity;import android.content.Context;import android.os.Bundle;public class MoveBallActivity3 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_moveball3);}}


这样我们的第三个教程也就到这里结束了,这次给大家介绍一下如果自定义的View需要有自定义的属性我们该如何处理,这一讲也是这个系列完结篇,自定义View之路还有很远,我也没有举一些很难的例子,我认为基础知识设计流程就是这样了,学习了这些之后自定义控件的制作就剩下的大家在次基础之上发挥了!之后如果有比较好的例子我还会继续补充的!


源码的地址是:http://download.csdn.net/detail/sunmc1204953974/7738915


希望大家能学到东西!


另外我也是学生,如果有写的不好或者有错误的地方还请大家多多指教,谢谢!




20 0
原创粉丝点击