tzt1000
tzt1000
采纳率14.3%
2016-07-02 08:01 阅读 2.1k

请问mpandroidchart如何实现动画连续不断的重载,动画如何暂停

50

新手程序员一枚,现在需要我要做一个连续不断的波形图,因为要实现手势操作,所以采用了mpandroidchart
,我在示例代码上面进行改动的,但是我无论如何只能实现一次动画效果。求大神们帮助,现在附上代码

public class LineWaveActivity extends DemoBase implements
OnSeekBarChangeListener, OnChartGestureListener,
OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private TextView tvX, tvY;
// -----------------波形曲线可能会用到的参数(尖波)--------------
private List> month = new ArrayList>();
private List> data = new ArrayList>();
// 数据
// x轴数据——单位秒(s)
private String[] mTimes = new String[] { "00:00s", "00:10s", "00:20s",
"00:30s", "00:40s", "00:50s", "00:60s", "00:70s", "00:80s",
"00:90s", "00:10s", "01:10s", "01:20s", "01:30s", "01:40s",
"01:50s", "01:60s", "01:70s", "01:80s", "01:90s", "02:00s",
"02:10s", "02:20s", "02:30s", "02:40s", "02:50s", "02:60s",
"02:70s", "02:80s", "02:90s", "03:00s", "03:10s", "03:20s",
"03:30s", "03:40s", "03:50s", "03:60s", "03:70s", "03:80s",
"03:90s", "04:00s", "04:10s", "04:20s", "04:30s", "04:40s",
"04:50s", "04:60s", "04:70s", "04:80s", "04:90s", "05:00s",
"05:10s", "05:20s", "05:30s", "05:40s", "05:50s", "05:60s",
"05:70s", "05:80s", "05:90s", "06:00s" };
// y轴数据——单位(n/m^2)
private int[] arrs = { 0, 0, 0, 0, 0, 0, 5, -10, 15, -20, 25, -25, 20, -15,
10, -5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -10, 15, -20, 25, -25, 20,
-15, 10, -5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -10, 15, -20, 25,
-25, 20, -15, 10, -5, 0, 0, 0, 0, 0 };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置窗体全屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_linechart);
    // ---------------波形曲线操作代码块----------------------
    Map<String, Object> map = null;
    for (int i = 0; i < mTimes.length; i++) {
        map = new HashMap<String, Object>();
        map.put("times", mTimes[i]);
        month.add(map);
    }

    Map<String, Object> dataMap = null;
    for (int i = 0; i < arrs.length; i++) {
        dataMap = new HashMap<String, Object>();
        dataMap.put("ranges", arrs[i]);
        data.add(dataMap);
    }

    // ---------------波形曲线操作代码块----------------------
    tvX = (TextView) findViewById(R.id.tvXMax);
    tvY = (TextView) findViewById(R.id.tvYMax);

    mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
    mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);

    mSeekBarX.setProgress(250);
    mSeekBarY.setProgress(100);

    mSeekBarY.setOnSeekBarChangeListener(this);
    mSeekBarX.setOnSeekBarChangeListener(this);

    mChart = (LineChart) findViewById(R.id.chart1);
    // ChartGestureListener手势监听器,可以使得 chart与手势操作进行交互。
    mChart.setOnChartGestureListener(this);
    // 设置表格上的点,被点击的时候,的回调函数
    mChart.setOnChartValueSelectedListener(this);
    // 设置图表内格子背景是否显示,默认是false
    mChart.setDrawGridBackground(false);
    // 数据描述
    // 如果没有数据的时候,会显示这个"You need to provide data for the chart.",类似listview的emtpyview
    mChart.setDescription("");
    mChart.setNoDataTextDescription("你需要为图表添加必要的数据!");

    // 设置点击value的时候,是否高亮显示
    mChart.setHighlightEnabled(true);

    // 设置是否可以手势操作
    mChart.setTouchEnabled(true);

    // 启用/禁用拖动(平移)图表。
    mChart.setDragEnabled(true);
    // 启用/禁用缩放图表上的两个轴。
    mChart.setScaleEnabled(true);
    // 启用/禁用x轴缩放
    mChart.setScaleXEnabled(true);
    // 启用/禁用y轴缩放
    mChart.setScaleYEnabled(true);

    // 如果设置为false,那么x轴,y轴可以分别放大
    mChart.setPinchZoom(false);

    // 设置背景颜色
    // mChart.setBackgroundColor(Color.GRAY);
    MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
    mChart.setMarkerView(mv);

    // 设置x轴相关参数
    LimitLine llXAxis = new LimitLine(10f, "警戒线");
    llXAxis.setLineWidth(4f);
    llXAxis.enableDashedLine(10f, 10f, 10f);
    llXAxis.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
    llXAxis.setTextSize(10f);

    XAxis xAxis = mChart.getXAxis();
    // xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
    // xAxis.addLimitLine(llXAxis); // add x-axis limit line

    Typeface tf = Typeface.createFromAsset(getAssets(),
            "OpenSans-Regular.ttf");

    LimitLine ll1 = new LimitLine(50f, "Upper Limit");
    // 设置警戒线粗细
    ll1.setLineWidth(10f);
    // 设置警戒线间隔大小(间隔宽度,线段位置,线段宽度)
    ll1.enableDashedLine(40f, 40f, 40f);
    ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
    ll1.setTextSize(10f);
    // 设置字体样式
    ll1.setTypeface(tf);

    LimitLine ll2 = new LimitLine(-50f, "Lower Limit");
    ll2.setLineWidth(10f);
    ll2.enableDashedLine(40f, 40f, 40f);
    ll2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
    ll2.setTextSize(10f);
    ll2.setTypeface(tf);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.removeAllLimitLines(); // reset all limit lines to avoid
                                    // overlapping lines
    leftAxis.addLimitLine(ll1);
    leftAxis.addLimitLine(ll2);
    // 设置y轴最大值
    leftAxis.setAxisMaxValue(50f);
    // 设置y轴最小值
    leftAxis.setAxisMinValue(-50f);
    // 设置y轴起始点是否为0
    leftAxis.setStartAtZero(false);
    // leftAxis.setYOffset(20f);
    // 设置横向表格为虚线
    leftAxis.enableGridDashedLine(10f, 10f, 0f);
    leftAxis.setDrawLimitLinesBehindData(true);
    // 设置隐藏右边的坐标轴
    mChart.getAxisRight().setEnabled(false);
    // 设置相对于x,y的放大倍率
    // mChart.getViewPortHandler().setMaximumScaleY(20f);
    // mChart.getViewPortHandler().setMaximumScaleX(20f);
    // mChart.setVisibleXRange(20);//设置频幕能显示点的数量,要和MAX和Min结合起来使用
    // mChart.setVisibleYRange(20f, AxisDependency.LEFT);
    // mChart.centerViewTo(20, 50, AxisDependency.LEFT);
    //设置绘图事件,单位——毫秒,绘图程序入口!!!!!!!!!!!!!
    setData(45, 100);
     mChart.animateX(6000, Easing.EasingOption.EaseInOutQuart);
     mChart.invalidate();// 界面刷新
    Legend l = mChart.getLegend();
    // l.setPosition(LegendPosition.LEFT_OF_CHART);//设置中轴线向右移
    l.setForm(LegendForm.LINE);// 设置图表最下面显示的类型
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {

    tvX.setText("" + (mSeekBarX.getProgress()));
    tvY.setText("" + (mSeekBarY.getProgress()));

    setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());

    // 刷新图标
    mChart.invalidate();
}

