Android 实现会旋转的饼状统计图实例代码

最近在做一个项目,由于有需要统计的需要,于是就做成了下面饼状统计图。
下图是效果图:
大致思路是:
关于的介绍这里不做详细介绍,如果想深入请点击开源项目MPAndroidChart
下面是其实现:
首先是添加MPAndroidChart依赖:
maven { url "https://jitpack.io" }
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
Mainactivity
package com.example.geekp.myapplication;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
getSupportActionBar().setTitle("饼状统计图");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
//fragment
public static class PlaceholderFragment extends Fragment {
@BindView(R.id.chart1)
PieChart mChart;
@BindView(R.id.tvXMax)
TextView tvXMax;
@BindView(R.id.tvYMax)
TextView tvYMax;
protected String[] mParties = new String[]{
"已完成", "未完成"
};
protected Typeface mTfRegular;
protected Typeface mTfLight;
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.bind(this, rootView);
int index = getArguments().getInt(ARG_SECTION_NUMBER);
mTfRegular = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Regular.ttf");
mTfLight = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Light.ttf");
mChart.setUsePercentValues(true);
mChart.getDescription().setEnabled(false);
mChart.setExtraOffsets(5, 10, 5, 5);
mChart.setDragDecelerationFrictionCoef(0.95f);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterText(generateCenterSpannableText(index));
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
mChart.setDrawCenterText(true);
mChart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
setData(index);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
Legend l = mChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
// entry label styling
mChart.setEntryLabelColor(Color.WHITE);
mChart.setEntryLabelTypeface(mTfRegular);
mChart.setEntryLabelTextSize(12f);
return rootView;
}
//饼状图中间要显示的内容
private SpannableString generateCenterSpannableText(int index) {
String sectionName = "";
switch (index) {
case 1:
sectionName = "科目一";
break;
case 2:
sectionName = "科目二";
break;
case 3:
sectionName = "科目三";
break;
case 4:
sectionName = "科目四";
break;
}
SpannableString s = new SpannableString(sectionName);
s.setSpan(new RelativeSizeSpan(1.7f), 0, sectionName.length(), 0);
return s;
}
private void setData(int fragmentIndex) {
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
PieDataSet dataSet = new PieDataSet(entries, "正确率:" + 25 + "%");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
ArrayList<Integer> colors = new ArrayList<Integer>();
if (fragmentIndex == 1) {
//这里写的是饼状图的组成部分,像我这样写就是第一部分是占百分之七十五,第二部分是占了百分之二十五
entries.add(new PieEntry(75, mParties[0]));
entries.add(new PieEntry(25, mParties[1]));
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
} else if (fragmentIndex == 2) {
entries.add(new PieEntry(50, mParties[0]));
entries.add(new PieEntry(50, mParties[1]));
colors.add(getResources().getColor(R.color.piecolor8));
colors.add(getResources().getColor(R.color.piecolor2));
} else if (fragmentIndex == 3) {
entries.add(new PieEntry(45, mParties[0]));
entries.add(new PieEntry(55, mParties[1]));
colors.add(getResources().getColor(R.color.piecolor3));
colors.add(getResources().getColor(R.color.piecolor4));
} else {
entries.add(new PieEntry(60, mParties[0]));
entries.add(new PieEntry(40, mParties[1]));
colors.add(getResources().getColor(R.color.piecolor5));
colors.add(getResources().getColor(R.color.piecolor6));
}
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.BLACK);
data.setValueTypeface(mTfLight);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
mChart.invalidate();
}
}
//适配器
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 4;
}
//这个方法用于显示标题
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "科目一";
case 1:
return "科目二";
case 2:
return "科目三";
case 3:
return "科目四";
}
return null;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.geekp.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tvXMax"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvYMax"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
源码传送门:http://xiazai./201612/yuanma/piechart-master().rar
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android
# 饼状
# 统计图
# 实现饼状统计图
# 饼状统计图实例代码
# android自定义环形统计图动画
# Android自定义条形对比统计图
# Android自定义View实现多边形统计图示例代码
# Android自定义View——扇形统计图的实现代码
# Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】
# Android编程实现canvas绘制柱状统计图功能【自动计算宽高及分度值、可左右滑动】
# Android自定义控件横向柱状统计图
# 的是
# 成了
# 请点击
# 希望能
# 做一个
# 详细介绍
# 不做
# 占了
# 谢谢大家
# 全屏
# 就做
# 第一部分
# 图中
# 开源
# 组成部分
# 二十五
# 第二部分
# 未完成
# 像我这样
相关文章:
如何快速搭建高效WAP手机网站吸引移动用户?
招商网站制作流程,网站招商广告语?
建站之星体验版:智能建站系统+响应式设计,多端适配快速建站
b2c电商网站制作流程,b2c水平综合的电商平台?
如何在宝塔面板创建新站点?
如何通过虚拟主机快速搭建个人网站?
娃派WAP自助建站:免费模板+移动优化,快速打造专业网站
一键制作网站软件下载安装,一键自动采集网页文档制作步骤?
如何在Tomcat中配置并部署网站项目?
如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法
如何零基础开发自助建站系统?完整教程解析
制作网站的软件免费下载,免费制作app哪个平台好?
建站之星在线版空间:自助建站+智能模板一键生成方案
网站制作知乎推荐,想做自己的网站用什么工具比较好?
学校建站服务器如何选型才能满足性能需求?
如何做静态网页,sublimetext3.0制作静态网页?
建站之星安装失败:服务器环境不兼容?
已有域名建站全流程解析:网站搭建步骤与建站工具选择
移民网站制作流程,怎么看加拿大移民官网?
高防服务器:AI智能防御DDoS攻击与数据安全保障
制作国外网站的软件,国外有哪些比较优质的网站推荐?
如何快速查询网站的真实建站时间?
建站上市公司网站建设方案与SEO优化服务定制指南
已有域名如何快速搭建专属网站?
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
大学网站设计制作软件有哪些,如何将网站制作成自己app?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?
制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?
官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站
道歉网站制作流程,世纪佳缘致歉小吴事件,相亲网站身份信息伪造该如何稽查?
定制建站价位费用解析与套餐推荐全攻略
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
如何在阿里云高效完成企业建站全流程?
如何快速查询域名建站关键信息?
怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?
免费制作小说封面的网站有哪些,怎么接网站批量的封面单?
深圳网站制作案例,网页的相关名词有哪些?
制作证书网站有哪些,全国城建培训中心证书查询官网?
如何在自有机房高效搭建专业网站?
小说建站VPS选用指南:性能对比、配置优化与建站方案解析
平台云上自助建站如何快速打造专业网站?
婚礼视频制作网站,学习*后期制作的网站有哪些?
阿里云网站搭建费用解析:服务器价格与建站成本优化指南
高防服务器租用指南:配置选择与快速部署攻略
建站之星2.7模板快速切换与批量管理功能操作指南
网站建设制作、微信公众号,公明人民医院怎么在网上预约?
如何获取免费开源的自助建站系统源码?
如何在IIS中新建站点并配置端口与物理路径?
*请认真填写需求信息,我们会在24小时内与您取得联系。