我最终在BarLineChartBase.java中添加了以下函数。我知道这不是很高雅,但似乎能完成这项工作。由于ValueAnimator的原因,它仅限于targetApi> 11。对于较低的API(我不喜欢),你可能需要看看nineoldandroid或其他一些动画循环技术。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void alignX() {
int count = this.getValueCount();
int xIndex = this.getLowestVisibleXIndex() + Math.round((this.getHighestVisibleXIndex() - this.getLowestVisibleXIndex())/2.0f);
float xsInView = this.getXAxis().getValues().size()/this.getViewPortHandler().getScaleX();
Transformer mTrans = this.getTransformer(YAxis.AxisDependency.LEFT);
float[] pts = new float[] { xIndex - xsInView/2f, 0 };
mTrans.pointValuesToPixel(pts);
final Matrix save = new Matrix();
save.set(this.getViewPortHandler().getMatrixTouch());
final float x = pts[0] - this.getViewPortHandler().offsetLeft();
final int frames = 20;
ValueAnimator valueAnimator = new ValueAnimator().ofInt(0, frames);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
int prev = -1;
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if((int) animation.getAnimatedValue() > prev) {
save.postTranslate(-x/(float)frames, 0);
BarLineChartBase.this.getViewPortHandler().refresh(save, BarLineChartBase.this, true);
}
prev = (int) animation.getAnimatedValue();
}
});
valueAnimator.start();
}
我触发它在computeScroll
功能的BarLineChartTouchListener
结束。
我保留变量的名称,因为我从MoveViewJob
,ViewPortHandler
等函数复制了代码。由于它只在x轴上对齐 - 我移除了Y轴计算并使用了零代替。任何优化都欢迎,特别是作者@PhilippJahoda。
来源
2016-02-23 09:30:38
wtk
对您的问题的更多解释将会有所帮助。 –
@Rod_Algonquin - 对不起。我添加了解释,希望现在可以更容易地看到我的问题在哪里。 – wtk
'chart.moveViewTo(...)'?也许? –