Android中的Serializable、Parcelable">Android中的Serializable、Parcelable
840
2025-03-31
文章目錄
一、自定義組件構(gòu)造方法簡(jiǎn)介
1、View(Context context) 構(gòu)造函數(shù)
2、View(Context context, @Nullable AttributeSet attrs)
3、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) 構(gòu)造函數(shù)
4、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) 構(gòu)造函數(shù)
二、代碼示例
三、源碼及資源下載
官方文檔 API : BitmapRegionDecoder
一、自定義組件構(gòu)造方法簡(jiǎn)介
1、View(Context context) 構(gòu)造函數(shù)
在代碼中創(chuàng)建 View 對(duì)象 , 就會(huì)調(diào)用該構(gòu)造函數(shù) ,
其中 Context context 參數(shù)是組件運(yùn)行的環(huán)境 , 可以從該 Context 對(duì)象中獲取當(dāng)前的主題 , 資源等 ;
/** * 代碼中創(chuàng)建組件調(diào)用該方法 * @param context View 組件運(yùn)行的上下文對(duì)象 , 一般是 Activity , * 可以通過(guò)該上下獲取當(dāng)前主題 , 資源等 */ public LongImageView(Context context) { super(context); }
1
2
3
4
5
6
7
8
2、View(Context context, @Nullable AttributeSet attrs)
1 . 構(gòu)造函數(shù)簡(jiǎn)介 :
① 構(gòu)造函數(shù)使用時(shí)機(jī) :
布局文件中使用組件調(diào)用該方法 , 當(dāng) View 組件從 XML 布局文件中構(gòu)造時(shí) , 調(diào)用該方法 ;
② 屬性指定 :
提供的 AttributeSet 屬性在 XML 文件中指定 ;
③ 默認(rèn)風(fēng)格 :
該方法使用默認(rèn)的風(fēng)格 defStyleAttr = 0 , 該組件的屬性設(shè)置只有 Context 中的主題和 XML 中的屬性 ;
2 . 參數(shù)分析 :
① Context context 參數(shù) :
View 組件運(yùn)行的上下文環(huán)境 , 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 ;
② AttributeSet attrs 參數(shù) :
XML 布局文件中的 View 組件標(biāo)簽中的屬性值 ;
/** * 布局文件中使用組件調(diào)用該方法 ; * 當(dāng) View 組件從 XML 布局文件中構(gòu)造時(shí) , 調(diào)用該方法 * 提供的 AttributeSet 屬性在 XML 文件中指定 ; * 該方法使用默認(rèn)的風(fēng)格 defStyleAttr = 0 , * 該組件的屬性設(shè)置只有 Context 中的主題和 XML 中的屬性 ; * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 */ public LongImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) 構(gòu)造函數(shù)
1 . 構(gòu)造函數(shù)簡(jiǎn)介 :
① 構(gòu)造函數(shù)使用時(shí)機(jī) : 布局文件中使用組件調(diào)用該方法 , 當(dāng) View 組件從 XML 布局文件中構(gòu)造時(shí) , 調(diào)用該方法 ;
② 主題風(fēng)格 : 從 XML 中加載組件同時(shí)還會(huì)提供一個(gè)主題屬性風(fēng)格 ;
③ 屬性指定 : 提供的 AttributeSet 屬性在 XML 文件中指定 ;
④ 主題風(fēng)格 : View 組件使用該構(gòu)造方法 , 從布局中加載時(shí) , 允許使用一個(gè)特定風(fēng)格 ;
⑤ 示例 : 如 : 按鈕類的構(gòu)造函數(shù)會(huì)傳入 defStyleAttr = R.attr.buttonStyle 風(fēng)格作為參數(shù) ;
2 . 參數(shù)分析 :
① Context context 參數(shù) : View 組件運(yùn)行的上下文環(huán)境 , 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 ;
② AttributeSet attrs 參數(shù) : XML 布局文件中的 View 組件標(biāo)簽中的屬性值 ;
③ int defStyleAttr 參數(shù) : 默認(rèn)的 Style 風(fēng)格 , 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 ;
/** * 布局文件中加載組件 , 并提供一個(gè)主題屬性風(fēng)格 ; * View 組件使用該構(gòu)造方法 , 從布局中加載時(shí) , 允許使用一個(gè)特定風(fēng)格 ; * 如 : 按鈕類的構(gòu)造函數(shù)會(huì)傳入 defStyleAttr = R.attr.buttonStyle 風(fēng)格作為參數(shù) ; * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 * @param defStyleAttr 默認(rèn)的 Style 風(fēng)格 * 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 */ public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) 構(gòu)造函數(shù)
1 . 版本兼容 : Android 5.0(API 級(jí)別 21)LOLLIPOP 版本加入的構(gòu)造函數(shù) , 定義該構(gòu)造函數(shù) , 必須加上 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 注解 ;
2 . 構(gòu)造函數(shù)簡(jiǎn)介 :
① 構(gòu)造函數(shù)使用時(shí)機(jī) : 布局文件中使用組件調(diào)用該方法 , 當(dāng) View 組件從 XML 布局文件中構(gòu)造時(shí) , 調(diào)用該方法 ;
② 主題風(fēng)格或資源 : 從 XML 中加載組件同時(shí)還會(huì)提供一個(gè)主題屬性風(fēng)格 , 或資源 ;
③ 屬性指定 : 提供的 AttributeSet 屬性在 XML 文件中指定 ;
④ 主題風(fēng)格 : View 組件使用該構(gòu)造方法 , 從布局中加載時(shí) , 允許使用一個(gè)特定風(fēng)格 ;
⑤ 示例 : 如 : 按鈕類的構(gòu)造函數(shù)會(huì)傳入 defStyleAttr = R.attr.buttonStyle 風(fēng)格作為參數(shù) ;
3 . 屬性設(shè)置優(yōu)先級(jí) ( 優(yōu)先級(jí)從高到低 ) :
布局文件中的標(biāo)簽屬性 AttributeSet
defStyleAttr 指定的默認(rèn)風(fēng)格
defStyleRes 指定的默認(rèn)風(fēng)格
主題的屬性值
4 . 參數(shù)分析 :
① Context context 參數(shù) : View 組件運(yùn)行的上下文環(huán)境 , 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 ;
② AttributeSet attrs 參數(shù) : XML 布局文件中的 View 組件標(biāo)簽中的屬性值 ;
③ int defStyleAttr 參數(shù) : 默認(rèn)的 Style 風(fēng)格 , 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 ;
④ int defStyleRes 參數(shù) :
style 資源的 id 標(biāo)識(shí)符 , 提供組件的默認(rèn)值 , 只有當(dāng) defStyleAttr 參數(shù)是 0 時(shí) , 或者主題中沒(méi)有 style 設(shè)置 ; 默認(rèn)可以設(shè)置成 0 ;
/** * 布局文件中加載組件 , 并提供一個(gè)主題屬性屬性 , 或風(fēng)格資源 ; * 該構(gòu)造方法允許組件在加載時(shí)使用自己的風(fēng)格 ; * * 屬性設(shè)置優(yōu)先級(jí) ( 優(yōu)先級(jí)從高到低 ) * 1. 布局文件中的標(biāo)簽屬性 AttributeSet * 2. defStyleAttr 指定的默認(rèn)風(fēng)格 * 3. defStyleRes 指定的默認(rèn)風(fēng)格 * 4. 主題的屬性值 * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 * @param defStyleAttr 默認(rèn)的 Style 風(fēng)格 * 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 * @param defStyleRes style 資源的 id 標(biāo)識(shí)符 , 提供組件的默認(rèn)值 , * 只有當(dāng) defStyleAttr 參數(shù)是 0 時(shí) , 或者主題中沒(méi)有 style 設(shè)置 ; * 默認(rèn)可以設(shè)置成 0 ; */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
二、代碼示例
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; /** * 長(zhǎng)圖展示自定義 View 組件 * */ public class LongImageView extends View { /** * 代碼中創(chuàng)建組件調(diào)用該方法 * @param context View 組件運(yùn)行的上下文對(duì)象 , 一般是 Activity , * 可以通過(guò)該上下獲取當(dāng)前主題 , 資源等 */ public LongImageView(Context context) { super(context); } /** * 布局文件中使用組件調(diào)用該方法 ; * 當(dāng) View 組件從 XML 布局文件中構(gòu)造時(shí) , 調(diào)用該方法 * 提供的 AttributeSet 屬性在 XML 文件中指定 ; * 該方法使用默認(rèn)的風(fēng)格 defStyleAttr = 0 , * 該組件的屬性設(shè)置只有 Context 中的主題和 XML 中的屬性 ; * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 */ public LongImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } /** * 布局文件中加載組件 , 并提供一個(gè)主題屬性風(fēng)格 ; * View 組件使用該構(gòu)造方法 , 從布局中加載時(shí) , 允許使用一個(gè)特定風(fēng)格 ; * 如 : 按鈕類的構(gòu)造函數(shù)會(huì)傳入 defStyleAttr = R.attr.buttonStyle 風(fēng)格作為參數(shù) ; * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 * @param defStyleAttr 默認(rèn)的 Style 風(fēng)格 * 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 */ public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 布局文件中加載組件 , 并提供一個(gè)主題屬性屬性 , 或風(fēng)格資源 ; * 該構(gòu)造方法允許組件在加載時(shí)使用自己的風(fēng)格 ; * * 屬性設(shè)置優(yōu)先級(jí) ( 優(yōu)先級(jí)從高到低 ) * 1. 布局文件中的標(biāo)簽屬性 AttributeSet * 2. defStyleAttr 指定的默認(rèn)風(fēng)格 * 3. defStyleRes 指定的默認(rèn)風(fēng)格 * 4. 主題的屬性值 * * @param context View 組件運(yùn)行的上下文環(huán)境 , * 通過(guò)該對(duì)象可以獲取當(dāng)前主題 , 資源等 * @param attrs XML 布局文件中的 View 組件標(biāo)簽中的屬性值 * @param defStyleAttr 默認(rèn)的 Style 風(fēng)格 * 當(dāng)前的應(yīng)用 Application 或 Activity 設(shè)置了風(fēng)格主題后 , 才生效 * @param defStyleRes style 資源的 id 標(biāo)識(shí)符 , 提供組件的默認(rèn)值 , * 只有當(dāng) defStyleAttr 參數(shù)是 0 時(shí) , 或者主題中沒(méi)有 style 設(shè)置 ; * 默認(rèn)可以設(shè)置成 0 ; */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
三、源碼及資源下載
源碼及資源- :
① GitHub 工程地址 : Long_Graph_Loading
② LongImageView.java 主界面代碼地址 : LongImageView.java , 這是上述示自定義組件代碼 ;
Android XML
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。