Android自定義控件實(shí)現(xiàn)雷達(dá)圖效果
本文實(shí)例為大家分享了Android自定義控件實(shí)現(xiàn)雷達(dá)圖的具體代碼,供大家參考,具體內(nèi)容如下
學(xué)習(xí)了大神的源代碼(奈何不知大神的博客地址),覺得必須記錄一下,方便以后再次學(xué)習(xí)。
效果如圖所示:

1.自定義雷達(dá)圖控件:
public class MyPolygonView extends View {
? ? //-------------我們必須給的模擬數(shù)據(jù)-------------
? ? //n邊形
? ? private int n = 6;
? ? //每個角對應(yīng)的文字
? ? private String[] text = new String[]{"語文", "數(shù)學(xué)", "英語", "生物", "化學(xué)","物理"};
? ? //區(qū)域等級,值不能超過n邊形的個數(shù)(每個角對應(yīng)的值到達(dá)的層數(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邊形頂點(diǎn)坐標(biāo)
? ? 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();
? ? ? ? //畫布移到中心點(diǎn)
? ? ? ? canvas.translate(mWidth / 2, mHeight / 2);
? ? ? ? //畫n邊形
? ? ? ? drawPolygon(canvas);
? ? ? ? //畫n邊形的中點(diǎn)到頂點(diǎn)的線
? ? ? ? drawLine(canvas);
? ? ? ? //畫文字
? ? ? ? drawText(canvas);
? ? ? ? //畫藍(lán)色區(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)閉當(dāng)前輪廓。如果當(dāng)前點(diǎn)不等于第一個點(diǎ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)閉當(dāng)前輪廓。如果當(dāng)前點(diǎn)不等于第一個點(diǎ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));//雷達(dá)圖的背景顏色
如果不設(shè)置背景顏色,效果就是:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?如何獲取傳感器的數(shù)據(jù)方法詳解
這篇文章主要介紹了Android?如何獲取傳感器的數(shù)據(jù),傳感器?Sensor?是一種檢測裝置,能感受到被測量的信息,并能將感受到的信息,按一定規(guī)律變換成為電信號或其他所需形式的信息輸出,以滿足信息的傳輸、處理、存儲、顯示、記錄和控制等要求2022-07-07
Android?Springboot?實(shí)現(xiàn)SSE通信案例詳解
SSE是一種用于實(shí)現(xiàn)服務(wù)器主動向客戶端推送數(shù)據(jù)的技術(shù),它基于?HTTP?協(xié)議,利用了其長連接特性,在客戶端與服務(wù)器之間建立一條持久化連接,并通過這條連接實(shí)現(xiàn)服務(wù)器向客戶端的實(shí)時數(shù)據(jù)推送,這篇文章主要介紹了Android?Springboot?實(shí)現(xiàn)SSE通信案例,需要的朋友可以參考下2024-07-07
Android使用http實(shí)現(xiàn)注冊登錄功能
這篇文章主要為大家詳細(xì)介紹了Android使用http實(shí)現(xiàn)注冊登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
詳解Android Service 使用時的注意事項(xiàng)
這篇文章主要介紹了詳解Android Service 使用時的注意事項(xiàng),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Android事件分發(fā)機(jī)制?ViewGroup分析
這篇文章主要介紹了Android事件分發(fā)機(jī)制?ViewGroup分析,事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會傳遞給Android的輸入系統(tǒng)服務(wù)IMS,更多相關(guān)介紹,需要的朋友可以參考一下2022-09-09
Android基礎(chǔ)之使用Fragment控制切換多個頁面
Android官方已經(jīng)提供了Fragment的各種使用的Demo例子,在我們SDK下面的API Demo里面就包含了Fragment的各種使用例子,需要看Demo的朋友,直接看API Demo那個程序就可以了,不用到處去找。里面分開不同功能,實(shí)現(xiàn)了不同的類2013-07-07

