您的位置:首页 > 移动开发 > Android开发

Android_自定义View

2016-03-21 10:30 513 查看
@作者 : 西野奈留

@博客:http://blog.csdn.net/narunishino

-2016/4/08-更新-

-2016/3/28-更新-

-2016/3/21-

view

//假设自定义的是view

步骤:(java代码)

1. 继承view。

2. 添加构造方法。在构造方法中初始化属性和画笔。//参考[android开发进阶(何红辉)P36]

3. 系统从属性中获取widthMeasureSpec和heightMeasureSpec两个值,我们在重写onMeaure方法中,根据这两个值的specMode来自定义大小。//参考[android群英传(徐宜生)P36]。

4. 重写onDraw方法,在onDraw方法里面绘制文字图形图片。

viewGroup

//假设你自定义的是viewGroup

1. 在valus–>attrs.xml文件(自己新建)中写上你要自定义的属性。

2. 在xml里面写上你要显示的view(不用管位置什么的,代码如下)。

3. 在java文件中重写

——1. onMeasure方法 ,//获取从xml中传过来的信息然后在这里,手动测量子view大小并得到结果从而确定了子view本身的大小。

——2. onLayout方法 ,//获取从xml中传过来的信息然后在这里,确定各个子view放在哪个位置(所以上面说不用管位置什么的 )。

——3. onDraw方法,//获取从xml中传过来的信息然后在这里,画图。

<?xml version="1.0" encoding="utf-8"?>
<com.atozmak.circleandroundimageviewdemo.widgets.ArcMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:atoz="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
atoz:position="right_bottom"
atoz:radius="150dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_music"
android:tag="Music" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_place"
android:tag="Place" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_sleep"
android:tag="Sleep" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_thought"
android:tag="Sun" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_with"
android:tag="People" />

</com.atozmak.circleandroundimageviewdemo.widgets.ArcMenu>


执行顺序

CustomView构造函数 //调用1次

onFinishInflate //调用1次

onMeasure //调用不定次

onSizeChanged //调用1次

onLayout //调用不定次

onDraw //调用不定次

参考:

http://www.imooc.com/learn/300

http://blog.csdn.net/dmk877/article/details/49558367

-End-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息