// 进度条拖动开始时候调用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

// 进度条拖动完成之后调用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

private void setData(int count1, int count2) {
    count1 = month.size();
    count2 = data.size();
    ArrayList<String> xVals = new ArrayList<String>();
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    // x轴显示数据操作
    for (int i = 0; i < count1; i++) {
        Map<String, Object> data = month.get(i);
        xVals.add(data.get("times").toString());
    }
    // y轴显示数据操作
    for (int i = 0; i < count2; i++) {
        Map<String, Object> data2 = data.get(i);
        yVals.add(new Entry(Integer
                .parseInt(data2.get("ranges").toString()), i));

    }

    // create a dataset and give it a type
    LineDataSet set1 = new LineDataSet(yVals, "——————");
    set1.setFillAlpha(110);// 设置填充颜色透明度
    set1.setFillColor(Color.RED);

    // 设置线的样式
    set1.enableDashedLine(10f, 5f, 0f);
    // set1.enableDashedHighlightLine(10f, 5f, 0f);
    set1.setColor(Color.BLACK);
    set1.setCircleColor(Color.BLACK);
    set1.setLineWidth(1f);
    set1.setCircleSize(4f);
    set1.setDrawCircleHole(true);// 点的圆圈样式,true为空心点,flase为实心点
    set1.setValueTextSize(9f);
    set1.setFillAlpha(65);
    set1.setFillColor(Color.BLACK);
    // set1.setDrawFilled(true);
    // set1.setShader(new LinearGradient(0, 0, 0, mChart.getHeight(),
    // Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));

    ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
    dataSets.add(set1); // add the datasets

    // create a data object with the datasets
    LineData data = new LineData(xVals, dataSets);

    // set data     
    Log.i("SumLog", "开始为图形设置数据");
    mChart.setData(data);
}

@Override
public void onChartGestureStart(MotionEvent me,
        ChartTouchListener.ChartGesture lastPerformedGesture) {
    Log.i("Gesture", "START");
}

@Override
public void onChartGestureEnd(MotionEvent me,
        ChartTouchListener.ChartGesture lastPerformedGesture) {
    Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture);

    if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
        mChart.highlightValues(null);
}

@Override
public void onChartLongPressed(MotionEvent me) {
    Log.i("LongPress", "Chart longpressed.");
}

@Override
public void onChartDoubleTapped(MotionEvent me) {
    Log.i("DoubleTap", "Chart double-tapped.");
}

@Override
public void onChartSingleTapped(MotionEvent me) {
    Log.i("SingleTap", "Chart single-tapped.");
}

@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX,
        float velocityY) {
    Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: "
            + velocityY);
}

@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
    Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
}

@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
    Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY);
}

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
    Log.i("Entry selected", e.toString());
    Log.i("", "low: " + mChart.getLowestVisibleXIndex() + ", high: "
            + mChart.getHighestVisibleXIndex());
}

@Override
public void onNothingSelected() {
    Log.i("Nothing selected", "Nothing selected.");

}

}

请问各位,如何实现一个可用按钮控制的连续不断的波形效果,比较急额,卡了四天了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