电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

android自定義可拖拽的儀表盤

瀏覽:5日期:2022-09-18 13:18:20

本文實例為大家分享了android自定義可拖拽的儀表盤的具體代碼,供大家參考,具體內容如下

因為項目最近需要用到儀表盤,又不想使用之前使用的背景圖的方式。主要是想自己寫一點代碼。覺得繪制要比圖片好。于是有了下面這張圖:

android自定義可拖拽的儀表盤

面從弧度,刻度,文字,指針都是canvas繪制出來的。

/** * Created by xulc on 2018/7/18. */public class DashboardView extends View { private int minWidthDP = 200; private int minHeightDP = 100; private Paint arcPaint,arcInnerPaint,linePaint,textPaint; private int arcColor = Color.parseColor('#0096ff'); //外層弧形顏色 private int arcInnerColor = Color.parseColor('#FFFFFFFF'); //內層弧形顏色 private int lineColor = Color.parseColor('#333333'); //線條顏色 private int pointerColor = Color.parseColor('#439AFF'); //指針顏色 private int arcWidthDP = 1; private RectF arcRectF,arcInnerRectF; private int widthDash = 0;//表盤的寬度 private int mwidth =0; private int mheight = 0; private float shortlineLength = 0 ,longlineLength = 0; //線的長度 private Path path = new Path(); private Path pointerPath = new Path(); //指針繪制路徑 private Region pointerRegion = new Region(); //指針區域 private RectF rectF = new RectF(); private boolean isChoosePointer = false; private int mdegree = 0; public DashboardView(Context context) {this(context,null); } public DashboardView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);initPaint(); } //初始化相關資源 private void initPaint() {arcPaint = new Paint();arcPaint.setColor(arcColor);arcPaint.setAntiAlias(true);arcPaint.setStrokeCap(Paint.Cap.SQUARE);arcPaint.setStrokeWidth(1);arcPaint.setStyle(Paint.Style.FILL);arcInnerPaint = new Paint();arcInnerPaint.setColor(arcInnerColor);arcInnerPaint.setAntiAlias(true);arcInnerPaint.setStrokeCap(Paint.Cap.SQUARE);arcInnerPaint.setStrokeWidth(1);arcInnerPaint.setStyle(Paint.Style.FILL);linePaint = new Paint();linePaint.setColor(lineColor);linePaint.setAntiAlias(true);linePaint.setStrokeCap(Paint.Cap.SQUARE);linePaint.setStrokeWidth(arcWidthDP);linePaint.setStyle(Paint.Style.FILL);textPaint = new Paint();textPaint.setColor(lineColor);textPaint.setAntiAlias(true);linePaint.setStrokeCap(Paint.Cap.SQUARE);linePaint.setStrokeWidth(arcWidthDP);linePaint.setStyle(Paint.Style.STROKE);textPaint.setTextAlign(Paint.Align.LEFT);textPaint.setTextSize(30);arcRectF = new RectF();arcInnerRectF = new RectF(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);if(widthSize < DensityUtil.dip2px(getContext(),minWidthDP)||heightSize < DensityUtil.dip2px(getContext(),minHeightDP)){ widthSize = DensityUtil.dip2px(getContext(),minWidthDP); heightSize = DensityUtil.dip2px(getContext(),minHeightDP);}if(widthSize/2 != heightSize){ heightSize = widthSize/2;}setMeasuredDimension(widthSize,heightSize + 50);arcRectF.left = 0;arcRectF.bottom = heightSize*2;arcRectF.right = widthSize;arcRectF.top = 0; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);widthDash = DensityUtil.dip2px(getContext(),50);arcInnerRectF.left = widthDash;arcInnerRectF.bottom = arcRectF.bottom - widthDash;arcInnerRectF.right = arcRectF.bottom -widthDash;arcInnerRectF.top = widthDash;shortlineLength = widthDash/7;longlineLength = widthDash/5;mwidth = getWidth();mheight = getHeight() - 50;Log.d('xulc','mheight----->'+mheight);Log.d('xulc','arcRectF.bottom----->'+arcRectF.bottom); } @Override protected void onDraw(Canvas canvas) {arcPaint.setColor(arcColor);canvas.drawArc(arcRectF,180,180,true,arcPaint); //繪制外弧形canvas.drawArc(arcInnerRectF,180,180,true,arcInnerPaint); //繪制內部弧形canvas.save();drawScale(canvas); //繪制刻度canvas.restore();drawText(canvas); //繪制文本drawPointer(canvas,mdegree); //繪制指針 } private int mradius = 50; //繪制指針 private void drawPointer(Canvas canvas,float degree){pointerPath.reset();if(isChoosePointer){ arcPaint.setColor(pointerColor);}pointerPath.reset();pointerPath.moveTo((float)( mwidth/2 - mradius*Math.sin(degree/180f*Math.PI)),(float)( mheight + mradius*Math.cos(degree/180f*Math.PI))); //下切點pointerPath.lineTo(mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength -mradius),mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-mradius));pointerPath.lineTo((float)( mwidth/2 + mradius*Math.sin(degree/180f*Math.PI)),(float)( mheight - mradius*Math.cos(degree/180f*Math.PI)));pointerPath.close();pointerPath.computeBounds(rectF,true);pointerRegion.setPath(pointerPath,new Region((int) rectF.left,(int) rectF.top,(int) rectF.right,(int) rectF.bottom));canvas.drawPath(pointerPath,arcPaint); //path轉化為Region區域,方便判斷用戶點擊的位置path.reset();arcPaint.setColor(arcColor);path.addCircle(mwidth/2,mheight,mradius, Path.Direction.CW);canvas.drawPath(path,arcPaint);textPaint.setTextAlign(Paint.Align.CENTER);canvas.drawText(''+mdegree,mwidth/2,mheight,textPaint); } //設置度數 public void setDegree(int degree){if(0<=degree && degree<=180){ mdegree = degree; invalidate();} } //觸摸事件 @Override public boolean onTouchEvent(MotionEvent event) {float startx ,starty;if(event.getAction()==MotionEvent.ACTION_DOWN){ startx = event.getX(); starty = event.getY(); if(pointerRegion.contains((int) startx,(int) starty)){ //在其中isChoosePointer =true;invalidate();return true; //消費當前事件,否則不會繼續分發后續事件 } return false;}else if(event.getAction()==MotionEvent.ACTION_MOVE){ if(isChoosePointer){float x = event.getX(); float y = event.getY();if(y <= mheight && x!=mwidth/2){ double degree= Math.atan2((mheight-y) ,(mwidth/2 - x)); setDegree((int) (degree/Math.PI*180));}else{ if(y > mheight&& x < mwidth/2){ //說明滑到下面了setDegree(0); }else if(y > mheight&& x > mwidth/2){setDegree(180); }}return true; }else{return false; }}else if(event.getAction()==MotionEvent.ACTION_UP||event.getAction()==MotionEvent.ACTION_CANCEL){ isChoosePointer =false; invalidate(); return true;}return super.onTouchEvent(event); } //繪制文字 private void drawText(Canvas canvas) {textPaint.setTextAlign(Paint.Align.LEFT);for(int i=0;i<=6;i++){ int degree = i*30; float textWidth = textPaint.measureText(''+degree); if(degree ==0){canvas.drawText('' + degree,mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength -10) - textWidth/2,mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10)+7,textPaint); } //向右邊移動7個像素 向下邊移動7個像素 else if(degree == 30){ canvas.drawText('' + degree,mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10) - textWidth/2,mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10)+7 ,textPaint); }else if(degree ==60){canvas.drawText('' + degree,mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10) - textWidth/2,mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10)+7 ,textPaint); }else if(degree ==90){canvas.drawText('' + degree,mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10) - textWidth/2,mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10)+7 ,textPaint); }else{canvas.drawText('' + degree,mwidth/2 - (float) Math.cos(degree/180f*Math.PI)*(mheight - widthDash - longlineLength - 10) - textWidth,mheight - (float) Math.sin(degree/180f*Math.PI)*(mheight - widthDash - longlineLength-10)+7 ,textPaint); }} } //繪制刻度 private void drawScale(Canvas canvas){for(int i=0;i<=36;i++){ //180角度,30度一個長線 0 30 60 90 120 150 180 5條小線 5度一個小線 if(i%6==0){//長線canvas.drawLine(widthDash,mheight,widthDash + longlineLength ,mheight,linePaint); }else{ //短線canvas.drawLine(widthDash,mheight,widthDash + shortlineLength ,mheight,linePaint); } canvas.rotate(5,mwidth/2,mheight);} }}

整體代碼差不多就這樣,代碼中詳盡的注釋。代碼基本上都在這了,就不上傳git了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 苏州同创电子有限公司 - 四探针测试仪源头厂家 | 江苏南京多语种翻译-专业翻译公司报价-正规商务翻译机构-南京华彦翻译服务有限公司 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | ISO9001认证咨询_iso9001企业认证代理机构_14001|18001|16949|50430认证-艾世欧认证网 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 电磁流量计厂家_涡街流量计厂家_热式气体流量计-青天伟业仪器仪表有限公司 | 浙江浩盛阀门有限公司| IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 郑州爱婴幼师学校_专业幼师培训_托育师培训_幼儿教育培训学校 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 高压绝缘垫-红色配电房绝缘垫-绿色高压绝缘地毯-上海苏海电气 | 氧化铝球_高铝球_氧化铝研磨球-淄博誉洁陶瓷新材料有限公司 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 氟氨基酮、氯硝柳胺、2-氟苯甲酸、异香兰素-新晨化工 | 10吨无线拉力计-2吨拉力计价格-上海佳宜电子科技有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 脉冲布袋除尘器_除尘布袋-泊头市净化除尘设备生产厂家 | 垃圾压缩设备_垃圾处理设备_智能移动式垃圾压缩设备--山东明莱环保设备有限公司 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 房屋质量检测-厂房抗震鉴定-玻璃幕墙检测-房屋安全鉴定机构 | IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 茅茅虫AI论文写作助手-免费AIGC论文查重_写毕业论文降重 | 软启动器-上海能曼电气有限公司 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 |