自定义控件_day03

自定义控件的实战(参考网上的资料 比着葫芦画瓢)

自定义属性

在res/values下面新建attrs.xml属性文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--name 是自定义属性名,一般采用驼峰命名,可以随意。 format 是属性的单位-->
    <attr name="titleSize" format="dimension"></attr>
    <attr name="titleText" format="string"></attr>
    <attr name="titleColor" format="color"></attr>
    <attr name="titleBackgroundColor" format="color"></attr>

    <!--name 是自定义控件的类名-->
    <declare-styleable name="MyCustomView">
        <attr name="titleSize"></attr>
        <attr name="titleText"></attr>
        <attr name="titleColor"></attr>
        <attr name="titleBackgroundColor"></attr>
    </declare-styleable>
</resources>

如上面的xml文件第一部分是公共的属性,第二部分是自定义控件MyCustomView的主题样式,该主题样式里的属性必须包含在公共属性里面。
言外之意就是公共属性可以被多个自定义控件主题样式使用。

自定义控件的实现

package com.example.mytextview;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyTextView extends View {
      private static final String TAG = "MyTextView";
        private static final boolean DEBUG = false;
        private String titleText = "Hello world";

        private int titleColor = Color.BLACK;
        private int titleBackgroundColor = Color.WHITE;
        private int titleSize = 16;

        private Paint mPaint;
        private Rect mBound;

        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);

            final Resources.Theme theme = context.getTheme();
            TypedArray attrArray = theme.obtainStyledAttributes(attrs,
                    R.styleable.MyTextView, defStyleAttr, 0);
            if (null != attrArray) {
                int conunt = attrArray.getIndexCount();
                for (int i = 0; i < conunt; i++) {
                    int attr = attrArray.getIndex(i);
                    switch (attr) {
                        case R.styleable.MyTextView_titleColor:
                            titleColor = attrArray.getColor(attr, Color.BLACK);
                            break;
                        case R.styleable.MyTextView_titleSize:
                            titleSize = attrArray.getDimensionPixelSize(attr, titleSize);
                            break;
                        case R.styleable.MyTextView_titleText:
                            titleText = attrArray.getString(attr);
                            break;
                        case R.styleable.MyTextView_titleBackgroundColor:
                            titleBackgroundColor = attrArray.getColor(attr, Color.WHITE);
                            break;
                    }
                }
                attrArray.recycle();
                init();
            }
        }

        /**
         * 初始化
         */
        private void init() {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setTextSize(titleSize);
            /**
             * 得到自定义View的titleText内容的宽和高
             */
            mBound = new Rect();
            mPaint.getTextBounds(titleText, 0, titleText.length(), mBound);
        }


        @Override
        protected void onDraw(Canvas canvas) {
            mPaint.setColor(titleBackgroundColor);
            canvas.drawCircle(getWidth() / 2f, getWidth() / 2f, getWidth() / 2f, mPaint);
            mPaint.setColor(titleColor);
            canvas.drawText(titleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            /**
             * 测量模式
             */
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            /**
             * 父布局希望子布局的大小,如果布局里面设置的是固定值,这里取布局里面的固定值和父布局大小值中的最小值.
             * 如果设置的是match_parent,则取父布局的大小
             */
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            int width;
            int height;
            Rect mBounds = new Rect();
            /**
             *  MeasureSpec.EXACTLY:父视图希望子视图的大小是specSize中指定的大小;一般是设置了明确的值或者是MATCH_PARENT
                MeasureSpec.AT_MOST:子视图的大小最多是specSize中的大小;表示子布局限制在一个最大值内,一般为WARP_CONTENT
                MeasureSpec.UNSPECIFIED:父视图不对子视图施加任何限制,子视图可以得到任意想要的大小;表示子布局想要多大就多大,很少使用。
             */
            if (widthMode == MeasureSpec.EXACTLY) {
                width = widthSize;
            } else {
                mPaint.setTextSize(titleSize);
                mPaint.getTextBounds(titleText, 0, titleText.length(), mBounds);
                float textWidth = mBounds.width();
                int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
                width = desired;
            }

            if (heightMode == MeasureSpec.EXACTLY) {
                height = heightSize;
            } else {

                height = width;
            }
            /**
             * 最后调用父类方法,把View的大小告诉父布局。
             */
            setMeasuredDimension(width, height);
        }

}

自定义文件的引用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <com.example.mytextview.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:titleColor="@android:color/black"
        custom:titleSize="25sp"
        custom:titleBackgroundColor="#ff0000"
        custom:titleText="自定义的View" />
</RelativeLayout>
xmlns后边的custom这个名字可以任意取,属性设置就是使用这个名字开头。

效果图

图片名称

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值