近段时间的学习碎片整理(8)

1、MPAndroidLineChart的使用

 (1)引入依赖

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

(2)在对应的布局中放入控件

<?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.LineChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

(3)设置基本属性

setOnChartValueSelectedListener(chartValueSelectedListener)
 val chartValueSelectedListener = object : OnChartValueSelectedListener {
            override fun onValueSelected(e: Entry?, h: Highlight?) {
                e?.let {
                    binding.tideLineChart.centerViewToAnimated(
                        it.x, it.y, h?.let { it1 ->
                            binding.tideLineChart.data.getDataSetByIndex(it1.dataSetIndex)
                                .axisDependency
                        }, 500
                    )
                }
            }

            override fun onNothingSelected() {

            }

        }
//设置x轴
xAxis.apply {
                position = XAxis.XAxisPosition.BOTTOM
                setLabelCount(5, true)
                textSize = 14f
                gridColor = 0x00000000
                axisMaximum = 1440f
                val valueFormatter = object :ValueFormatter(){
                
                    //这个是用来格式化x轴上面的数据,例如数字48转为00:48 60转为01:00

                }
                this.valueFormatter = valueFormatter
}
//设置y轴
 axisLeft.apply {
                textSize = 14f
                axisMinimum = -2f
                axisMaximum = 10f
            }
            axisRight.apply {
                isEnabled = false
                axisMinimum = 0f
            }
            //设置是否可以缩放 x和y,默认true
            setScaleEnabled(false)
            //设置是否可以通过双击屏幕放大图表。默认是true
            isDoubleTapToZoomEnabled = false
            //是否启用网格背景
            setDrawGridBackground(false)
//            isHighlightPerDragEnabled = true
//            setPinchZoom(true)
            //图表背景颜色的设置
            setBackgroundResource(Color.White)

            //描述信息
            val description = Description()
            description.text = "描述"
            description.textColor = Color.RED
            description.isEnabled = true
            setDescription(description)
            
//设置表头
             legend.apply {
                //表头代表线条说明前的图形 可以设置线形,圆形,方形
//                form = Legend.LegendForm.SQUARE
                //表头字体大小
                textSize = 15f
                //表头线条说明的颜色
                textColor = R.color.red
                //表头线条放置的位置
                verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
                horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
                //表头多条线条的排列方式
                orientation = Legend.LegendOrientation.HORIZONTAL
                //表头的说明是否绘制到图表内部
//                setDrawInside(false)
            }

(4)点击对应点显示y轴上的数据可以这么做(可参考MPAndroidChart 3.0——LineChart(折线图) - S丶black - 博客园)

首先自定义markview

package com.tidal.hisea.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.tidal.hisea.R;

/**
 *自定义MyMarkerView
 */
public class MyMarkerView extends MarkerView {

    private TextView tvContent;

    public MyMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        tvContent= (TextView) findViewById(R.id.tvContent);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

            tvContent.setText("" + ce.getHigh() + "m");
        } else {

            tvContent.setText("" + e.getY() + "m");
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}

  layout_markview

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="40dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginBottom="5dp"
    android:background="@drawable/background_x_y">

    <TextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        tools:text="000"
        android:textSize="12sp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</androidx.constraintlayout.widget.ConstraintLayout>
   val mv = MyMarkerView(mContext!!, R.layout.layout_markview)
            mv.chartView = mchart
            mchart.marker = mv

2、一个不错的自定义控件ZLoadingDialog 类似于progressbar

// ZLoadingDialog
    implementation 'com.zyao89:zloading:1.2.0'

1、初始化

 zLoadingDialog = ZLoadingDialog(mContext!!)
        zLoadingDialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)
            .setLoadingColor(Color.parseColor("#FF7A21"))
            .setHintText("加载中...")
            .setHintTextSize(16f) // 设置字体大小 dp
            .setHintTextColor(Color.GRAY)  // 设置字体颜色
            .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
            .setDialogBackgroundColor(Color.WHITE) // 设置背景色,默认白色

使用时show和dismiss即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值