这篇文章主要介绍如何使用MPAndroidChart库实现K线面板的相关功能,我们会着重介绍以下几个方面:
1.绘制K线图和展示股票数据
2.处理用户手势操作
3.拉伸和压缩K线图
最终的效果图:图1
绘制K线图和展示股票数据
绘制K线图需要用到真实的股票数据,网上有很多免费的接口可以使用,我们也可以自己编写一个股票数据接口 Python实现股票数据接口
1.将MPAndroidChart集成到项目中
在project根目录的build.gradle添加中央库地址:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
在项目build.gradle中添加相关依赖:
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}
2.在布局文件中添加CandleStickChart
android:id="@+id/candler_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/top_line"
android:layout_above="@+id/rl_bottom_view"/>
mChart = findViewById(R.id.candler_chart);
3.基本参数设置
设置提示文字
mChart.setNoDataTextDescription("加载中...");//如果没有数据的时候,会显示这个
设置背景颜色
mChart.setDrawGridBackground(false);//是否显示表格颜色mChart.setBackgroundColor(Color.BLACK);//设置背景mChart.setGridBackgroun