最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義控件實現(xiàn)雷達圖效果

 更新時間:2022年09月05日 14:45:18   作者:芒果蜜桃π  
這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)雷達圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android自定義控件實現(xiàn)雷達圖的具體代碼,供大家參考,具體內(nèi)容如下

學習了大神的源代碼(奈何不知大神的博客地址),覺得必須記錄一下,方便以后再次學習。

效果如圖所示:

1.自定義雷達圖控件:

public class MyPolygonView extends View {

? ? //-------------我們必須給的模擬數(shù)據(jù)-------------
? ? //n邊形
? ? private int n = 6;
? ? //每個角對應(yīng)的文字
? ? private String[] text = new String[]{"語文", "數(shù)學", "英語", "生物", "化學","物理"};
? ? //區(qū)域等級,值不能超過n邊形的個數(shù)(每個角對應(yīng)的值到達的層數(shù))
? ? private int[] area = new int[]{3,3,2,2,3,2};

? ? //-------------View相關(guān)-------------
? ? //View自身的寬和高
? ? private int mHeight;
? ? private int mWidth;

? ? //-------------畫筆相關(guān)-------------
? ? //邊框的畫筆
? ? private Paint borderPaint;
? ? //文字的畫筆
? ? private Paint textPaint;
? ? //區(qū)域的畫筆
? ? private Paint areaPaint;

? ? //-------------多邊形相關(guān)-------------
? ? //n邊形個數(shù)
? ? private int num = 4;
? ? //兩個多邊形之間的半徑
? ? private int r = 60;
? ? //n邊形頂點坐標
? ? private float x, y;
? ? //n邊形角度
? ? private float angle = (float) ((2 * Math.PI) / n);
? ? //文字與邊框的邊距等級,值越大邊距越?。ㄎ淖峙c邊框的距離)
? ? private int textAlign = 5;

? ? //-------------顏色相關(guān)-------------
? ? //邊框顏色(整個n邊型的區(qū)域顏色)
? ? private int mColor = getResources().getColor(R.color.app_polygon);
? ? //文字顏色
? ? private int textColor = getResources().getColor(R.color.app_black);
? ? //區(qū)域顏色(整個連線的顏色)
? ? private int strengthColor = Color.parseColor("#f9c172");

? ? public MyPolygonView(Context context) {
? ? ? ? super(context);
? ? }

? ? public MyPolygonView(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }

? ? public MyPolygonView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? }

? ? @Override
? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) {
? ? ? ? super.onSizeChanged(w, h, oldw, oldh);
? ? ? ? mWidth = w;
? ? ? ? mHeight = h;
? ? }

? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? //初始化畫筆
? ? ? ? initPaint();
? ? ? ? //畫布移到中心點
? ? ? ? canvas.translate(mWidth / 2, mHeight / 2);
? ? ? ? //畫n邊形
? ? ? ? drawPolygon(canvas);
? ? ? ? //畫n邊形的中點到頂點的線
? ? ? ? drawLine(canvas);
? ? ? ? //畫文字
? ? ? ? drawText(canvas);
? ? ? ? //畫藍色區(qū)域
? ? ? ? drawArea(canvas);
? ? }

? ? /**
? ? ?* 初始化畫筆
? ? ?*/
? ? private void initPaint() {
? ? ? ? //邊框畫筆
? ? ? ? borderPaint = new Paint();
? ? ? ? borderPaint.setAntiAlias(true);
? ? ? ? borderPaint.setStyle(Paint.Style.FILL_AND_STROKE);
? ? ? ? borderPaint.setColor(mColor);
? ? ? ? borderPaint.setStrokeWidth(3);

? ? ? ? //文字畫筆
? ? ? ? textPaint = new Paint();
? ? ? ? textPaint.setTextSize(30);
? ? ? ? textPaint.setColor(textColor);
? ? ? ? textPaint.setAntiAlias(true);

? ? ? ? //區(qū)域畫筆
? ? ? ? areaPaint = new Paint();
? ? ? ? areaPaint.setStrokeWidth(5);
? ? ? ? areaPaint.setColor(strengthColor);
? ? ? ? areaPaint.setAntiAlias(true);
? ? ? ? areaPaint.setStyle(Paint.Style.STROKE);
? ? }

