@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.width = w; this.height = h; itemWidth = Math.min(itemWidth, width); int number = height / itemHeight; if ((number & 1) == 1) { this.numberPerPage = number; } else { this.numberPerPage = number - 1; } this.topBottomPadding = (height - numberPerPage * itemHeight) / 2; this.leftRightPadding = (width - itemWidth) / 2; this.originalSelectedIndex = 0; this.selectedIndex = 0; itemList = new ArrayList<ItemView>(); for (int i = -numberPerPage / 2; i < stringList.size() + numberPerPage / 2; i++) { ItemView itemView = new ItemView(getContext()); itemView.setIndex(i); itemView.setyOffset((i + numberPerPage / 2) * itemHeight); LogUtil.i("chenxu", "onSizeChanged:i:" + i + " yoffset:" + itemView.getyOffset()); if (i >= 0 && i < stringList.size()) { itemView.setText(stringList.get(i)); } else { itemView.setText(""); } itemList.add(itemView); } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); LogUtil.i("chenxu", "---------------------------------"); for (int i = 0; i < itemList.size(); i++) { ItemView itemView = itemList.get(i); LogUtil.i("chenxu", "onDraw:i:" + i + " yoffset:" + itemView.getyOffset()); // if (isInView(itemView.getIndex())) { itemView.drawSelf(canvas); // } } LogUtil.i("chenxu", "---------------------------------"); }
public void scrollLeft(int velocityX) { int processedVelocityX = velocityX / 2; int index = (int) (getScrollX() + itemWidth / 2) / itemWidth; int currentScrollX = padNumber * itemWidth + itemWidth / 2 + getScrollX(); int leftScrollX = padNumber * itemWidth + itemWidth / 2 + Math.max(0, index) * itemWidth; int rightScrollX = padNumber * itemWidth + itemWidth / 2 + Math.min(imageViewList.size() - 1, index + 1) * itemWidth; int dx = 0; int leftAbs = Math.abs(currentScrollX - leftScrollX); int rightAbs = Math.abs(currentScrollX - rightScrollX); if (leftAbs >= rightAbs) { dx = -processedVelocityX / itemWidth * itemWidth - (rightScrollX - currentScrollX); } else { dx = -processedVelocityX / itemWidth * itemWidth + (currentScrollX - leftScrollX); } while (dx > (itemWidth * (imageIdList.size() - 1) - getScrollX())) { dx -= itemWidth; } int duration = (int) Math.abs(500.0f / itemWidth * dx); LogUtil.i("chenxu", "scrollLeft dx:" + dx + " duration:" + duration); scroller.startScroll(getScrollX(), 0, dx, 0, duration); invalidate(); }
@Override public void computeScroll() { super.computeScroll(); if (scroller.computeScrollOffset()) { LogUtil.i("chenxu", "scroller.getCurrentY():" + scroller.getCurrY()); scrollTo(0, scroller.getCurrY()); postInvalidate(); } else { // setCurrentIndex(Math.min(list.size() - 1, // Math.max(0, currentIndex + (int) (velocity / 20)))); } }
public boolean onSingleTapUp(MotionEvent e) { // LogUtil.i("chenxu", "onSingleTapUp e.getX():" + e.getX() // + " e.getRawX():" + e.getRawX()); int index = (int) (e.getX() + getScrollX() - padNumber * itemWidth) / itemWidth; LogUtil.i("chenxu", "calculated index:" + index); if (isIndexValid(index)) { if (listener != null) { listener.coverFlowDidClick(index); } return true; } else { return false; } };
public void moveByOffset(float offset, float accumulatedOffset) { for (int i = 0; i < itemList.size(); i++) { ItemView itemView = itemList.get(i); itemView.setyOffset((int) (itemView.getyOffset() + offset)); ; } selectedIndex = (int) Math.min( stringList.size(), Math.max( 0, (originalSelectedIndex - (accumulatedOffset + itemHeight / 2) / itemHeight))); LogUtil.i("chenxu", "moveByOffset-selectedIndex:" + selectedIndex); invalidate(); }
public void scrollBack() { int index = (int) (getScrollX() + itemWidth / 2) / itemWidth; int currentScrollX = padNumber * itemWidth + itemWidth / 2 + getScrollX(); int leftScrollX = padNumber * itemWidth + itemWidth / 2 + Math.max(0, index) * itemWidth; int rightScrollX = padNumber * itemWidth + itemWidth / 2 + Math.min(imageViewList.size() - 1, index + 1) * itemWidth; int dx = 0; int leftAbs = Math.abs(currentScrollX - leftScrollX); int rightAbs = Math.abs(currentScrollX - rightScrollX); if (leftAbs >= rightAbs) { dx = -(rightScrollX - currentScrollX); } else { dx = currentScrollX - leftScrollX; } int duration = (int) Math.abs(500.0f / itemWidth * dx); LogUtil.i("chenxu", "scrollBack dx:" + dx + " duration:" + duration); scroller.startScroll(getScrollX(), 0, dx, 0, duration); invalidate(); }