Android_自定义View


@作者 : 西野奈留
@博客: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>

执行顺序

  1. CustomView构造函数 //调用1次
  2. onFinishInflate //调用1次
  3. onMeasure //调用不定次
  4. onSizeChanged //调用1次
  5. onLayout //调用不定次
  6. onDraw //调用不定次

参考:
http://www.imooc.com/learn/300
http://blog.csdn.net/dmk877/article/details/49558367

-End-


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值