mpandroidchart – 条形图中的y值变化动画

前端之家收集整理的这篇文章主要介绍了mpandroidchart – 条形图中的y值变化动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在barChart上调用animateY时,将重绘整个图表,将条形图从y-zero设置为y-new.
barChart.invalidate();
barChart.animateY(1000);

是否可以将动画限制为值更改.因此,允许用户看到图表如何从y-old(例如100)增长到y-new(例如120)?

解决方法

我遇到了同样的问题,无法找到解决方案.所以,我创建了一个可以调用来处理这种任务的类.
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.data.Entry;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.os.Handler;

public class AnimateDataSetChanged {
    private int duration;
    private long startTime;
    private int fps = 30;
    private Handler timerHandler;
    private Chart chart;
    private List<Entry> oldData;
    private List<Entry> newData;
    private Interpolator interpolator;

    public AnimateDataSetChanged(int duration,Chart chart,List<Entry> oldData,List<Entry> newData){
        this.duration = duration;
        this.chart = chart;
        this.oldData = new ArrayList<>(oldData);
        this.newData = new ArrayList<>(newData);
        interpolator = new LinearInterpolator();
    }

    public void setInterpolator(Interpolator interpolator){
        this.interpolator = interpolator;
    }

    public void run(int fps){
        this.fps = fps;
        run();
    }

    public void run(){
        startTime = Calendar.getInstance().getTimeInMillis();
        timerHandler = new Handler();
        Runner runner = new Runner();
        runner.run();
    }

    private class Runner implements Runnable{
        @Override
        public void run() {
            float increment = (Calendar.getInstance().getTimeInMillis() - startTime) / (float)duration;
            increment = interpolator.getInterpolation(increment < 0f ? 0f : increment > 1f ? 1f :increment);
            chart.getData().getDataSetByIndex(0).clear();
            for(int i = 0; i < newData.size(); i++){
                float oldY = oldData.size() > i ? oldData.get(i).getY() : newData.get(i).getY();
                float oldX = oldData.size() > i ? oldData.get(i).getX() : newData.get(i).getX();
                float newX = newData.get(i).getX();
                float newY = newData.get(i).getY();
                Entry e = new Entry(oldX + (newX - oldX) * increment,oldY + (newY - oldY) * increment);
                chart.getData().getDataSetByIndex(0).addEntry(e);
            }
            chart.getXAxis().resetAxisMaximum();
            chart.getXAxis().resetAxisMinimum();
            chart.notifyDataSetChanged();
            chart.refreshDrawableState();
            chart.invalidate();
            if(chart instanceof BarLineChartBase){
                ((BarLineChartBase)chart).setAutoScaleMinMaxEnabled(true);
            }
            if(increment < 1f){
                timerHandler.postDelayed(this,1000/fps);
            }
        }
    }
}

你可以像这样调用这个类:

List<Entry> oldEntries = ...
List<Entry> newEntries = ...

AnimateDataSetChanged changer = new AnimateDataSetChanged(600,mChart,oldEntries,currentDataEntries);
changer.setInterpolator(new AccelerateInterpolator()); // optionally set the Interpolator
changer.run();
原文链接:https://www.f2er.com/android/317722.html

猜你在找的Android相关文章