前面已经介绍过自定义View组件的开发,自定义View组件与Android系统提供的View组件一样,即可在Java代码中使用,也可在XML界面布局代码中使用。

当在XML布局文件中使用Android系统提供的View组件时,开发者可以指定多个属性,这些熟悉可以很好地控制View组价的外观行为。如果用户开发的自定义View组件也需要指定属性,就需要属性资源的帮助了。

属性资源文件也放在/res/values目录下,属性资源的根元素也是<resources.../>,该元素里包含如下两个子元素。

  • attr子元素:定义一个属性。
  • declare-styleable子元素:定义一个styleable对象,每个styleable对象就是一组attr属性的集合。

当我们使用属性文件定义了属性之后,接下来就可以在自定义组价的构造器中通过AttributeSet对象获取这些属性了。

例如我们想开发一个默认带动画效果的图片,该图片显示时,自动从透明变成完全不透明,为此我们需要定义一个自定义组件,但这个自定义组件需要指定一个额外duration属性,该属性控制动画的持续时间。

为了在自定义组件中使用duration属性,需要先定义如下属性资源文件。

程序清单\res\values\attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 定义一个属性 -->
<attr name="duration"  format="integer"></attr>
<!-- 定义一个styleable对象来组合多个属性 -->
<declare-styleable name="AlphaImageView">
<attr name="duration"/>
</declare-styleable>
</resources>

上面的属性资源文件定义了该属性之后,至于到底在哪个View组件中使用该属性,该属性到底能发挥什么作用,就不归属性资源文件管了。属性资源所定义的属性到底可以发挥什么作用,取决于自定义组件的代码实现。

例如如下自定义的AlphaImageView,它获取了定义该组件所指定的duration属性之后,根据该属性来控制图片透明度的改变。程序代码如下。

提示:

在属性资源中定义<declare-styeable.../>元素时,也可在其内部直接使用<attr.../>定义属性,使用<attr.../>时指定一个format属性即可,例如上面我们可以指定<attr name="duration"  format="integer"/>

该程序的后台代码如下:

package com.example.studyresources;

import java.util.Timer;
import java.util.TimerTask; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView; public class AlphaImageView extends ImageView {
//图像透明度每次改变的大小
private int alphaDelta=0;
//记录图片当前的透明度
private int curAlpha=0;
//每隔多少毫秒透明度改变一次
private final int SPEED=300;
Handler handler=new Handler()
{ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==0x123)
{
//每次增加curAlpha的值
curAlpha+=alphaDelta;
if(curAlpha>255)
{
curAlpha=255;
}
//修改该ImageView的透明度
AlphaImageView.this.setAlpha(curAlpha);
}
} };
public AlphaImageView(Context context,AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.AlphaImageView);
//获取duration参数
int duration=typedArray.getInt(R.styleable.AlphaImageView_duration, 0);
//计算图像透明度每次改变的大小
alphaDelta=255*SPEED/duration;

}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
this.setAlpha(curAlpha);
super.onDraw(canvas);
final Timer timer=new Timer();
//按固定间隔发送短信,通知系统改变图片的透明度
timer.schedule(new TimerTask(){ @Override
public void run() {
// TODO Auto-generated method stub
Message msg=new Message();
msg.what=0x123;
if(curAlpha>=255)
{
timer.cancel();
}
else
{
handler.sendMessage(msg);
}
}}, 0,SPEED);
} }

上面的程序中粗体字代码用于获取定义AlphaImageView时指定的duration属性,并根据该属性计算图片的透明度和变化幅度,接着程序重写了ImageView的onDraw(Canvas canvas)方法,该方法启动了一个任务调度来控制图片透明度的改变。

上面粗体字代码中的R.styeable.AlphaImageView、R.styeable.AlphaImageView_duration都是Android SDK根据属性资源文件自动生成的。

接着在界面布局文件使用AlphaImageView时可以为它指定一个duration属性,注意该属性位于"http://schemas.android.com/apk/res/+项目子包"命名空间下,例如本应用的包名为com.example.studyresources,那么duration属性就位于"http://schemas.android.com/apk/res/com.example.studyresources"命名空间下。

