您的位置:首页 > 移动开发 > Android开发

安卓开发自定义view初体验

2017-10-24 13:27 225 查看
最近学习发现自定义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();
mPa
4000
int.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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 控件 体验
相关文章推荐