<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.hjq.shuxing.AttrResTest
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/start"
hjq:duration="60000"/>
</LinearLayout>这是。
public class AttrResTest 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)
{
if (msg.what == 0x123)
{
//每次增加curAlpha的值
curAlpha += alphaDelta;
if (curAlpha > 255)
curAlpha = 255;
//修改该ImageView的透明度
AttrResTest.this.setAlpha(curAlpha);
}
}
};
/**
* @param context
* @param attrs
*/
public AttrResTest(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ShuxingActivity);
//获取duration参数
int duration = typedArray.getInt(
R.attr.duration, 0);
//计算图像透明度每次改变的大小
alphaDelta = 255 * SPEED / duration;
}
@Override
protected void onDraw(Canvas canvas)
{
this.setAlpha(curAlpha);
super.onDraw(canvas);
final Timer timer = new Timer();
//按固定间隔发送消息,通知系统改变图片的透明度
timer.schedule(new TimerTask()
{
@Override
public void run()
{
Message msg = new Message();
msg.what = 0x123;
if (curAlpha >= 255)
{
timer.cancel();
}
else
{
handler.sendMessage(msg);
}
}
}, 0, SPEED);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.hjq.shuxing.AttrResTest
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/start"
hjq:duration="60000"/>
</LinearLayout>这是。
public class AttrResTest 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)
{
if (msg.what == 0x123)
{
//每次增加curAlpha的值
curAlpha += alphaDelta;
if (curAlpha > 255)
curAlpha = 255;
//修改该ImageView的透明度
AttrResTest.this.setAlpha(curAlpha);
}
}
};
/**
* @param context
* @param attrs
*/
public AttrResTest(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ShuxingActivity);
//获取duration参数
int duration = typedArray.getInt(
R.attr.duration, 0);
//计算图像透明度每次改变的大小
alphaDelta = 255 * SPEED / duration;
}
@Override
protected void onDraw(Canvas canvas)
{
this.setAlpha(curAlpha);
super.onDraw(canvas);
final Timer timer = new Timer();
//按固定间隔发送消息,通知系统改变图片的透明度
timer.schedule(new TimerTask()
{
@Override
public void run()
{
Message msg = new Message();
msg.what = 0x123;
if (curAlpha >= 255)
{
timer.cancel();
}
else
{
handler.sendMessage(msg);
}
}
}, 0, SPEED);
}
}
你还没给这个属性命名吧?
xmlns:hjq="http://schemas.android.com/apk/res/+包名
我命名了。在其他的xml文件中
你看这句不是每个用到android属性的xml文件都要有么
xmlns:android="http://schemas.android.com/apk/res/android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hjq="http://schemas.android.com/apk/res/+包名
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.hjq.shuxing.AttrResTest
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/start"
hjq:duration="60000"/>
</LinearLayout>