日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【Android 内存优化】自定义组件长图组件 ( 自定义组件构造方法 )

發布時間:2025/6/17 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 内存优化】自定义组件长图组件 ( 自定义组件构造方法 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、自定義組件構造方法簡介
    • 1、View(Context context) 構造函數
    • 2、View(Context context, @Nullable AttributeSet attrs)
    • 3、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) 構造函數
    • 4、View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) 構造函數
  • 二、代碼示例
  • 三、源碼及資源下載



官方文檔 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 , 這是上述示自定義組件代碼 ;

總結

以上是生活随笔為你收集整理的【Android 内存优化】自定义组件长图组件 ( 自定义组件构造方法 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。