我最終在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(...)'?也許? –