我在我的应用程序中使用Android 绘图图表库条形图。我想在图表呈现时应用过渡动画。
例如:条形图从 0 平稳上升到图表中的当前值。
帮助我实现这一目标。
提前致谢。
Keshaw 是正确的 - 没有“内置”动画效果,但是因为该库是为实时显示而设计的,所以创建自己的效果并不难。这是 DemoApp 中 SimpleXYPlot 示例之上的功能示例:(AnimatedSeries 实现是您感兴趣的部分)
/**
* A straightforward example of using AndroidPlot to plot some data.
*/
public class SimpleXYPlotActivity extends Activity
{
private XYPlot plot;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_xy_plot_example);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// Create a couple arrays of y-values to plot:
Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1"); // Set the display title of the series
// same as above
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
// Create a formatter to use for drawing a series using LineAndPointRenderer
// and configure it from xml:
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getApplicationContext(),
R.xml.line_point_formatter_with_plf1);
AnimatedSeries as1 = new AnimatedSeries(plot, series1);
// add a new series' to the xyplot:
plot.addSeries(as1, series1Format);
// same as above:
LineAndPointFormatter series2Format = new LineAndPointFormatter();
series2Format.setPointLabelFormatter(new PointLabelFormatter());
series2Format.configure(getApplicationContext(),
R.xml.line_point_formatter_with_plf2);
plot.addSeries(series2, series2Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
new Thread(as1).start();
}
/**
* A primitive example of applying an animation to a series
*/
class AnimatedSeries implements XYSeries, Runnable {
private final XYPlot plot;
private final XYSeries series;
private int step = 0;
private int stepCount = 25;
private float factor = 0;
public AnimatedSeries(XYPlot plot, XYSeries series) {
this.plot = plot;
this.series = series;
}
@Override
public void run() {
try {
while (step < stepCount) {
factor = step / (float) stepCount;
Thread.sleep(50);
plot.redraw();
step++;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public int size() {
return series.size();
}
@Override
public Number getX(int index) {
return index;
}
@Override
public Number getY(int index) {
return series.getY(index).doubleValue() * factor;
}
@Override
public String getTitle() {
return series.getTitle();
}
}
}