AndroidPlot条形图在图中添加过渡动画

时间:2014-03-20 09:29:54

标签: android charts android-animation androidplot

我在我的应用程序中使用Android Plot chart Library条形图表。我想在渲染图表时应用过渡动画。

例如:条形图从0平滑上升到图表中的当前值。

帮助我实现这一目标。

提前致谢。

1 个答案:

答案 0 :(得分:1)

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();
        }
    }
}