自定义控件时使用自定义属性

  这里以自定义一个可以控制圆角显示的ImageView控件UpRoundImageView为例,展开说明。

1、/res/values/arrrs.xml

其中declare-styleable标签的属性name最好是自定义控件的类名。

  标签attr的属性name是自定义的;format:类型值,有多种可选,并且可以指定多种类型值,将在后面进行详细说明。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="UpRoundImageView">
        <attr name="leftup_radius_l" format="float"/>
        <attr name="leftup_radius_r" format="float"/>
        <attr name="rightup_radius_l" format="float"/>
        <attr name="rightup_radius_r" format="float"/>
        <attr name="rightdown_radius_l" format="float"/>
        <attr name="rightdown_radius_r" format="float"/>
        <attr name="leftdown_radius_l" format="float"/>
        <attr name="leftdown_radius_r" format="float"/>
    </declare-styleable>
</resources>

  2、UpRoundImageView: 

package com.example.qyn.projectqyn.customviews;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 自定义圆角的ImageView
 * Created by Administrator on 2017/9/25.
 */

@SuppressLint("AppCompatCustomView")
public class UpRoundImageView extends ImageView {

    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/
//    private float[] rids = {5.0f,5.0f,5.0f,5.0f,0.0f,0.0f,0.0f,0.0f};
    private float[] rids ;

//    public UpRoundImageView(Context context) {
//        super(context);
//    }

    public UpRoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * AttributeSet控件的属性集合,已键值对的形式存在。
         * attrs.getAttributeFloatValue(String urlres,String keystr,float default);其中urlstr:attrs资源文件被引用地址,与布局文件中的保持一直;keystr:自定义属性的name;default:默认值。该方法输出为单精度浮点小数。
         */
        float leftup_radius_l = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","leftup_radius_l",0.0f);
        float leftup_radius_r = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","leftup_radius_r",0.0f);
        float rightup_radius_l = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","rightup_radius_l",0.0f);
        float rightup_radius_r = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","rightup_radius_r",0.0f);
        float rightdown_radius_l = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","rightdown_radius_l",0.0f);
        float rightdown_radius_r = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","rightdown_radius_r",0.0f);
        float leftdown_radius_l = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","leftdown_radius_l",0.0f);
        float leftdown_radius_r = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto","leftdown_radius_r",0.0f);
        rids = new float[]{leftup_radius_l,leftup_radius_r,rightup_radius_l,rightup_radius_r,rightdown_radius_l,rightdown_radius_r,leftdown_radius_l,leftdown_radius_r};
    }

//    public UpRoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }


    /**
     * 画图
     * @param canvas
     */
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(0,0,w,h),rids,Path.Direction.CCW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
}

  3、布局文件activity_seconed.xml

xmlns:qiradius="http://schemas.android.com/apk/res-auto",其中的“qiradius”是自定义。
“res-auto”在低版本中可以替换成“你的包名”。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:qiradius="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <com.example.qyn.projectqyn.customviews.UpRoundImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        qiradius:leftup_radius_l="6.0"
        qiradius:leftup_radius_r="6.0"
        qiradius:rightup_radius_l="6.0"
        qiradius:rightup_radius_r="6.0"
        qiradius:rightdown_radius_l="6.0"
        qiradius:rightdown_radius_r="6.0"
        qiradius:leftdown_radius_l="6.0"
        qiradius:leftdown_radius_r="6.0"
        android:src="@mipmap/shouye"
        android:scaleType="fitXY"/>

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:text="@string/app_name"
        android:layout_marginTop="10dp" />

</LinearLayout>

  至此,对自定义控件和属性的介绍已然完成。下面将对format所拥有的类型值详细说明,以及多种类型值叠加的使用。

1. reference:参考某一资源ID。

(1)属性定义:

代码如下:


<declare-styleable name = "名称">

   <attr name = "background" format = "reference" />

</declare-styleable>

(2)属性使用:

代码如下:


<ImageView

