Android自定义View

来源:互联网 发布:三明职业技术学院网络 编辑:程序博客网 时间:2024/06/03 13:02

本文转自http://blog.csdn.net/lmj623565791/article/details/24252901


一、自定义View的步骤

(1)自定义View的属性

(2)在View的构造方法中获得自定义的属性

(3)重写onMeasure

(4)重写oDraw


二、自定义View的属性

首先在res/values下建立一个attrs.xml,在里面定义我们的属性和样式

例如:

<?xml version="1.0" encoding="utf-8"?><resources>    <attr name="titleText" format="string" />    <attr name="titleTextColor" format="color" />    <attr name="titleTextSize" format="dimension" />    <declare-styleable name="CustomTitleView">        <attr name="titleText" />        <attr name="titleTextColor" />        <attr name="titleTextSize" />    </declare-styleable>

其中format是值该属性的取值类型

(1)reference:资源ID

(2)color:颜色值

(3)boolean:布尔值

(4)dimension:尺寸值

(5)float:浮点型

(6)integer:整形

(7)string:字符串

(8)fraction:百分比

(9)enum:枚举值

(10)flag:位或者运算


之后再布局中声明自定义view,并引入命名空间


三、在构造方法中获取自定义样式

<pre name="code" class="java">TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.RadioViewPager);        String one = (String) attr.getText(R.styleable.RadioViewPager_rdone);        String two = (String) attr.getText(R.styleable.RadioViewPager_rdtwo);        String three = (String) attr.getText(R.styleable.RadioViewPager_rdthree);        String title = (String) attr.getText(R.styleable.RadioViewPager_rdtext);        String infService = Context.LAYOUT_INFLATER_SERVICE;        LayoutInflater inflater = (LayoutInflater) context                .getSystemService(infService);        inflater.inflate(R.layout.radio_viewpager, this);        rd_one.setText(one);        rd_two.setText(two);        if (three.equals("1")) {            rd_three.setText("");            rd_three.setVisibility(GONE);            last_image.setVisibility(GONE);        } else {            rd_three.setText(three);        }        tv_title.setText(title);        onCheckedChanged();        attr.recycle();


四、重写onDraw,onMeasure调用系统提供的

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onDraw(Canvas canvas){mPaint.setColor(Color.YELLOW);canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);mPaint.setColor(mTitleTextColor);canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);}

五、onMeasure()(不一定要有)

在这之前先了解MeasureSpec的三种类型

(1)EXACTLY:一般是设置了明确的值或者是match_parent

(2)AT_MOST:表示子布局限制在一个最大值,一般为wrap_content

(3)UNSPECIFIED:表示子布局想要多大就多大,很少使用

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int width;int height ;if (widthMode == MeasureSpec.EXACTLY){width = widthSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textWidth = mBounds.width();int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());width = desired;}if (heightMode == MeasureSpec.EXACTLY){height = heightSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textHeight = mBounds.height();int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());height = desired;}


0 0