文章目錄
官方文檔 API : BitmapRegionDecoder
一、自定義組件構造方法簡介
1、View(Context context) 構造函數
在代碼中創建 View 對象 , 就會調用該構造函數 , 其中 Context context 參數是組件運行的環境 , 可以從該 Context 對象中獲取當前的主題 , 資源等 ;
/**
* 代碼中創建組件調用該方法
* @param context View 組件運行的上下文對象 , 一般是 Activity ,
* 可以通過該上下獲取當前主題 , 資源等
*/
public LongImageView(Context context) {
super(context);
}
2、View(Context context, @Nullable AttributeSet attrs)
1 . 構造函數簡介 :
① 構造函數使用時機 : 佈局文件中使用組件調用該方法 , 當 View 組件從 XML 佈局文件中構造時 , 調用該方法 ;
② 屬性指定 : 提供的 AttributeSet 屬性在 XML 文件中指定 ;
③ 默認風格 : 該方法使用默認的風格 defStyleAttr = 0 , 該組件的屬性設置只有 Context 中的主題和 XML 中的屬性 ;
2 . 參數分析 :
① Context context 參數 : View 組件運行的上下文環境 , 通過該對象可以獲取當前主題 , 資源等 ;
② AttributeSet attrs 參數 : XML 佈局文件中的 View 組件標籤中的屬性值 ;
/**
* 佈局文件中使用組件調用該方法 ;
* 當 View 組件從 XML 佈局文件中構造時 , 調用該方法
* 提供的 AttributeSet 屬性在 XML 文件中指定 ;
* 該方法使用默認的風格 defStyleAttr = 0 ,
* 該組件的屬性設置只有 Context 中的主題和 XML 中的屬性 ;
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
*/
public LongImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
3、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) 構造函數
1 . 構造函數簡介 :
① 構造函數使用時機 : 佈局文件中使用組件調用該方法 , 當 View 組件從 XML 佈局文件中構造時 , 調用該方法 ;
② 主題風格 : 從 XML 中加載組件同時還會提供一個主題屬性風格 ;
③ 屬性指定 : 提供的 AttributeSet 屬性在 XML 文件中指定 ;
④ 主題風格 : View 組件使用該構造方法 , 從佈局中加載時 , 允許使用一個特定風格 ;
⑤ 示例 : 如 : 按鈕類的構造函數會傳入 defStyleAttr = R.attr.buttonStyle 風格作爲參數 ;
2 . 參數分析 :
① Context context 參數 : View 組件運行的上下文環境 , 通過該對象可以獲取當前主題 , 資源等 ;
② AttributeSet attrs 參數 : XML 佈局文件中的 View 組件標籤中的屬性值 ;
③ int defStyleAttr 參數 : 默認的 Style 風格 , 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效 ;
/**
* 佈局文件中加載組件 , 並提供一個主題屬性風格 ;
* View 組件使用該構造方法 , 從佈局中加載時 , 允許使用一個特定風格 ;
* 如 : 按鈕類的構造函數會傳入 defStyleAttr = R.attr.buttonStyle 風格作爲參數 ;
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
* @param defStyleAttr 默認的 Style 風格
* 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效
*/
public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
4、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) 構造函數
1 . 版本兼容 : Android 5.0(API 級別 21)LOLLIPOP 版本加入的構造函數 , 定義該構造函數 , 必須加上 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
註解 ;
2 . 構造函數簡介 :
① 構造函數使用時機 : 佈局文件中使用組件調用該方法 , 當 View 組件從 XML 佈局文件中構造時 , 調用該方法 ;
② 主題風格或資源 : 從 XML 中加載組件同時還會提供一個主題屬性風格 , 或資源 ;
③ 屬性指定 : 提供的 AttributeSet 屬性在 XML 文件中指定 ;
④ 主題風格 : View 組件使用該構造方法 , 從佈局中加載時 , 允許使用一個特定風格 ;
⑤ 示例 : 如 : 按鈕類的構造函數會傳入 defStyleAttr = R.attr.buttonStyle 風格作爲參數 ;
3 . 屬性設置優先級 ( 優先級從高到低 ) :
-
佈局文件中的標籤屬性 AttributeSet
-
defStyleAttr 指定的默認風格
-
defStyleRes 指定的默認風格
-
主題的屬性值
4 . 參數分析 :
① Context context 參數 : View 組件運行的上下文環境 , 通過該對象可以獲取當前主題 , 資源等 ;
② AttributeSet attrs 參數 : XML 佈局文件中的 View 組件標籤中的屬性值 ;
③ int defStyleAttr 參數 : 默認的 Style 風格 , 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效 ;
④ int defStyleRes 參數 : style 資源的 id 標識符 , 提供組件的默認值 , 只有當 defStyleAttr 參數是 0 時 , 或者主題中沒有 style 設置 ; 默認可以設置成 0 ;
/**
* 佈局文件中加載組件 , 並提供一個主題屬性屬性 , 或風格資源 ;
* 該構造方法允許組件在加載時使用自己的風格 ;
*
* 屬性設置優先級 ( 優先級從高到低 )
* 1. 佈局文件中的標籤屬性 AttributeSet
* 2. defStyleAttr 指定的默認風格
* 3. defStyleRes 指定的默認風格
* 4. 主題的屬性值
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
* @param defStyleAttr 默認的 Style 風格
* 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效
* @param defStyleRes style 資源的 id 標識符 , 提供組件的默認值 ,
* 只有當 defStyleAttr 參數是 0 時 , 或者主題中沒有 style 設置 ;
* 默認可以設置成 0 ;
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
二、代碼示例
package kim.hsl.lgl;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
/**
* 長圖展示自定義 View 組件
*
*/
public class LongImageView extends View {
/**
* 代碼中創建組件調用該方法
* @param context View 組件運行的上下文對象 , 一般是 Activity ,
* 可以通過該上下獲取當前主題 , 資源等
*/
public LongImageView(Context context) {
super(context);
}
/**
* 佈局文件中使用組件調用該方法 ;
* 當 View 組件從 XML 佈局文件中構造時 , 調用該方法
* 提供的 AttributeSet 屬性在 XML 文件中指定 ;
* 該方法使用默認的風格 defStyleAttr = 0 ,
* 該組件的屬性設置只有 Context 中的主題和 XML 中的屬性 ;
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
*/
public LongImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
/**
* 佈局文件中加載組件 , 並提供一個主題屬性風格 ;
* View 組件使用該構造方法 , 從佈局中加載時 , 允許使用一個特定風格 ;
* 如 : 按鈕類的構造函數會傳入 defStyleAttr = R.attr.buttonStyle 風格作爲參數 ;
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
* @param defStyleAttr 默認的 Style 風格
* 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效
*/
public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 佈局文件中加載組件 , 並提供一個主題屬性屬性 , 或風格資源 ;
* 該構造方法允許組件在加載時使用自己的風格 ;
*
* 屬性設置優先級 ( 優先級從高到低 )
* 1. 佈局文件中的標籤屬性 AttributeSet
* 2. defStyleAttr 指定的默認風格
* 3. defStyleRes 指定的默認風格
* 4. 主題的屬性值
*
* @param context View 組件運行的上下文環境 ,
* 通過該對象可以獲取當前主題 , 資源等
* @param attrs XML 佈局文件中的 View 組件標籤中的屬性值
* @param defStyleAttr 默認的 Style 風格
* 當前的應用 Application 或 Activity 設置了風格主題後 , 才生效
* @param defStyleRes style 資源的 id 標識符 , 提供組件的默認值 ,
* 只有當 defStyleAttr 參數是 0 時 , 或者主題中沒有 style 設置 ;
* 默認可以設置成 0 ;
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
三、源碼及資源下載
源碼及資源下載地址 :
-
① GitHub 工程地址 : Long_Graph_Loading
-
② LongImageView.java 主界面代碼地址 : LongImageView.java , 這是上述示自定義組件代碼 ;