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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android自定义View —— TypedArray

發(fā)布時間:2023/11/27 生活经验 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自定义View —— TypedArray 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在上一篇中Android 自定義View Canvas —— Bitmap寫到了TypedArray 這個屬性

下面也簡單的說一下TypedArray的使用

TypedArray 的作用:

用于從該結(jié)構(gòu)檢索值的索引對應于給定給獲取StyledAttributes的屬性的位置。

TypedArray? 使用 obtainStyledAttributes? 檢索此上下文主題中的樣式化屬性信息

下面使用TypedArray 來 顯示一張圖片

自定義TestView

 public class TestView extends View {// paint 初始化private Paint paint = new Paint();private Bitmap testBitmap;private int testBitmapResID;public TestView(Context context) {super(context);}public TestView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs);}public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {// 檢索此上下文主題中的樣式化屬性信息TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindView);testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);// 這里styleable 和面的跟的是attrs.xml中declare-styleable 里面的name和attr的nametestBitmapResID = typedArray.getResourceId(R.styleable.FindView_test_image,R.drawable.ic_launcher_background);// 回收TypedArray,供以后的調(diào)用方重新使用, 這個不要忘記添加typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setStyle(Paint.Style.FILL);testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);canvas.drawBitmap(testBitmap, 100, 100, paint);}}

attrs.xml?

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="FindView"><attr name="test_image" format="reference" /></declare-styleable>
</resources>

布局中使用attrs.xml 了里面的 attr 里面的name test_name 來顯示圖片

<?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-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.hly.view.TestViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"app:test_image="@mipmap/girl" /></LinearLayout>

好了一個簡單的繪制圖片就完成了,運行看下效果

上面的demo 中 attrs 里面的 format使用的是 "reference" 這個表示引用,參考某一資源ID,它還有很多其他的屬性

如下:

reference :表示引用,參考某一資源ID
string :表示字符串
color :表示顏色值
dimension :表示尺寸值
boolean :表示布爾值
integer :表示整型值
float :表示浮點值
fraction :表示百分數(shù)
enum :表示枚舉值
flag :表示位運算

下面在寫一個text 的demo 使用下其中的幾個屬性

public class TestView extends View {// paint 初始化private Paint paint = new Paint();private String text = "";public TestView(Context context) {super(context);}public TestView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs);}public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {// 檢索此上下文主題中的樣式化屬性信息TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindView);// 這里styleable 和面的跟的是attrs.xml中declare-styleable 里面的name和attr的nametext = typedArray.getString(R.styleable.FindView_test_text);int textColor = typedArray.getColor(R.styleable.FindView_test_color,Color.parseColor("#00d8a0"));float textSize = typedArray.getDimension(R.styleable.FindView_test_size, 33);paint.setTextSize(textSize);//畫筆字體大小設置paint.setColor(textColor);//畫筆的顏色// 回收TypedArray,供以后的調(diào)用方重新使用, 這個不要忘記添加typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setStyle(Paint.Style.FILL);canvas.drawText(text, 100, 100, paint);}}

attrs.xml 代碼如下

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="FindView"><attr name="test_text" format="string" /><attr name="test_color" format="color" /><attr name="test_size" format="dimension" /></declare-styleable>
</resources>

布局中的代碼:

<?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-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.hly.view.TestViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"app:test_color="@android:color/holo_red_dark"app:test_size="40dp"app:test_text="我是胡小牧" /></LinearLayout>

實現(xiàn)的效果:

總結(jié)

以上是生活随笔為你收集整理的Android自定义View —— TypedArray的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。