自定義控件,實現加載圖片時透明變換

自定義控件,通常都是經過繼承已有的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); }}

這當中使用了屬性 AlphaImageView_duration ,須要在values目錄下新建attrs文件中聲明,兩種方式
一、不聲明類型:
   
   
   
   
<?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>


第一種方式,有可能致使編譯正確,但運行錯誤,第二種編譯器會拒絕輸入錯誤類型 。其中的 AlphaImageView 能夠是非自定義控件的類名。而後在佈局文件中添加一個AlphaImageView實例

   
   
   
   
<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" >
如上方所示,①的命名控件爲安卓系統提供的控件的命名控件,②是自定義的命名控件,由 http://schemas.android.com/apk/res+應用的包名 共同構成!

此時就完成了一個自定義控件!!









相關文章
相關標籤/搜索