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

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

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

解决方法

我遇到了同样的问题,无法找到解决方案.所以,我创建了一个可以调用来处理这种任务的类.
  1. import android.view.animation.Interpolator;
  2. import android.view.animation.LinearInterpolator;
  3. import com.github.mikephil.charting.charts.BarLineChartBase;
  4. import com.github.mikephil.charting.charts.Chart;
  5. import com.github.mikephil.charting.data.Entry;
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.List;
  9. import android.os.Handler;
  10.  
  11. public class AnimateDataSetChanged {
  12. private int duration;
  13. private long startTime;
  14. private int fps = 30;
  15. private Handler timerHandler;
  16. private Chart chart;
  17. private List<Entry> oldData;
  18. private List<Entry> newData;
  19. private Interpolator interpolator;
  20.  
  21. public AnimateDataSetChanged(int duration,Chart chart,List<Entry> oldData,List<Entry> newData){
  22. this.duration = duration;
  23. this.chart = chart;
  24. this.oldData = new ArrayList<>(oldData);
  25. this.newData = new ArrayList<>(newData);
  26. interpolator = new LinearInterpolator();
  27. }
  28.  
  29. public void setInterpolator(Interpolator interpolator){
  30. this.interpolator = interpolator;
  31. }
  32.  
  33. public void run(int fps){
  34. this.fps = fps;
  35. run();
  36. }
  37.  
  38. public void run(){
  39. startTime = Calendar.getInstance().getTimeInMillis();
  40. timerHandler = new Handler();
  41. Runner runner = new Runner();
  42. runner.run();
  43. }
  44.  
  45. private class Runner implements Runnable{
  46. @Override
  47. public void run() {
  48. float increment = (Calendar.getInstance().getTimeInMillis() - startTime) / (float)duration;
  49. increment = interpolator.getInterpolation(increment < 0f ? 0f : increment > 1f ? 1f :increment);
  50. chart.getData().getDataSetByIndex(0).clear();
  51. for(int i = 0; i < newData.size(); i++){
  52. float oldY = oldData.size() > i ? oldData.get(i).getY() : newData.get(i).getY();
  53. float oldX = oldData.size() > i ? oldData.get(i).getX() : newData.get(i).getX();
  54. float newX = newData.get(i).getX();
  55. float newY = newData.get(i).getY();
  56. Entry e = new Entry(oldX + (newX - oldX) * increment,oldY + (newY - oldY) * increment);
  57. chart.getData().getDataSetByIndex(0).addEntry(e);
  58. }
  59. chart.getXAxis().resetAxisMaximum();
  60. chart.getXAxis().resetAxisMinimum();
  61. chart.notifyDataSetChanged();
  62. chart.refreshDrawableState();
  63. chart.invalidate();
  64. if(chart instanceof BarLineChartBase){
  65. ((BarLineChartBase)chart).setAutoScaleMinMaxEnabled(true);
  66. }
  67. if(increment < 1f){
  68. timerHandler.postDelayed(this,1000/fps);
  69. }
  70. }
  71. }
  72. }

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

  1. List<Entry> oldEntries = ...
  2. List<Entry> newEntries = ...
  3.  
  4. AnimateDataSetChanged changer = new AnimateDataSetChanged(600,mChart,oldEntries,currentDataEntries);
  5. changer.setInterpolator(new AccelerateInterpolator()); // optionally set the Interpolator
  6. changer.run();

猜你在找的Android相关文章