<Android开源库> MPAndroidChart Wiki(译文)~Part 4
来源:互联网 发布:优化营商环境总结报告 编辑:程序博客网 时间:2024/06/05 22:54
16. 动画
注意:本章的动画效果只会在API 11(Android3.0.x)及以上的Android版本上生效
在低于上述的Android版本中,动画将不会被执行,并不会导致程序崩溃。
所有类型的图标都可以用一种看上去比较炫酷的动画效果来进行构建。
三种不同的动画方法来让我们在X轴,Y轴或则两个轴同时显示动画效果。
mChart.animateX(3000); // animate horizontal 3000 milliseconds// or:mChart.animateY(3000); // animate vertical 3000 milliseconds// or:mChart.animateXY(3000, 3000); // animate horizontal and vertical 3000 milliseconds
任意一种 animate(…) 动画方法被调用后,无需再调用 invalidate() 方法。
16.1 缓动动画
这个库可以让你对动画应用”缓动函数”。
你可以选择以下预定义的静态 Easing.EasingOption :
public enum EasingOption { Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce, }
基本上,有以下两种方式进行 easing 你的动画。
1. 预定义的缓动选项:(下面代码可在所有 Android 版本运行)
public void animateY(int durationmillis, Easing.EasingOption option);
例如,调用带有预定义缓动选项的动画方法
// animate both axes with easingmChart.animateY(3000, Easing.EasingOption.EaseOutBack);
当你想代码运行在 Android 3.0 (API 11) 以下时,要使用 Easing.EasingOption 。
2. 自定义缓动函数(在 Android 3.0 自定义缓动函数会使应用 crash):
public void animateY(int durationmillis, EasingFunction function);
通过创建你自己的easing-function类并且实现EasingFunction接口,你可以实现你自己的easing功能
/** * Interface for creating custom made easing functions. */ public interface EasingFunction { /** * Called everytime the animation is updated. * @param input - the time passed since the animation started (value between 0 and 1) */ public float getInterpolation(float input); }
然后,按照如下方式使用(注意,这个不能在小于Android3.0的版本上使用,将会导致程序崩溃)
// animate both axes with easingmChart.animateY(3000, new MyEasingFunction());
17. IMarker接口
自版本v3.0.0以来,图表中的标记(弹出窗口)由IMarker接口表示。
17.1 IMarker接口
这个接口可以帮助你实现在图表中条目高亮的时候显示自定义的Marker窗口。这个接口给我们提供了如下需要实现的方法:
public interface IMarker { /** * @return The desired (general) offset you wish the IMarker to have on the x- and y-axis. * By returning x: -(width / 2) you will center the IMarker horizontally. * By returning y: -(height / 2) you will center the IMarker vertically. */ MPPointF getOffset(); /** * @return The offset for drawing at the specific `point`. This allows conditional adjusting of the Marker position. * If you have no adjustments to make, return getOffset(). * * @param posX This is the X position at which the marker wants to be drawn. * You can adjust the offset conditionally based on this argument. * @param posY This is the X position at which the marker wants to be drawn. * You can adjust the offset conditionally based on this argument. */ MPPointF getOffsetForDrawingAtPos(float posX, float posY); /** * This method enables a specified custom IMarker to update it's content every time the IMarker is redrawn. * * @param e The Entry the IMarker belongs to. This can also be any subclass of Entry, like BarEntry or * CandleEntry, simply cast it at runtime. * @param highlight The highlight object contains information about the highlighted value such as it's dataset-index, the * selected range or stack-index (only stacked bar entries). */ void refreshContent(Entry e, Highlight highlight); /** * Draws the IMarker on the given position on the screen with the given Canvas object. * * @param canvas * @param posX * @param posY */ void draw(Canvas canvas, float posX, float posY);}
17.2 创建一个标记视图
为了创建一个自定义的标记视图,你需要创建一个实现了IMarker接口的类:
public class YourMarkerView implements IMarker { ... }
实现IMarker接口时返回什么样的值,完全取决于你自己的需求。所以,看看上面关于每个方法的介绍是很有必要的。
除了通过实现IMarker接口来创建自己的标记视图,我们还可以通过继承下面提到的一个类来达到同样的目的。这个方法比较简单,并且我们也不需要实现IMarker接口中的所有方法。只有几个特定的方法需要我们复写和自定义。最重要的是复写refreshContent(…)方法来调整标记视图中的数据。一个简单的示例如下:
public class YourMarkerView extends MarkerView { private TextView tvContent; public MyMarkerView(Context context, int layoutResource) { super(context, layoutResource); // find your layout components tvContent = (TextView) findViewById(R.id.tvContent); } // callbacks everytime the MarkerView is redrawn, can be used to update the // content (user-interface) @Override public void refreshContent(Entry e, Highlight highlight) { tvContent.setText("" + e.getY()); // this will perform necessary layouting super.refreshContent(e, highlight); } private MPPointF mOffset; @Override public MPPointF getOffset() { if(mOffset == null) { // center the marker horizontally and vertically mOffset = new MPPointF(-(getWidth() / 2), -getHeight()); } return mOffset; }}
17.3 获取/设置Marker
给图表设置一个Marker,使用setMarker(…)方法:
IMarker marker = new YourMarkerView();chart.setMarker(marker);
要访问图表上的一个Marker,使用getMarker()方法:
IMarker marker = chart.getMarker();
17.4 预定义Markers
除了你自定义的MarkerView,开源库中也给我们提供了几个预定义的markers以供方便快捷的使用。其中包括:
- MarkerView :基本marker。允许提供布局文件作为图表上呈现的标记。继承这个类并且复写refreshContent(…)方法来调整标记视图中的内容。
- MarkerImage:一种绘制图片的标记视图。允许提供一个图片资源作为图表上的标记视图。继承这个类并且复写refreshContent(…)方法来调整标记视图中的内容。
17.5 旧版MarkerView
版本v3.0.0 之前,MarkerView类负责在图表高亮部分绘制标记视图。详情请查阅老版的the old MarkerView wiki page.
18. ChartData类
本章旨在让你更深入的了解MPAndroidChart中的数据模型。
ChartData类是像LineData,BarData等等这些数据类的父类。setData()方法可以将数据设置到图表中。
public class LineData extends ChartData { ...}
下面介绍一些ChartData类中的方法,可以直接在它的子类中使用。
18.1 数据样式
18.2 Getter方法
18.3 清除
18.4 选中高亮
18.5 动态数据
想了解如何在数据对象上增加或者移除数据,请查阅dynamic & realtime data章节
- <Android开源库> MPAndroidChart Wiki(译文)~Part 4
- <Android开源库> MPAndroidChart Wiki(译文)~Part 1
- <Android开源库> MPAndroidChart Wiki(译文)~Part 2
- <Android开源库> MPAndroidChart Wiki(译文)~Part 3
- <Android开源库> MPAndroidChart Wiki(译文)~Part 5
- <Android开源库> MPAndroidChart Wiki(译文)~Part 6
- MPAndroidChart Library Wiki翻譯
- MPAndroidChart Library Wiki翻譯
- <Android开源库> Essential介绍 <译文>
- SolrCloud:根据Solr Wiki的译文
- android wiki
- Android译文
- Android统计图表MPAndroidChart
- Android统计图表MPAndroidChart
- Android统计图表MPAndroidChart
- android 统计图表MPAndroidChart
- Android统计图表MPAndroidChart
- Android统计图表MPAndroidChart
- [音之国度进度]团队探索系统
- Mybatis实现原理
- caffe训练数据时出现了Check failed: data_怎么办?
- sublime text 2注册码
- 没想好
- <Android开源库> MPAndroidChart Wiki(译文)~Part 4
- 菜鸟教程之shell _1
- Linux for Ubuntu Java環境變量配置
- JZOJ1281【USACO题库】3.1.6 Stamps邮票
- Data invert to BCD
- iOS 开发中的争议(二)UI到底应该用xib/storyboard完成,还是用手写代码来完成?
- Linux 命令笔记
- jsp
- 一元多项式求和的实现