2016-02-18 104 views
0

經過幾個小時的嘗試,我正在尋找一些關於如何向MPAndroid添加捕捉滾動機制的提示。基本上我想要5個可見的酒吧對齊,以便他們完全可見和居中。我現在導入了庫源代碼,因爲看起來沒有其他方法可以更改computeScroll(BarLineChartTouchListener)中的代碼。MPAndroid - 滾動時捕捉x位置

編輯: 澄清 - 我顯示大約20個小節,但圖表放大以便用戶可以水平滾動。讓我困擾的是它並沒有自動對齊,所以第一個可見的條可能會被截斷。我正在尋找捕捉效果,它會將位置四捨五入到最接近的條寬度乘法,從而留下5個完全可見的條。

+0

對您的問題的更多解釋將會有所幫助。 –

+0

@Rod_Algonquin - 對不起。我添加了解釋,希望現在可以更容易地看到我的問題在哪裏。 – wtk

+0

'chart.moveViewTo(...)'?也許? –

回答

1

我最終在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結束。

我保留變量的名稱,因爲我從MoveViewJobViewPortHandler等函數複製了代碼。由於它只在x軸上對齊 - 我移除了Y軸計算並使用了零代替。任何優化都歡迎,特別是作者@PhilippJahoda。