自定义Android View属性
2015-11-12 12:33
260 查看
自定义MyTextView
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr format="string" name="text"/>
<attr format="dimension" name="textsize"/>
<attr format="color" name="backGround"/>
<declare-styleable name="MyTextView">
<attr name="text"/>
<attr name="textsize"/>
<attr name="backGround"/>
</declare-styleable>
</resources>
其中我自定义了text textsize backgroud 三个属性,format的作用为定义属性值的类型有String color blooean float,intager,dimension等几种类型
declare-styleable的作用:styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作
public class MyTextView extends View {
// 定以属性
private String mText;
private int mBackground;
private int mTextSize;
private Rect mBunds;
private Paint mPaint;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, com.example.asasas.R.styleable.MyTextView,
defStyleAttr, 0);
for (int i = 0; i < a.getIndexCount(); i++) {
int array = a.getIndex(i);
switch (array) {
case R.styleable.MyTextView_text:
mText = a.getString(array);
break;
case R.styleable.MyTextView_textsize:
mTextSize = a.getDimensionPixelSize(array, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
16, getResources().getDisplayMetrics()));
mBackground = a.getColor(array, Color.BLACK);
break;
}
}
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mBunds = new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
}
TypeArray的作用:
TypedArray其实是用来简化我们的工作的,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。
int width;
int height;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = getPaddingLeft() + getPaddingRight() + widthSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingLeft() + mBunds.width() + getPaddingRight());
width = desired;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode == MeasureSpec.EXACTLY) {
height = getPaddingTop() + getPaddingBottom() + heightSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingTop() + mBunds.height() + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
重写OnDrow();方法画出控件
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
mPaint.setColor(mBackground);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint = new Paint();
canvas.drawText(mText, getWidth() / 2 - mBunds.width() / 2, getHeight() / 3ff7 2 + mBunds.height() / 2, mPaint);
如何自定义一个简单的View.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr format="string" name="text"/>
<attr format="dimension" name="textsize"/>
<attr format="color" name="backGround"/>
<declare-styleable name="MyTextView">
<attr name="text"/>
<attr name="textsize"/>
<attr name="backGround"/>
</declare-styleable>
</resources>
其中我自定义了text textsize backgroud 三个属性,format的作用为定义属性值的类型有String color blooean float,intager,dimension等几种类型
declare-styleable的作用:styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作
第二步构造函数继承View 重写OnMeasure();OnDrow();方法;
//在上面构造方法里面我们获得我们自定义的样式;public class MyTextView extends View {
// 定以属性
private String mText;
private int mBackground;
private int mTextSize;
private Rect mBunds;
private Paint mPaint;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, com.example.asasas.R.styleable.MyTextView,
defStyleAttr, 0);
for (int i = 0; i < a.getIndexCount(); i++) {
int array = a.getIndex(i);
switch (array) {
case R.styleable.MyTextView_text:
mText = a.getString(array);
break;
case R.styleable.MyTextView_textsize:
mTextSize = a.getDimensionPixelSize(array, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
16, getResources().getDisplayMetrics()));
break;
sp与dp的互换
case R.styleable.MyTextView_backGround:mBackground = a.getColor(array, Color.BLACK);
break;
}
}
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mBunds = new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
}
TypeArray的作用:
TypedArray其实是用来简化我们的工作的,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。
AttributeSet的作用
AttributeSet中保存的是该View声明的所有的属性以通过它去获取(自定义的)属性
重写OnMeasure()获得大小的大小@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stubint width;
int height;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = getPaddingLeft() + getPaddingRight() + widthSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingLeft() + mBunds.width() + getPaddingRight());
width = desired;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode == MeasureSpec.EXACTLY) {
height = getPaddingTop() + getPaddingBottom() + heightSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingTop() + mBunds.height() + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
重写OnDrow();方法画出控件
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
mPaint.setColor(mBackground);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint = new Paint();
canvas.drawText(mText, getWidth() / 2 - mBunds.width() / 2, getHeight() / 3ff7 2 + mBunds.height() / 2, mPaint);
}
第三步:在布局文件中使用自定义的控件.
<com.example.asasas.View.MyTextView
android:layout_height="43dp"
android:layout_width="32dp" app:textsize="26sp"
app:text="测试1"
app:backGround="#578"
android:padding="10dp"/>
<com.example.asasas.View.MyTextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:textsize="45dp" app:text="测试2"
app:backGround="#713"
android:padding="15dp"
android:layout_centerInParent="true"/>
相关文章推荐
- HenCoder Android 自定义 View 1-6:属性动画(上手篇)
- Android自定义View的自定义属性atrrs.xml解析
- Android 自定义View自定义属性的声明
- Android 自定义view设置xml属性
- Android自定义View之属性
- Android 自定义View 属性和命名空间
- android开发之自定义属性、View和使用
- Android解析自定义属性的XML实现底部导航栏TabSelectedView,实现灵活的配置扩展
- Android绘图机制(一)——自定义View的基础属性和方法
- 转载Android 自定义View属性相关细节
- Android自定义View读取XML属性详解
- Android自定义View以及layout属性全攻略
- Android 自定义View时使用TypedArray配置样式属性详细介绍
- Android 自定义View - Paint属性大全
- Android读取自定义View属性
- Android 自定义View并添加属性
- android自定义view属性
- Android自定义View之属性解析
- android在自定义view中自定义属性的方法
- Android 自定义View实现圆形进度条 深入理解onDraw和onMeasure及自定义属性