标签:android style http io color ar os 使用 java
自定义控件,一般都是通过继承已有的view类,如TextView,Button。。。或者直接继承其父类View。这次因为要加载图片。所以继承自ImageView。并且实现了一个通过属性来控制的加载时间。首先是自定义的类
/**
*知我者为我心忧,不知我者谓我何求!
*linwoain@outlook.com
*作者 linwoain
*日期 2014/11/7 9:17
*/
package com.linwoain.TestAndroid.fragment;
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.view.View;
import android.widget.ImageView;
import com.linwoain.TestAndroid.R;
import java.util.Timer;
import java.util.TimerTask;
/**
* 自定义控件,实现
* @author linwoain
* @version 2014/11/7 9:17
*/
public class AlphaImageView extends ImageView {
private static final int SPEED = 300;//每隔多少毫秒透明度改变一次
private int alphaDelta = 0;//图像透明度每次改变的大小
//记录图片当前的透明度
private int curAlpha = 0;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
curAlpha += alphaDelta;
if (curAlpha > 255) {
curAlpha = 255;
AlphaImageView.this.setAlpha(curAlpha);
}
}
}
};
public AlphaImageView(Context context) {
this(context, null);
}
public AlphaImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlphaImageView);
int duration = typedArray.getInt(R.styleable.AlphaImageView_duration, 0);//默认不透明
alphaDelta = 255 * SPEED / duration;
typedArray.recycle();
}
@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 message = new Message();
message.what = 0x123;
if (curAlpha >= 255) {
timer.cancel();
} else {
handler.sendMessage(message);
}
}
}, 0, SPEED);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="duration"></attr>
<declare-styleable name="AlphaImageView">
<attr name="duration"></attr>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AlphaImageView">
<attr name="duration" format="integer"></attr>
</declare-styleable>
</resources>
<com.linwoain.TestAndroid.fragment.AlphaImageView
app:duration="6000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gif"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ①
xmlns:app="http://schemas.android.com/apk/res/com.linwoain.TestAndroid" ②
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
标签:android style http io color ar os 使用 java
原文地址:http://www.cnblogs.com/linwoain/p/4080668.html