首页专栏详情
打赏
Android自定义控件(三)之属性添加
易百纳技术社区 TT_123456789 2020-08-03 13:33:48

上一篇博客讲解了给自定义控件添加事件,这篇博客讲解给自定义控件添加属性,首先介绍一下添加自定义属性的基本步骤:

1.在res/values文件下新建一个属性xml文件,如attrs.xml,xml的文件名字可以自己任意取,然后再属性文件里添加

标签,如下图所示 2.声明属性,比如上面声明了名字为test的一组属性,标签里的是属性,可以有很多组属性,属性里有单位(format)和属性(name)名称,属性的单位有以下几种: reference:引用资源 string:字符串 Color:颜色 boolean:布尔值 dimension:尺寸值 float:浮点型 integer:整型 fraction:百分数 enum:枚举类型 flag:位或运算 3.在控件布局文件里添加属性和再自定义的构造方法里获取属性,下图给出的是本篇博客给出的例子的布局文件属性添加和获取属性的方法: 通过以上几步就可以获取自定义控件的属性了。 下面是给出的一个代码的示例,接着前面两篇博客继续做的工作。 属性文件attrs_percent.xml 布局文件activity_main.xml 自定义控件类代码 package com.example.myapplication; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class SimpleView extends View { private final static String TAG = SimpleView.class.getSimpleName(); //画笔 private Paint mPaint; private RectF oval; //事件处理 private EventHandle mEventHandle; //鼠标按下位置 private int startX,startY; //按下鼠标时控件的位置 private int startLeft,startTop; //状态栏高度 int statusHeight = 0; //背景颜色 int backgroundColor=Color.GRAY; int progressColor=Color.RED; int textColor=Color.WHITE; int textSize=30; int circleRadius=2048; int progress=120;//进度 public SimpleView(Context context) { super(context); init(); } public SimpleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initParameter(context,attrs); init(); } public SimpleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initParameter(context,attrs); init(); } private void init(){ mPaint = new Paint(); //设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。 mPaint.setAntiAlias(true); mPaint.setTextSize(textSize); oval=new RectF(); mEventHandle=null; startY=startX=0; int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { statusHeight = this.getResources().getDimensionPixelSize(resourceId); } } private void initParameter(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SimpleView); if (typedArray != null) { backgroundColor = typedArray.getColor(R.styleable.SimpleView_percent_background_color, Color.GRAY); progressColor = typedArray.getColor(R.styleable.SimpleView_percent_progress_color, Color.RED); circleRadius = (int)typedArray.getDimension(R.styleable.SimpleView_percent_circle_radius, 2048); progress = typedArray.getInt(R.styleable.SimpleView_percent_circle_progress, 120); textColor=typedArray.getColor(R.styleable.SimpleView_percent_text_color,Color.WHITE); textSize=(int)typedArray.getDimension(R.styleable.SimpleView_percent_text_size,30); if(progress>360) progress=360; if(progress<0) progress=0; typedArray.recycle(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); Log.e(TAG, "onMeasure--widthMode-->" + widthMode); switch (widthMode) { case MeasureSpec.EXACTLY: //精确值模式,当控件的layout_width和layout_height属性指定为具体数值或match_parent时。 break; case MeasureSpec.AT_MOST: //最大值模式,当空间的宽高设置为wrap_content时。 break; case MeasureSpec.UNSPECIFIED: //未指定模式,View想多大就多大,通常在绘制自定义View时才会用。 break; } //取最小边为控件的宽高的最小值 int minWidth=widthSize>heightSize?heightSize:widthSize; setMeasuredDimension(minWidth,minWidth); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 绘制背景圆 */ mPaint.setColor(backgroundColor); // FILL填充, STROKE描边,FILL_AND_STROKE填充和描边 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); int with = getWidth(); int height = getHeight(); Log.e(TAG, "onDraw---->" + with + "*" + height); float radius = with / 2-5; //如果设置的半径比较合理,就去设置的半径 if(circleRadius

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。

1373
收藏
1
打赏
给作者打赏,鼓励他抓紧创作吧~
评论
0个
内容存在敏感词
相关专栏
打赏作者
易百纳技术社区
TT_123456789
您的支持将鼓励我继续创作!
打赏金额:
¥1 易百纳技术社区
¥5 易百纳技术社区
¥10 易百纳技术社区
¥50 易百纳技术社区
¥100 易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区 微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
Loading...
易百纳技术社区
确定要删除此文章、专栏、评论吗?
确定
取消
易百纳技术社区
易百纳技术社区
在专栏模块发布专栏,可获得其他E友的打赏
易百纳技术社区
回答悬赏问答,被题主采纳后即可获得悬赏金
易百纳技术社区
在上传资料时,有价值的资料可设置为付费资源
易百纳技术社区
达到一定金额,收益即可提现~
收益也可用来充值ebc,下载资料、兑换礼品更容易
易百纳技术社区
活动规则
  • 1.周任务为周期性任务,每周周一00:00刷新,上周完成的任务不会累计到本周,本周需要从头开始任务,当前任务完成后才可以完成下一个任务
  • 2.发布的专栏与资料需要与平台的板块有相关性,禁止注水,专栏/资料任务以审核通过的篇数为准,专栏需为原创文章且首次在社区发布
  • 3.任务完成后,现金奖励直接打款到微信账户;EBC/收益将自动发放到个人账户,可前往“我的钱包”查看;其他奖励请联系客服兑换
易百纳技术社区
升级提醒
易百纳技术社区

恭喜您由入门

社区送出礼品一份

请填写您的收件地址,礼品将在3个工作日寄出

易百纳技术社区