android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID"
/>

2.color:颜色值。

(1)属性定义:

代码如下:

<declare-styleablename="名称">

<attrname="textColor"format="color"/>

</declare-styleable>

(2)属性使用:

代码如下:

<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00"
/>

3.boolean:布尔值。

(1)属性定义:

代码如下:

<declare-styleablename="名称">

<attrname="focusable"format="boolean"/>

</declare-styleable>

(2)属性使用:

代码如下:

<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true"
/>

4.dimension:尺寸值。

(1)属性定义:

代码如下:

<declare-styleablename="名称">

<attrname="layout_width"format="dimension"/>

</declare-styleable>

(2)属性使用:

代码如下:

<Button
android:layout_width="42dip"
android:layout_height="42dip"
/>

5.float:浮点值。

(1)属性定义:

代码如下:

<declare-styleablename="AlphaAnimation">
<attrname="fromAlpha"format="float"/>
<attrname="toAlpha"format="float"/>
</declare-styleable>

(2)属性使用:

代码如下:

<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7"
/>

6.integer:整型值。

(1)属性定义:

代码如下:

<declare-styleablename="AnimatedRotateDrawable">
<attrname="visible"/>
<attrname="frameDuration"format="integer"/>
<attrname="framesCount"format="integer"/>
<attrname="pivotX"/>
<attrname="pivotY"/>
<attrname="drawable"/>
</declare-styleable>

(2)属性使用:

代码如下:

<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/图片ID"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100"
/>

7.string:字符串。

(1)属性定义:

代码如下:

<declare-styleablename="MapView">
<attrname="apiKey"format="string"/>
</declare-styleable>

(2)属性使用:

代码如下:

<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

8.fraction:百分数。

(1)属性定义:

代码如下:

<declare-styleablename="RotateDrawable">
<attrname="visible"/>
<attrname="fromDegrees"format="float"/>
<attrname="toDegrees"format="float"/>
<attrname="pivotX"format="fraction"/>
<attrname="pivotY"format="fraction"/>
<attrname="drawable"/>
</declare-styleable>

(2)属性使用:

代码如下:

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@anim/动画ID"

android:fromDegrees="0"
android:toDegrees="360"

android:pivotX="200%"

android:pivotY="300%"
android:duration="5000"

android:repeatMode="restart"

android:repeatCount="infinite"

/>

9.enum:枚举值。

(1)属性定义:

代码如下:

<declare-styleablename="名称">
<attrname="orientation">
<enumname="horizontal"value="0"/>
<enumname="vertical"value="1"/>
</attr>
</declare-styleable>

(2)属性使用:

代码如下:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>

10.flag:位或运算。

(1)属性定义:

代码如下:

<declare-styleablename="名称">
<attrname="windowSoftInputMode">
<flagname="stateUnspecified"value="0"/>
<flagname="stateUnchanged"value="1"/>
<flagname="stateHidden"value="2"/>
<flagname="stateAlwaysHidden"value="3"/>
<flagname="stateVisible"value="4"/>
<flagname="stateAlwaysVisible"value="5"/>
<flagname="adjustUnspecified"value="0x00"/>
<flagname="adjustResize"value="0x10"/>
<flagname="adjustPan"value="0x20"/>
<flagname="adjustNothing"value="0x30"/>
</attr>
</declare-styleable>

(2)属性使用:

代码如下:

<activity
android:name=".StyleAndThemeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

特别要注意:

属性定义时可以指定多种类型值。

(1)属性定义:

代码如下:

<declare-styleablename="名称">
<attrname="background"format="reference|color"/>
</declare-styleable>

(2)属性使用:

代码如下:

<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID|#00FF00"
/>
 
参考文献:
   1、http://www.jb51.net/article/48962.htm

    2、自定义UpRoundImageView控件很早以前找的,现在找不到出处了。

有更多有趣的用法请留言。

原文地址:https://www.cnblogs.com/qynprime/p/8079010.html