自定义view点击生成随机数

来源:互联网 发布:网络的利与弊素材 编辑:程序博客网 时间:2024/06/05 03:36
首先在valuse文件夹下创建一个命名为attrs的文件

<resources>

    <attr name="titleText" format="string" />
    <attr name="titleTextColor" format="color" />
    <attr name="titleTextSize" format="dimension" />

    <declare-styleable name="MyView">
        <attr name="titleText" />
        <attr name="titleTextColor" />
        <attr name="titleTextSize" />
    </declare-styleable>

</resources>

像这样 自定义属性名称 format 诠释分类 确定属性

之后再布局文件中中创建你的自定义view

    xmlns:zzp="http://schemas.android.com/apk/res-auto"

不要忘了这个 接下来的属性就可以自己定义了

像这样 :zzp:titleText="15155"        zzp:titleTextColor="#ff0000"        zzp:titleTextSize="40sp"
自定义属性整理好之后,在自定义view中同步

public class MyView extends View{

    private String mTitleText;
    private int mTitleColor;
    private int mTitleSize;

    private Rect mBound;
    private Paint mpaint;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a=context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyView,defStyleAttr,0);
        int n=a.getIndexCount();

判断属性信息

        for(int i=0;i<n;i++){

            int attr=a.getIndex(i);
            switch (attr)
            {
                case R.styleable.MyView_titleText:
                    mTitleText=a.getString(attr);
                    break;
                case R.styleable.MyView_titleTextColor:
                    mTitleColor=a.getColor(attr, Color.RED);
                    break;
                case R.styleable.MyView_titleTextSize:
                    mTitleSize=a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()));
                    break;
            }

        }

释放资源
        a.recycle();

属性设置好 就可以初始化画笔  进行我们想要绘制的图形

        mpaint=new Paint();
        mpaint.setTextSize(mTitleSize);

        mBound=new Rect();
        mpaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);

    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

调用系统定义好的方法

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mpaint.setColor(Color.BLACK);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mpaint);

        mpaint.setColor(mTitleColor);
        canvas.drawText(mTitleText,getWidth()/2-mBound.width()/2,getHeight()/2+mBound.height()/2,mpaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}
原创粉丝点击