安卓开发自定义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文件里编写自定义属性
接下来就是代码部分了
接下来就是效果图
是不是很简单,大家可以慢慢玩制作出更加复杂的更加炫酷的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开发-自定义View-AndroidStudio(八)自定义View初体验
- 安卓开发中自定义View之onMeasure(),onLayout(),onDraw()讲解(一)
- 安卓开发入门之自定义View(翻译)
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(六)实现ViewPager最基本的
- 安卓开发中自定义View之onMeasure(),onLayout(),onDraw()讲解(二)
- 安卓开发(5):自定义RecyclerView.Adapter、RecyclerView和自定义RecyclerView.ViewHolder的使用
- 安卓开发-自定义view(二)-自定义属性
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(十)实现内嵌在app中的webview 腾讯开源X5 高效安全
- [安卓开发] 自定义View之一: 组合View
- 安卓开发之自定义动画控件BatteryView(电池加载动画)
- 安卓开发:仿微博自定义带进度条和vip标识功能的圆形头像IdentityImageView
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(七)实现ViewPager无限循环与自动播放
- 安卓开发 简单实现自定义横向滚动选择View : HorizontalselectedView
- 安卓开发之ViewDragHelper的使用及自定义可下拉展示内容的ViewGroup
- 安卓开发中自定义View之onMeasure(),onLayout(),onDraw()讲解(三)
- 安卓开发——自定义ViewGroup
- [安卓开发]弹幕滚幕效果自定义View之BarrageView|支持点击事件|隐藏不滞留|颜色随机|大小速度范围随机
- 安卓开发自定义View的框架学习
- 安卓开发:弹出自定义对话框自动播放视频(仿视频广告效果,使用mediaplayer和surfaceview做播放器)
- 安卓开发之自定义View------> 圆形头像ImageView