Skip to content

Commit 4c65c04

Browse files
committed
自定义view饼形图
饼形图
1 parent 8b3caab commit 4c65c04

File tree

29 files changed

+773
-47
lines changed

29 files changed

+773
-47
lines changed

.gitignore

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
1-
# Windows image file caches
2-
Thumbs.db
3-
ehthumbs.db
4-
5-
# Folder config file
6-
Desktop.ini
7-
8-
# Recycle Bin used on file shares
9-
$RECYCLE.BIN/
10-
11-
# Windows Installer files
12-
*.cab
13-
*.msi
14-
*.msm
15-
*.msp
16-
17-
# Windows shortcuts
18-
*.lnk
19-
20-
# =========================
21-
# Operating System Files
22-
# =========================
23-
24-
# OSX
25-
# =========================
26-
27-
.DS_Store
28-
.AppleDouble
29-
.LSOverride
30-
31-
# Thumbnails
32-
._*
33-
34-
# Files that might appear in the root of a volume
35-
.DocumentRevisions-V100
36-
.fseventsd
37-
.Spotlight-V100
38-
.TemporaryItems
39-
.Trashes
40-
.VolumeIcon.icns
41-
42-
# Directories potentially created on remote AFP share
43-
.AppleDB
44-
.AppleDesktop
45-
Network Trash Folder
46-
Temporary Items
47-
.apdisk
1+
2+
# Built application files
3+
*.apk
4+
*.ap_
5+
6+
# Files for the ART/Dalvik VM
7+
*.dex
8+
9+
# Java class files
10+
*.class
11+
12+
# Generated files
13+
bin/
14+
gen/
15+
out/
16+
17+
# Gradle files
18+
.gradle/
19+
build/
20+
.idea/
21+
22+
23+
# Local configuration file (sdk path, etc)
24+
local.properties
25+
26+
# Proguard folder generated by Eclipse
27+
proguard/
28+
29+
# Log Files
30+
*.log
31+
32+
# Android Studio Navigation editor temp files
33+
.navigation/
34+
35+
# Android Studio captures folder
36+
captures/
37+
38+
# Intellij
39+
*.iml
40+
.idea/workspace.xml
41+
42+
# Keystore files
43+
*.jks

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.2"
6+
defaultConfig {
7+
applicationId "org.ninetripods.mq.circleview"
8+
minSdkVersion 15
9+
targetSdkVersion 24
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support:appcompat-v7:24.2.1'
28+
testCompile 'junit:junit:4.12'
29+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in D:\Android\sdkN/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.ninetripods.mq.circleview;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("org.ninetripods.mq.circleview", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.ninetripods.mq.circleview">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.ninetripods.mq.circleview;
2+
3+
import android.graphics.Color;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.os.Bundle;
6+
7+
import org.ninetripods.mq.circleview.customView.CakeBean;
8+
import org.ninetripods.mq.circleview.customView.CakeView;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class MainActivity extends AppCompatActivity {
14+
private List<CakeBean> beans;
15+
private String[] names = {"php", "object-c", "c", "c++", "java", "android", "linux"};
16+
private float[] values = {2f, 2f, 3f, 4f, 5f, 6f, 7f};
17+
private int[] colArrs = {Color.RED, Color.parseColor("#4ebcd3"), Color.MAGENTA, Color.YELLOW, Color.GREEN, Color.parseColor("#f68b2b"), Color.parseColor("#6fb30d")};//圆弧颜色
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_main);
23+
initData();
24+
initViews();
25+
}
26+
27+
/**
28+
* 初始化数据
29+
*/
30+
private void initData() {
31+
beans = new ArrayList<>();
32+
for (int i = 0; i < 7; i++) {
33+
CakeBean bean = new CakeBean();
34+
bean.name = names[i];
35+
bean.value = values[i];
36+
bean.mColor = colArrs[i];
37+
beans.add(bean);
38+
}
39+
}
40+
41+
/**
42+
* 初始化视图
43+
*/
44+
private void initViews() {
45+
CakeView cake_view = (CakeView) findViewById(R.id.cake_view);
46+
cake_view.setData(beans);
47+
}
48+
49+
50+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.ninetripods.mq.circleview.customView;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by MQ on 2016/12/15.
7+
*/
8+
9+
public class CakeBean implements Serializable {
10+
public String name;//名字
11+
public float value;//值
12+
public float degree;//旋转过的角度
13+
public int mColor;//圆弧颜色
14+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package org.ninetripods.mq.circleview.customView;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
7+
import android.graphics.RectF;
8+
import android.util.AttributeSet;
9+
import android.view.View;
10+
11+
12+
import java.text.DecimalFormat;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
/**
17+
* Created by MQ on 2016/12/15.
18+
*/
19+
20+
public class CakeView extends View {
21+
private List<CakeBean> beanList;
22+
//画圆的矩形
23+
private RectF mRectF;
24+
//右边的小矩形
25+
private RectF iRectF;
26+
private Paint mPaint;
27+
private int mWidth, mHeight;
28+
private float rotateDegree;//每个圆弧的起始角度
29+
private float sumValue = 0;//所有值的和
30+
private float diameter;//圆的直径
31+
private float textY;//绘制文字的Y坐标
32+
33+
private float mRectHeight = 40;//矩形高度
34+
private float mRectWidth = 80;//矩形宽度
35+
private float mMargin = 40;//矩形和圆的距离
36+
37+
public CakeView(Context context) {
38+
this(context, null);
39+
}
40+
41+
public CakeView(Context context, AttributeSet attrs) {
42+
this(context, attrs, 0);
43+
}
44+
45+
public CakeView(Context context, AttributeSet attrs, int defStyleAttr) {
46+
super(context, attrs, defStyleAttr);
47+
init();
48+
}
49+
50+
private void init() {
51+
beanList = new ArrayList<>();
52+
mRectF = new RectF();
53+
mPaint = new Paint();
54+
mPaint.setColor(Color.YELLOW);
55+
mPaint.setAntiAlias(true);
56+
mPaint.setDither(true);
57+
}
58+
59+
@Override
60+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
61+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
62+
}
63+
64+
@Override
65+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
66+
super.onSizeChanged(w, h, oldw, oldh);
67+
mWidth = w;
68+
mHeight = h;
69+
diameter = Math.min(mWidth, mHeight);
70+
}
71+
72+
@Override
73+
protected void onDraw(Canvas canvas) {
74+
super.onDraw(canvas);
75+
//设置圆形绘制的范围
76+
mRectF.set(0, 0, diameter, diameter);
77+
//画布中心X坐标向右移动(控件宽度-圆直径)之差的八分之一的距离
78+
//画布中心Y坐标向下移动(控件宽度-圆直径)之差的二分之一的距离
79+
canvas.translate((mWidth - diameter) / 8, (mHeight - diameter) / 2);
80+
if (beanList.size() > 0 && Float.compare(sumValue, 0.0f) != 0) {
81+
for (int i = 0; i < beanList.size(); i++) {
82+
CakeBean bean = beanList.get(i);
83+
//画圆弧
84+
mPaint.setColor(bean.mColor);
85+
canvas.drawArc(mRectF, rotateDegree, bean.degree, true, mPaint);
86+
rotateDegree += bean.degree;
87+
//画矩形和文字
88+
drawRectAndText(canvas, bean);
89+
}
90+
}
91+
}
92+
93+
private void drawRectAndText(Canvas canvas, CakeBean bean) {
94+
iRectF = new RectF();
95+
//设置画矩形的范围
96+
float left = diameter + mMargin;
97+
float right = diameter + mMargin + mRectWidth;
98+
float bottom = textY + mRectHeight;
99+
iRectF.set(left, textY, right, bottom);
100+
canvas.drawRect(iRectF, mPaint);
101+
//设置颜色
102+
mPaint.setColor(Color.BLACK);
103+
//设置文字大小
104+
mPaint.setTextSize(30);
105+
//画文字
106+
canvas.drawText(bean.name + "(" + new DecimalFormat(".00").format(bean.value / sumValue * 100) + "%)", right + 10, textY + 30, mPaint);
107+
textY += mRectHeight;
108+
}
109+
110+
/**
111+
* 饼状图添加数据
112+
*
113+
* @param beans CakeBean数据
114+
*/
115+
public void setData(List<CakeBean> beans) {
116+
if (beans == null || beans.size() <= 0) return;
117+
for (int i = 0; i < beans.size(); i++) {
118+
CakeBean bean = beans.get(i);
119+
sumValue += bean.value;
120+
}
121+
for (int i = 0; i < beans.size(); i++) {
122+
CakeBean bean = beans.get(i);
123+
bean.degree = bean.value / sumValue * 360;
124+
beanList.add(bean);
125+
}
126+
invalidate();
127+
}
128+
129+
/**
130+
* @param startDegree 设置起始角度
131+
*/
132+
public void setStartDegree(float startDegree) {
133+
this.rotateDegree = startDegree;
134+
invalidate();
135+
}
136+
}

0 commit comments

Comments
 (0)