Android自定义view:自定义属性 TypedArray
Posted Mars-xq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自定义view:自定义属性 TypedArray相关的知识,希望对你有一定的参考价值。
定义:
<declare-styleable name="MyView">
<attr name="vColor" format="color" />
<attr name="vDrawable" format="color|reference" />
<attr name="vDimen" format="dimension" />
<attr name="vReference" format="reference" />
<attr name="vBoolean" format="boolean" />
<attr name="vFloat" format="float" />
<attr name="vInteger" format="integer" />
<attr name="vString" format="string" />
<attr name="vFraction" format="fraction" />
<attr name="vType" format="enum">
<enum name="type1" value="1" />
<enum name="type2" value="2" />
<enum name="type3" value="3" />
</attr>
<attr name="vFlag" format="flags">
<flag name="flag1" value="0x1" />
<flag name="flag2" value="0x2" />
<flag name="flag3" value="0x3" />
</attr>
</declare-styleable>
attrs中的format属性 | TypedArray对应方法 | 返回 | 备注 |
---|---|---|---|
reference | getInt | int | |
color | getColor | int | |
boolean | getBoolean | boolean | |
dimension | getDimension | float | |
float | getFloat | float | |
integer | getInteger,getInt | Integer,int | |
string | getString | String | 必须是资源文件中的字符串 |
fraction | getFraction | float | |
enum | getInt,getInteger,getFloat,getString | int,Integer,float,String | 返回的全是value的值 |
flag | getInt,getFloat,getString | int,float,string | 返回value值的或运算值 |
声明
public class MyView extends View
private static final String TAG = MyView.class.getSimpleName();
public MyView(Context context)
this(context, null);
public MyView(Context context, @Nullable AttributeSet attrs)
this(context, attrs, -1);
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int vColor = typedArray.getColor(R.styleable.MyView_vColor, getResources().getColor(R.color.black));
// ColorDrawable / BitmapDrawable
Drawable vDrawable = typedArray.getDrawable(R.styleable.MyView_vDrawable);
float vDimen = typedArray.getDimension(R.styleable.MyView_vDimen, dp2px(20));
float vFloat = typedArray.getFloat(R.styleable.MyView_vFloat, -1F);
int vInteger = typedArray.getInteger(R.styleable.MyView_vInteger, -1);
boolean vBoolean = typedArray.getBoolean(R.styleable.MyView_vBoolean, false);
String vString = typedArray.getString(R.styleable.MyView_vString);
int vReference = typedArray.getResourceId(R.styleable.MyView_vReference, R.mipmap.ic_launcher);
int vType = typedArray.getInteger(R.styleable.MyView_vType, -1);
int vFlag = typedArray.getInt(R.styleable.MyView_vFlag, 0);
Log.e(TAG, "MyView:vColor===============> " + vColor);
Log.e(TAG, "MyView:vDrawable===============> " + vDrawable);
Log.e(TAG, "MyView:vDimen===============> " + vDimen);
Log.e(TAG, "MyView:vFloat===============> " + vFloat);
Log.e(TAG, "MyView:vInteger===============> " + vInteger);
Log.e(TAG, "MyView:vBoolean===============> " + vBoolean);
Log.e(TAG, "MyView:vString===============> " + vString);
Log.e(TAG, "MyView:vReference===============> " + vReference);
Log.e(TAG, "MyView:vType===============> " + vType);
Log.e(TAG, "MyView:vFlag===============> " + vFlag);
typedArray.recycle();
/**
* dp转px
*
* @param dp dp值
* @return px值
*/
public int dp2px(float dp)
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
/**
* sp转px
*
* @param sp sp值
* @return px值
*/
private int sp2px(float sp)
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
使用
<com.xq.myapplication.view.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:vBoolean="true"
app:vColor="#000000"
app:vDimen="18dp"
app:vDrawable="@android:color/white"
app:vFlag="flag1|flag2|flag3"
app:vFloat="3.14"
app:vInteger="10086"
app:vReference="@mipmap/ic_launcher"
app:vString="自定义属性"
app:vType="type3" />
drawable 的绘制
void drawDrawable(Canvas canvas, Rect bound,Drawable drawable)
drawable.setBounds(bounds);
drawable.draw(canvas);
以上是关于Android自定义view:自定义属性 TypedArray的主要内容,如果未能解决你的问题,请参考以下文章