Android 自定义View与自定义属性
- 继承自View创建自定义控件
- 测量视图大小
- 绘制视图内容
- 如果需要自定义属性,在values/attrs.xml中定义属性
- 在代码中获取布局中设置的自定义属性的值
1继承自View创建自定义控件
- 比如我们先自定义一个View 让他去显示一张图片,首先我们创建一个SimpleImageView 继承自View 定义一些需要的成员属性
public class SimpleImageView extends View {
// 初始化画笔
private Paint mPaint;
// 初始化 一个资源文件
private Drawable mDrawable;
// 控件的高度和宽度
private int mWidth;
private int mHeight;
//一个bitmap 对象
private Bitmap mbitmap;
private String text;
public SimpleImageView(Context context) {
this(context,null);
}
public SimpleImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
}
2获取这张图片
给自定义控件添加自定义属性
//在attr.xml中添加自定义属性集 SimpleImageView 表示自定义属性集的名称 src 表示具体属性的名称 format 表示类型 具体类型在下面会给到
<declare-styleable name="SimpleImageView">
<attr name="src" format="reference" />
<attr name="text" format="string"/>
</declare-styleable>
布局文件中设置自定义属性的值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:img="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.administrator.mytext.SimpleImageView
android:layout_width="100dp"
img:src="@mipmap/h1"
android:layout_height="100dp"
img:text="医院图片1"
/>
</LinearLayout>
在代码里面获取属性的值
// 获取自定义属性
private void initAttrs(AttributeSet attrs) {
if (attrs!=null){
TypedArray array = null;
try {
// 通过name 获取属性集
array = getContext().obtainStyledAttributes(attrs,R.styleable.SimpleImageView);
// 通过name 获取属性集里面的具体属性的具体值(这里是一个图片)
mDrawable = array.getDrawable(R.styleable.SimpleImageView_src);
text = array.getString(R.styleable.SimpleImageView_text);
// 这个方法 是用来获取图片的具体高宽 然后设置控件的具体高宽(类似于设置 wrap_content)
measureDrawable();
}catch (Exception e){
}finally {
if (array!=null){
array.recycle();
}
}
}
}
private void measureDrawable(){
if (mDrawable==null){
throw new RuntimeException("src 不能为null");
}
mWidth = mDrawable.getIntrinsicWidth();
mHeight = mDrawable.getIntrinsicHeight();
}
3绘制视图的大小
我们要获取到布局文件中设置的控件的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 一般不这么用,这里图片有多大控件就有多大 在布局中设置控件的大小将失效
// setMeasuredDimension(mWidth,mHeight);
// 获取宽度的模式跟大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
// 获取高度的模式跟大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heith = MeasureSpec.getSize(heightMeasureSpec);
// 根据用户设置控件的属性(这里主要是控件的宽高模式和大小 ) 绘制出这张图片
setMeasuredDimension(measureWidth(widthMode,width),measureHeight(heightMode,heith));
}
private int measureWidth(int mode,int width){
switch (mode){
// 这两个模式 wrap_content
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
// 这个模式 就是 match_parent 或者设置具体的值
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
// 高度 同上
private int measureHeight(int mode,int height){
switch (mode){
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
4绘制视图
@Override
protected void onDraw(Canvas canvas) {
if (mbitmap ==null){
mbitmap = Bitmap.createScaledBitmap(BitmapUtils.drawableToBitmap(mDrawable),getMeasuredWidth(),getMeasuredHeight(),true);
}
canvas.drawBitmap(mbitmap,getLeft(),getTop(),mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(50);
canvas.drawText(text,getLeft(),getTop(),mPaint);
canvas.restore();
}
5完整代码
public class SimpleImageView extends View {
private Paint mPaint;
private Drawable mDrawable;
private int mWidth;
private int mHeight;
private Bitmap mbitmap;
private String text;
public SimpleImageView(Context context) {
this(context,null);
}
public SimpleImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
// 获取自定义属性
private void initAttrs(AttributeSet attrs) {
if (attrs!=null){
TypedArray array = null;
try {
// 通过name 获取属性集
array = getContext().obtainStyledAttributes(attrs,R.styleable.SimpleImageView);
// 通过name 获取属性集里面的具体属性的具体值(这里是一个图片)
mDrawable = array.getDrawable(R.styleable.SimpleImageView_src);
text = array.getString(R.styleable.SimpleImageView_text);
// 这个方法 是用来获取图片的具体高宽 然后设置控件的具体高宽(类似于设置 wrap_content)
measureDrawable();
}catch (Exception e){
}finally {
if (array!=null){
array.recycle();
}
}
}
}
private void measureDrawable(){
if (mDrawable==null){
throw new RuntimeException("src 不能为null");
}
mWidth = mDrawable.getIntrinsicWidth();
mHeight = mDrawable.getIntrinsicHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 一般不这么用,这里图片有多大控件就有多大 在布局中设置控件的大小将失效
// setMeasuredDimension(mWidth,mHeight);
// 获取宽度的模式跟大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
// 获取高度的模式跟大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heith = MeasureSpec.getSize(heightMeasureSpec);
// 根据用户设置控件的属性(这里主要是控件的宽高模式和大小 ) 绘制出这张图片
setMeasuredDimension(measureWidth(widthMode,width), measureHeight(heightMode,heith));
}
private int measureWidth(int mode,int width){
switch (mode){
// 这两个模式 wrap_content
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
// 这个模式 就是 match_parent 或者设置具体的值
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
// 高度 同上
private int measureHeight(int mode,int height){
switch (mode){
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
if (mbitmap ==null){
mbitmap = Bitmap.createScaledBitmap(BitmapUtils.drawableToBitmap(mDrawable),getMeasuredWidth(),getMeasuredHeight(),true);
}
canvas.drawBitmap(mbitmap,getLeft(),getTop(),mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(50);
canvas.drawText(text,getLeft(),getTop(),mPaint);
canvas.restore();
}
}
6 format值与使用方式
reference:源ID。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "src" format = "reference" /> </declare-styleable>
(2)属性使用:
<ImageView Android:layout_width = "42dip" android:layout_height = "42dip" android:src = "@drawable/图片ID" />
color:颜色值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "color" format = "color" /> </declare-styleable>
(2)属性使用:
<TextView android:layout_width = "42dip" android:layout_height = "42dip" android:color= "#000000" />
boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "boolean" format = "boolean" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" android:boolean = "true" />
dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "width" format = "dimension" /> </declare-styleable>
(2)属性使用:
<Button android:width = "42dip" android:height = "42dip" />
float:浮点值。
(1)属性定义:
<declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable>
(2)属性使用:
<alpha android:fromAlpha = "1.0" android:toAlpha = "0.7" />
integer:整型值。
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<animated-rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:drawable = "@drawable/图片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100" />
string:字符串。
(1)属性定义:
<declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
fraction:百分数。
(1)属性定义:
<declare-styleable name="RotateDrawable"> <attr name = "visible" /> <attr name = "fromDegrees" format = "float" /> <attr name = "toDegrees" format = "float" /> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<rotate xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = “@anim/动画ID”
android:fromDegrees = "0"
android:toDegrees = “360”
android:pivotX = "200%" android:pivotY = "300%"
android:duration = “5000”
android:repeatMode = "restart" android:repeatCount = "infinite" />
enum:枚举值。
(1)属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
(2)属性使用:
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </LinearLayout>
flag:位或运算。
(1)属性定义:
<declare-styleable name="名称"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> <flag name = "stateVisible" value = "4" /> <flag name = "stateAlwaysVisible" value = "5" /> <flag name = "adjustUnspecified" value = "0x00" /> <flag name = "adjustResize" value = "0x10" /> <flag name = "adjustPan" value = "0x20" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable>
(2)属性使用:
<activity android:name = ".StyleAndThemeActivity" android:label = "@string/app_name" android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable>
(2)属性使用:
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID|#00FF00" />