android 第三方jar 包 替换源码 使用

最近要修改一点表格相关的东西

我也没怎么研究过,今天刚改完 ,做一个总结

我用的https://github.com/PhilJay/MPAndroidChart
git clone git@github.com:PhilJay/MPAndroidChart.git
因为需要对表格做一些定制,所以我直接把jar 包替换成 开源代码进行编译

  1. clone 代码
    clone 代码 在MPAndroidChart/MPChartLib 有一个库,需要导入到android 项目

  2. 首先自己创建一个android 项目 如下(已经包含MPChartLib)
    这里写图片描述

  3. 项目下导入 MPChartLib
    new -> import module 选择 MPChartLib
    这里写图片描述

4.将 MPChartLib 配置成 app模块 的lib库使用

可以看到 MPChartLib 已经配置成库 查看MPChartLib模块的build.gradle

apply plugin: 'com.android.library'
apply plugin: 'maven'
//apply plugin: 'com.github.dcendents.android-maven'
//apply plugin: 'realm-android'

所以需要进入app 模块的build.gradle 修改 ,设置MPChartLib为依赖

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+    implementation project(':MPChartLib')
}

再写几个代码测试下

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLineChart = (LineChart) findViewById(R.id.chart1);
        initChart();
        initData();


    }
    //设置chart基本属性
    private void initChart() {
        //描述信息
        Description description = new Description();
        description.setText("我是描述信息");
        //设置描述信息
        mLineChart.setDescription(description);
        //设置没有数据时显示的文本
        mLineChart.setNoDataText("没有数据喔~~");
        //设置是否绘制chart边框的线
        mLineChart.setDrawBorders(true);
        //设置chart边框线颜色
        mLineChart.setBorderColor(Color.GRAY);
        //设置chart边框线宽度
        mLineChart.setBorderWidth(1f);
        //设置chart是否可以触摸
        mLineChart.setTouchEnabled(true);
        //设置是否可以拖拽
        mLineChart.setDragEnabled(true);
        //设置是否可以缩放 x和y,默认true
        mLineChart.setScaleEnabled(false);
        //设置是否可以通过双击屏幕放大图表。默认是true
        mLineChart.setDoubleTapToZoomEnabled(false);
        //设置chart动画
        mLineChart.animateXY(1000, 1000);

        //=========================设置图例=========================
        // 像"□ xxx"就是图例
        Legend legend = mLineChart.getLegend();
        //设置图例显示在chart那个位置 setPosition建议放弃使用了
        //设置垂直方向上还是下或中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        //设置水平方向是左边还是右边或中
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        //设置所有图例位置排序方向
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //设置图例的形状 有圆形、正方形、线
        legend.setForm(Legend.LegendForm.CIRCLE);
        //是否支持自动换行 目前只支持BelowChartLeft, BelowChartRight, BelowChartCenter
        legend.setWordWrapEnabled(true);


        //=======================设置X轴显示效果==================
        XAxis xAxis = mLineChart.getXAxis();
        //是否启用X轴
        xAxis.setEnabled(true);
        //是否绘制X轴线
        xAxis.setDrawAxisLine(true);
        //设置X轴上每个竖线是否显示
        xAxis.setDrawGridLines(true);
        //设置是否绘制X轴上的对应值(标签)
        xAxis.setDrawLabels(true);
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置竖线为虚线样式
        // xAxis.enableGridDashedLine(10f, 10f, 0f);
        //设置x轴标签数
        xAxis.setLabelCount(5, true);
        //图表第一个和最后一个label数据不超出左边和右边的Y轴
        // xAxis.setAvoidFirstLastClipping(true);

        //设置限制线 12代表某个该轴某个值,也就是要画到该轴某个值上
        LimitLine limitLine = new LimitLine(12);
        //设置限制线的宽
        limitLine.setLineWidth(1f);
        //设置限制线的颜色
        limitLine.setLineColor(Color.RED);
        //设置基线的位置
        limitLine.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
        limitLine.setLabel("基线 水位线");
        //设置限制线为虚线
        limitLine.enableDashedLine(10f, 10f, 0f);


        //=================设置左边Y轴===============
        YAxis axisLeft = mLineChart.getAxisLeft();
        //左边Y轴添加限制线
        axisLeft.addLimitLine(limitLine);
        //是否启用左边Y轴
        axisLeft.setEnabled(true);
        //设置最小值
        axisLeft.setAxisMinimum(1);
        //设置最大值
        axisLeft.setAxisMaximum(20);
        //设置横向的线为虚线
        axisLeft.enableGridDashedLine(10f, 10f, 0f);
        //axisLeft.setDrawLimitLinesBehindData(true);




        //====================设置右边的Y轴===============
        YAxis axisRight = mLineChart.getAxisRight();
        //是否启用右边Y轴
        axisRight.setEnabled(true);
        //设置最小值
        axisRight.setAxisMinimum(1);
        //设置最大值
        axisRight.setAxisMaximum(20);
        //设置横向的线为虚线
        axisRight.enableGridDashedLine(10f, 10f, 0f);

    }


    //设置数据
    private void initData() {
        //每个点的坐标
        pointValues = new ArrayList<>();
        for (int i = 1; i < 19; i++) {
            int y = (int)( Math.random() * 20);
            pointValues.add(new Entry(i, y));

        }

        //点构成的某条线
        LineDataSet lineDataSet = new LineDataSet(pointValues, "标签1");
        //设置该线的颜色
        lineDataSet.setColor(Color.RED);
        //设置每个点的颜色
        lineDataSet.setCircleColor(Color.YELLOW);
        //设置该线的宽度
        lineDataSet.setLineWidth(1f);
        //设置每个坐标点的圆大小
        //lineDataSet.setCircleRadius(1f);
        //设置是否画圆
        lineDataSet.setDrawCircles(false);
        // 设置平滑曲线模式
        //  lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        //设置线一面部分是否填充颜色
        lineDataSet.setDrawFilled(true);
        //设置填充的颜色
        lineDataSet.setFillColor(Color.BLUE);
        //设置是否显示点的坐标值
        lineDataSet.setDrawValues(false);

        //线的集合(可单条或多条线)
        List<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(lineDataSet);
        //把要画的所有线(线的集合)添加到LineData里
        LineData lineData = new LineData(dataSets);
        //把最终的数据setData
        mLineChart.setData(lineData);

    }
}

然后就可以跑通 结果如下
这里写图片描述

以上基本上就是,如果想在其他项目中使用MyChartLib,就像一个平常的包拷贝 com.github.mikephil.charting 内容,将之前配置jar 包的部分删除 ,修改Android.mk 编译脚本删除编译引用lib库的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值