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 Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法
本篇文章主要介紹了詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法,非常具有實(shí)用價值,需要的朋友可以參考下2017-06-06
Android開發(fā)之App widget用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之App widget用法,結(jié)合實(shí)例形式詳細(xì)分析了Android開發(fā)中使用App widget組件的具體步驟與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
Android實(shí)現(xiàn)儀表盤控件開發(fā)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)儀表盤控件開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05

