安卓开发自定义view初体验

最近学习发现自定义view可以制作,可以制作很秀的自己专属的控件,具体的介绍我就不多说了,很多大牛博客已经说得很清楚了。主要实现了两个方法,我也是简单讲一下
1:protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
对view进行测量,首先获取他的模式,根据不同的模式进行进行不同的处理,进行测量获取他们的宽高
2:protected synchronized void onDraw(Canvas canvas)
在里面画出你的图形的样子,画出属于自己专属的VIEW的样子
下面就是代码部分也是很简单的
首先在attrs.xml文件里编写自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="RoundProgress">
        <attr name="mWidth" format="dimension">40</attr>
        <attr name="mHeight" format="dimension">100</attr>
        <attr name="textSize" format="dimension">18</attr>
    </declare-styleable>
    <declare-styleable name="MyView">
        <attr name="radius_s" format="dimension"></attr>
    </declare-styleable>
</resources>

接下来就是代码部分了

public class RoundProgress extends ProgressBar {
    private Paint mPaint = new Paint();
    private final int MIN_WIDTH = 40;
    private final int MIN_HEIGHT = 100;
    private final int TEXT_SIZE = 18;
    private final String PROGRESS_TEXT="30%";
    private float mWidth = dp2sp(MIN_WIDTH);
    private float mHeight = dp2sp(MIN_HEIGHT);
    private float mTextSize = sp2dp(TEXT_SIZE);
    private String progressText=PROGRESS_TEXT;
    private  Rect rect;

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

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

    public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        rect = new Rect();
    }
    /**
     * 获取自定义属性
     *
     * @param attrs attrs
     */
    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.RoundProgress);
        mHeight = (int) ta.getDimension(R.styleable.RoundProgress_mHeight, mHeight);
        mWidth = (int) ta.getDimension(R.styleable.RoundProgress_mWidth, mWidth);
        mTextSize = (int) ta.getDimension(R.styleable.RoundProgress_textSize, mTextSize);
        ta.recycle();
    }
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mPaint.getTextBounds(progressText, 0, progressText.length(), rect);//获取文字宽高
        if (widthMode != MeasureSpec.EXACTLY) {
            float finalWidth= Math.max(rect.width(),mWidth); //比较文字宽和进度条宽那个大取那个
            int widthSize = (int) (finalWidth + getPaddingLeft() + getPaddingRight());
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        }
        if (heightMode != MeasureSpec.EXACTLY) {
            float finalHeight= Math.max(rect.height(),mHeight);
            int heightSize = (int) (finalHeight + getPaddingTop() + getPaddingBottom());
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        mPaint.setColor(0xFF62666c);
        mPaint.setStyle(Paint.Style.STROKE);//空心
        float left = (getWidth() - mWidth) / 2;
        float right = (getWidth() + mWidth) / 2;
        float bottom = (getHeight() + mHeight) / 2;
        float top = (getHeight() - mHeight) / 2;
        canvas.drawRoundRect(left, top, right, bottom, 20, 20, mPaint);//画一个空心椭圆
        mPaint.setColor(0xaF00fffd);
        mPaint.setStyle(Paint.Style.FILL);//实心
        float progressTop = top + (mHeight / 100f) * (100 - getProgress());
        canvas.drawRoundRect(left, progressTop, right, bottom, 20, 20, mPaint);//画一个实心椭圆
        mPaint.setColor(0xff000000);
        progressText=getProgress()+"%";
        mPaint.getTextBounds(progressText, 0, progressText.length(), rect);
        if (getProgress() < 70)
            canvas.drawText(progressText, (mWidth - rect.width()) / 2 + left, progressTop, mPaint);
        else {
            canvas.drawText(progressText, (mWidth - rect.width()) / 2 + left, (progressTop+rect.height()), mPaint);
        }
        canvas.restore();
    }



    private int dp2sp(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }

    private int sp2dp(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());
    }
}

接下来就是效果图
这里写图片描述

是不是很简单,大家可以慢慢玩制作出更加复杂的更加炫酷的VIEW。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值