? ? /**
? ? ?* 繪制多邊形
? ? ?*
? ? ?* @param canvas
? ? ?*/
? ? private void drawPolygon(Canvas canvas) {
? ? ? ? Path path = new Path();
? ? ? ? //n邊形數(shù)目
? ? ? ? for (int j = 1; j <= num; j++) {
? ? ? ? ? ? float r = j * this.r;
? ? ? ? ? ? path.reset();
? ? ? ? ? ? //畫n邊形
? ? ? ? ? ? for (int i = 1; i <= n; i++) {
? ? ? ? ? ? ? ? x = (float) (Math.cos(i * angle) * r);
? ? ? ? ? ? ? ? y = (float) (Math.sin(i * angle) * r);
? ? ? ? ? ? ? ? if (i == 1) {
? ? ? ? ? ? ? ? ? ? path.moveTo(x, y);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? path.lineTo(x, y);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉當前輪廓。如果當前點不等于第一個點的輪廓,一條線段是自動添加的
? ? ? ? ? ? path.close();
? ? ? ? ? ? canvas.drawPath(path, borderPaint);
? ? ? ? }
? ? }

? ? /**
? ? ?* 畫多邊形線段
? ? ?*
? ? ?* @param canvas
? ? ?*/
? ? private void drawLine(Canvas canvas) {
? ? ? ? Path path = new Path();
? ? ? ? float r = num * this.r;
? ? ? ? for (int i = 1; i <= n; i++) {
? ? ? ? ? ? path.reset();
? ? ? ? ? ? x = (float) (Math.cos(i * angle) * r);
? ? ? ? ? ? y = (float) (Math.sin(i * angle) * r);
? ? ? ? ? ? path.lineTo(x, y);
? ? ? ? ? ? canvas.drawPath(path, borderPaint);
? ? ? ? }
? ? }

? ? /**
? ? ?* 畫文字
? ? ?*
? ? ?* @param canvas
? ? ?*/
? ? private void drawText(Canvas canvas) {
? ? ? ? float r = num * this.r;
? ? ? ? for (int i = 1; i <= n; i++) {
? ? ? ? ? ? //測量文字的寬高
? ? ? ? ? ? Rect rect = new Rect();
? ? ? ? ? ? textPaint.getTextBounds(text[i - 1], 0, text[i - 1].length(), rect);
? ? ? ? ? ? float textWidth = rect.width();
? ? ? ? ? ? float textHeight = rect.height();

? ? ? ? ? ? x = (float) (Math.cos(i * angle) * r);
? ? ? ? ? ? y = (float) (Math.sin(i * angle) * r);
? ? ? ? ? ? //位置微調(diào)
? ? ? ? ? ? if (x < 0) {
? ? ? ? ? ? ? ? x = x - textWidth;
? ? ? ? ? ? }
? ? ? ? ? ? if (y > 25) {
? ? ? ? ? ? ? ? y = y + textHeight;
? ? ? ? ? ? }
? ? ? ? ? ? //調(diào)文字與邊框的邊距
? ? ? ? ? ? float LastX = x + x / num / textAlign;
? ? ? ? ? ? float LastY = y + y / num / textAlign;
? ? ? ? ? ? canvas.drawText(text[i - 1],LastX, LastY, textPaint);
? ? ? ? }
? ? }

? ? /**
? ? ?* 畫區(qū)域
? ? ?*
? ? ?* @param canvas
? ? ?*/
? ? private void drawArea(Canvas canvas) {
? ? ? ? Path path = new Path();
? ? ? ? for (int ?i= 1; ?i<= n; i++) {
? ? ? ? ? ? float r = area[i - 1] * this.r;
? ? ? ? ? ? x = (float) (Math.cos(i * angle) * r);
? ? ? ? ? ? y = (float) (Math.sin(i * angle) * r);
? ? ? ? ? ? if (i == 1) {
? ? ? ? ? ? ? ? path.moveTo(x, y);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? path.lineTo(x, y);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //關(guān)閉當前輪廓。如果當前點不等于第一個點的輪廓,一條線段是自動添加的
? ? ? ? path.close();
? ? ? ? canvas.drawPath(path, areaPaint);
? ? }
? ? public void setArea (int[] area){
? ? ? ? this.area =area;
? ? ? ? invalidate();
? ? }

}

2.界面布局文件xml中直接使用:

<com.lotus.chartspagedemo.MyPolygonView
? ? ? ? ? ? ? ? android:id="@+id/polygon"
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:visibility="visible" />

3.界面activity中可以設(shè)置控件顏色:

polygon.setBackgroundColor(getResources().getColor(R.color.app_blue));//雷達圖的背景顏色

如果不設(shè)置背景顏色,效果就是:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android多線程之同步鎖的使用

    Android多線程之同步鎖的使用

    本篇文章主要介紹了Android多線程之同步鎖的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • android多行標簽熱點示例

    android多行標簽熱點示例

    這篇文章主要介紹了android多行標簽熱點示例,需要的朋友可以參考下
    2014-04-04
  • Android?如何獲取傳感器的數(shù)據(jù)方法詳解

    Android?如何獲取傳感器的數(shù)據(jù)方法詳解

    這篇文章主要介紹了Android?如何獲取傳感器的數(shù)據(jù),傳感器?Sensor?是一種檢測裝置,能感受到被測量的信息,并能將感受到的信息,按一定規(guī)律變換成為電信號或其他所需形式的信息輸出,以滿足信息的傳輸、處理、存儲、顯示、記錄和控制等要求
    2022-07-07
  • 淺談Android添加快捷方式ShortCut

    淺談Android添加快捷方式ShortCut

    這篇文章主要介紹了淺談Android添加快捷方式ShortCut,對添加快捷方式感興趣的同學,可以參考下
    2021-04-04
  • Android?Springboot?實現(xiàn)SSE通信案例詳解

    Android?Springboot?實現(xiàn)SSE通信案例詳解

    SSE是一種用于實現(xiàn)服務(wù)器主動向客戶端推送數(shù)據(jù)的技術(shù),它基于?HTTP?協(xié)議,利用了其長連接特性,在客戶端與服務(wù)器之間建立一條持久化連接,并通過這條連接實現(xiàn)服務(wù)器向客戶端的實時數(shù)據(jù)推送,這篇文章主要介紹了Android?Springboot?實現(xiàn)SSE通信案例,需要的朋友可以參考下
    2024-07-07
  • Android使用http實現(xiàn)注冊登錄功能

    Android使用http實現(xiàn)注冊登錄功能

    這篇文章主要為大家詳細介紹了Android使用http實現(xiàn)注冊登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Android Service 使用時的注意事項

    詳解Android Service 使用時的注意事項

    這篇文章主要介紹了詳解Android Service 使用時的注意事項,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Android事件分發(fā)機制?ViewGroup分析

    Android事件分發(fā)機制?ViewGroup分析

    這篇文章主要介紹了Android事件分發(fā)機制?ViewGroup分析,事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會傳遞給Android的輸入系統(tǒng)服務(wù)IMS,更多相關(guān)介紹,需要的朋友可以參考一下
    2022-09-09
  • Android自定義圓角ImageView

    Android自定義圓角ImageView

    這篇文章主要介紹了Android自定義圓角ImageView的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android基礎(chǔ)之使用Fragment控制切換多個頁面

    Android基礎(chǔ)之使用Fragment控制切換多個頁面

    Android官方已經(jīng)提供了Fragment的各種使用的Demo例子,在我們SDK下面的API Demo里面就包含了Fragment的各種使用例子,需要看Demo的朋友,直接看API Demo那個程序就可以了,不用到處去找。里面分開不同功能,實現(xiàn)了不同的類
    2013-07-07

最新評論

深水埗区| 固镇县| 通海县| 永登县| 麻阳| 新乡县| 阿克苏市| 延川县| 阜康市| 彩票| 广元市| 广元市| 开平市| 潜山县| 得荣县| 敖汉旗| 海南省| 乌拉特中旗| 平谷区| 汕头市| 固安县| 嫩江县| 黑山县| 邵阳市| 西藏| 博罗县| 彭州市| 黎城县| 长垣县| 开鲁县| 保靖县| 江城| 东宁县| 泰兴市| 固镇县| 双江| 三江| 水城县| 德阳市| 姜堰市| 南溪县|