下面是该应用的界面布局文件的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:studyresources="http://schemas.android.com/apk/res/com.example.studyresources"

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义自定义组件,并指定属性资源中定义的属性 -->
<com.example.studyresources.AlphaImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ee"
studyresources:duration="60000"
/> </LinearLayout>

上面的程序中第一行粗体字代码用于导入http://schemas.android.com/apk/res/com.example.studyresources命名空间,并指定该命名空间对应的短名前缀为studyresources;
   程序第二行粗体字代码用于为AlphaImageView组件指定自定义属性duration的属性值为60000。

主程序无须指定任何特殊的控制,只要简单地加载并显示上面的界面布局文件,运行该程序时即可看到该图片从无到有,慢慢显示出来的效果。

如下图:

属性(Attribute)资源的更多相关文章

  1. Android应用资源--之属性(Attribute)资源

    原文链接: http://wujiandong.iteye.com/blog/1184921 属性(Attribute)资源:属于整个Android应用资源的一部分.其实就是网上一堆介绍怎么给自定义V ...

  2. 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨

    1.引子 转发和重定向是我们在做web项目中常用到的两个术语,有必要理清两者的区别和与之相关的参数.属性获取问题. 2.转发和重定向 1).转发 转发是服务器行为,将当前请求(Request)和响应( ...

  3. C#属性(Attribute)用法实例解析

    属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变.本文就以实例形式分析了C#中属性的应用.具体入戏: 一.运用范围 程序集,模块,类型(类,结构,枚举,接口,委 ...

  4. opencart3属性attribute实现换行等简单html代码

    opencart3属性attribute在前台页面默认是没有解析html代码功能的,比如想实现换行,后台这样写:line 1<br>line 2,但前台产品页也是line 1<br& ...

  5. Servlet中的属性(attribute)和参数(parameter)的区别

    1.引子 初学者对属性(attribute)和参数(parameter)容易搞混.没搞清他们的区别,项目中就可能出现一此莫名其妙的问题. 2.两者的区别 1) 属性(attribute) 属性是在后台 ...

  6. C#教程之C#属性(Attribute)用法实例解析

    引用:https://www.xin3721.com/ArticlecSharp/c11686.html 属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变.本文 ...

  7. 属性 Attribute

    一.创建属性 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor, AllowMultiple = true, ...

  8. 【转】Struts1.x系列教程(3):属性(资源)文件乱码问题的解决之道

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/14/251244.html ...

  9. boolean attribute(布尔值属性) attribute vs property

    boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean val ...

  10. WPF样式中TargetType 属性 (Property) 和 x:Key 属性 (Attribute)

    如第一个示例所示,如果将 TargetType 属性设置为 TextBlock 而不为样式分配 x:Key,样式就会应用于所有 TextBlock 元素.这种情况下,x:Key 隐式设置为 {x:Ty ...

随机推荐

  1. 何查询SQL Server数据库没有主键的表并增加主键

    SQL Server数据库中,如果一个表没有主键,我们该如何查询呢?本文我们主要就介绍了如何查询数据库中没有主键的表名并为其增加主键的方法,希望能够对您有所帮助. 该功能的实现代码如下: declar ...

  2. Abandoned country

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 滑雪(ski)

    滑雪(ski) 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  4. HDU 1896 Stones(优先队列)

    还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...

  5. string数组转化成int数组

    string idsStr = "1,2,3,4,5"; int[] ids = idsStr.Split(',').Select(Int32.Parse).ToArray();

  6. IMapControl3 Interface(1) Properties属性

    IMapControl3 Interface Provides access to members that control the MapControl. Note: the IMapControl ...

  7. php script 的生命周期

    原文地址:https://support.cloud.engineyard.com/hc/en-us/articles/205411888-PHP-Performance-I-Everything-Y ...

  8. CodeForces 609A USB Flash Drives

    水题 #include<cstdio> #include<cmath> #include<algorithm> using namespace std; +; in ...

  9. 转载:org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.Annotation

    转载:org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.Annotation (2012 ...

  10. 转 [ javascript面向对象技术

    以下文章来自iteye,作者是 sdcyst ,个人主页 http://www.iteye.com/topic/288813 类变量/类方法/实例变量/实例方法先补充一下以前写过的方法:在javasc ...