Android自定义View —— TypedArray
在上一篇中Android 自定義View Canvas —— Bitmap寫到了TypedArray 這個屬性
下面也簡單的說一下TypedArray的使用
TypedArray 的作用:
用于從該結構檢索值的索引對應于給定給獲取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,供以后的調用方重新使用, 這個不要忘記添加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 :表示百分數
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,供以后的調用方重新使用, 這個不要忘記添加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>
實現的效果:
總結
以上是生活随笔為你收集整理的Android自定义View —— TypedArray的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄金多少钱回收啊?
- 下一篇: 小香风连衣裙怎么搭配好看??