我正在跟踪this链接以显示android的动画图。
我已经实现了Stackbar图表,它可以正确显示,但是当我尝试单击未调用的bar click事件时,并且当用户单击每个条形的左侧时,就会得到click事件。
请检查以下代码以计算条形位置和手势检测器。
有人知道这个问题吗?
protected void calculateBarPositions(int _DataSize) {
int dataSize = mScrollEnabled ? mVisibleBars : _DataSize;
float barWidth = mBarWidth;
float margin = mBarMargin;
if (!mFixedBarWidth) {
// calculate the bar width if the bars should be dynamically displayed
barWidth = (mAvailableScreenSize / _DataSize) - margin;
} else {
if (_DataSize < mVisibleBars) {
dataSize = _DataSize;
}
// calculate margin between bars if the bars have a fixed width
float cumulatedBarWidths = barWidth * dataSize;
float remainingScreenSize = mAvailableScreenSize - cumulatedBarWidths;
margin = remainingScreenSize / dataSize;
}
boolean isVertical = isVerticalAlignment;
int calculatedSize = (int) ((barWidth * _DataSize) + (margin * _DataSize));
int contentWidth = isVertical ? mGraphWidth : calculatedSize;
int contentHeight = isVertical ? calculatedSize : mGraphHeight;
mContentRect = new Rect(0, 0, contentWidth, contentHeight);
mCurrentViewport = new RectF(0, 0, mGraphWidth, mGraphHeight);
calculateBounds(barWidth, margin);
mLegend.invalidate();
mGraph.invalidate();
}
/**
* The gesture listener, used for handling simple gestures such as double touches, scrolls,
* and flings.
*/
private final GestureDetector.SimpleOnGestureListener mGestureListener
= new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (mCurrentViewport.left + distanceX > mContentRect.left && mCurrentViewport.right + distanceX < mContentRect.right) {
mCurrentViewport.left += distanceX;
mCurrentViewport.right += distanceX;
}
if (mCurrentViewport.top + distanceY > mContentRect.top && mCurrentViewport.bottom + distanceY < mContentRect.bottom) {
mCurrentViewport.top += distanceY;
mCurrentViewport.bottom += distanceY;
}
invalidateGlobal();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
fling((int) -velocityX, (int) -velocityY);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// The user is interacting with the pie, so we want to turn on acceleration
// so that the interaction is smooth.
if (!mScroller.isFinished()) {
stopScrolling();
}
return true;
}
};
private void fling(int velocityX, int velocityY) {
mScroller.fling(
(int) mCurrentViewport.left,
(int) mCurrentViewport.top,
velocityX,
velocityY,
0, mContentRect.width() - mGraphWidth,
0, mContentRect.height() - mGraphHeight);
// Start the animator and tell it to animate for the expected duration of the fling.
mScrollAnimator.setDuration(mScroller.getDuration());
mScrollAnimator.start();
}
private void tickScrollAnimation() {
if (!mScroller.isFinished()) {
mScroller.computeScrollOffset();
int currX = mScroller.getCurrX();
int currY = mScroller.getCurrY();
if (currX > mContentRect.left && currX + mGraphWidth < mContentRect.right) {
mCurrentViewport.left = currX;
mCurrentViewport.right = currX + mGraphWidth;
}
if (currY > mContentRect.top && currY + mGraphHeight < mContentRect.bottom) {
mCurrentViewport.top = currY;
mCurrentViewport.bottom = currY + mGraphHeight;
}
} else {
mScrollAnimator.cancel();
}
}