国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義view實戰筆記--餅圖demo

2019-11-09 15:44:43
字體:
來源:轉載
供稿:網友

1:想要從view的中心開始繪制可以如下

canvas.save(); canvas.translate(mWidth / 2, mHeight / 2); canvas.restore();

2:數學相關的計算多用:Math類 —三角函數等等

3:想要在一組數據中查找value在這組數據中的位置,可以先排序,再用二分查找法(binarySearch)找出

int position = Arrays.binarySearch(endAngles, (float) touchDegree);

4:在處理觸摸事件和繪制的時候,特別注意坐標問題,可以轉變坐標系,常用還可以用兩個變量來提升性能,不用一直繪制 lastTouchedPosition ,currentTouchedPosition

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: float touchX = event.getX(); float touchY = event.getY(); //轉換坐標為圓中心點的坐標 touchX = touchX - mWidth / 2; touchY = touchY - mHeight / 2; //根據反正切值拿到角度 touchDegree = MathUtil.getTouchAngle(touchX, touchY); Log.e("degree", touchDegree + ""); //找出觸摸的是第幾塊區域 float touchRadius = (float) Math.sqrt(touchX * touchX + touchY * touchY); if (touchRadius < circleRadius) { int position = Arrays.binarySearch(startAngles, (float) touchDegree); if (position > 0) { currentTouchedPosition = position; } else { // -index - 1 = position currentTouchedPosition = -position - 1; } if (currentTouchedPosition != lastTouchedPosition) { lastTouchedPosition = currentTouchedPosition; Log.e("lastTouchedPosition",lastTouchedPosition +""); invalidate(); } } break; case MotionEvent.ACTION_UP: lastTouchedPosition = -1; invalidate(); break; } return true; }

5:注意坐標系的象限和數學中不一樣

String percentText = String.format("%.2f", pieBean.getPercent() * 100) + "%"; if (startAngle % 360 > 90 && startAngle % 360 < 270) { mLinePaint.setTextAlign(Paint.Align.RIGHT); canvas.drawText(percentText, endX, endY, mLinePaint); mLinePaint.setTextAlign(Paint.Align.LEFT); } else { canvas.drawText(percentText, endX, endY, mLinePaint); }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 句容市| 华坪县| 远安县| 琼中| 皋兰县| 重庆市| 增城市| 梨树县| 荥经县| 哈巴河县| 新沂市| 汕尾市| 潮安县| 秭归县| 丰镇市| 古浪县| 从江县| 孝昌县| 崇明县| 板桥市| 桐城市| 富裕县| 南宁市| 南充市| 密云县| 密山市| 资溪县| 介休市| 拜泉县| 永宁县| 刚察县| 水富县| 淅川县| 成武县| 勃利县| 新建县| 朝阳县| 措美县| 康乐县| 宾川县| 增城市|