values下定义一个attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="duration">
</attr>
<declare-styleable name="AlphaImageView">
<attr name="duration"/>
</declare-styleable>
</resources>
-----------------------------------------------------------------------------------------
在主界面中定义了AlphaImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:crazyit="http://schemas.android.com/apk/res/org.crazyit.res"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
><org.crazyit.res.AlphaImageView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher"
crazyit:duration="60000"<!-- 将其属性值设为60000 -->
/>
</LinearLayout>
---------------------------------------------------------------------------------------------
可是在AlphaImageView 的构造函数中,无法取出duration的值,代码如下:
public AlphaImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.AlphaImageView);
//获取duration参数
int duration = typedArray.getInt(
R.styleable.AlphaImageView_duration , 0);
//计算图像透明度每次改变的大小
alphaDelta = 255 * SPEED / duration;//duration值为0,不知何原因
}
----------------------------------------------------------------------------------------------
不知是没有存入属性值,还是取值错误,请各位大侠帮帮忙,谢谢
Android开发控件

解决方案 »

  1.   

    <attr name="duration"/>
    改成
    <attr name="duration" format="integer"/>
    试试
      

  2.   

    typedArray.recycle();
    也加了,不好使
    typedArray.getInt(
    R.styleable.AlphaImageView_duration , 0);
    取出来的值是0,也就是缺省的值,不知道为什么
      

  3.   

    The following classes could not be instantiated:
    - org.crazyit.res.AlphaImageView (Open Class, Show Error Log)
    See the Error Log (Window > Show View) for more details.
    Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipsejava.lang.ArithmeticException: / by zero
        at org.crazyit.res.AlphaImageView.<init>(AlphaImageView.java:66)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
        at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
        at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
        at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
        at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:372)//以上是XML编辑界面时提示的错误,应该是duration值为0提示的错误
      

  4.   

    (AlphaImageView.java:66)
    66行是哪里
      

  5.   

    还有你这个地方
    <attr name="duration">
    </attr>是多余的
      

  6.   

    alphaDelta = 255 * SPEED / duration;//这里是66行,duration值为0了