欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Volley源碼之使用方式和使用場景詳解

    Volley源碼之使用方式和使用場景詳解

    這篇文章主要介紹了Volley源碼之使用方式和使用場景詳解,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Android仿淘寶物流信息TimeLineView

    Android仿淘寶物流信息TimeLineView

    這篇文章主要為大家詳細介紹了Android仿淘寶物流信息TimeLineView的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法

    詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法

    本篇文章主要介紹了詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法,非常具有實用價值,需要的朋友可以參考下
    2017-06-06
  • 詳解Android框架MVVM分析以及使用

    詳解Android框架MVVM分析以及使用

    這篇文章主要介紹了詳解Android框架MVVM分析以及使用,對MVVM感興趣的同學,一定要看一下
    2021-04-04
  • Android開發(fā)之App widget用法實例分析

    Android開發(fā)之App widget用法實例分析

    這篇文章主要介紹了Android開發(fā)之App widget用法,結(jié)合實例形式詳細分析了Android開發(fā)中使用App widget組件的具體步驟與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android實現(xiàn)布局全屏

    Android實現(xiàn)布局全屏

    這篇文章主要為大家詳細介紹了Android實現(xiàn)布局全屏,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android中Parcelable的使用詳解

    Android中Parcelable的使用詳解

    Serializable是Java為我們提供的一個標準化的序列化接口。而Parcelable是Android為我們提供的序列化的接口。 這篇文章主要介紹了Android中Parcelable的使用 ,需要的朋友可以參考下
    2019-06-06
  • 淺析Android的啟動原理

    淺析Android的啟動原理

    當談到Android啟動原理時,我們進入了Android操作系統(tǒng)的核心,理解Android系統(tǒng)啟動的原理對于開發(fā)者來說非常重要,因為這有助于優(yōu)化應(yīng)用程序性能并提供更好的用戶體驗,本文給大家講講Android啟動原理,需要的朋友可以參考下
    2023-10-10
  • Android實現(xiàn)儀表盤控件開發(fā)

    Android實現(xiàn)儀表盤控件開發(fā)

    這篇文章主要為大家詳細介紹了Android實現(xiàn)儀表盤控件開發(fā),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 詳解android.mk中引用第三方庫的方法

    詳解android.mk中引用第三方庫的方法

    本篇文章主要介紹了詳解android.mk中引用第三方庫的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05

最新評論