我正在使用http://www.codeproject.com/Articles/146145/Android-3D-Carousel代码创建垂直轮播视图。我可以使用以下代码更改来查看垂直轮播,但中心项未正确放置在屏幕中,如果列表项的大小增加,则直径会向上移动。

private void setUpChild(CarouselImageView child, int index, float angleOffset) {
  // Ignore any layout parameters for child, use wrap content
  addViewInLayout(child, -1 /*index*/, generateDefaultLayoutParams());

  child.setSelected(index == mSelectedPosition);

  int h;
  int w;

  if (mInLayout)
  {
    h = (getMeasuredHeight() - getPaddingBottom()-getPaddingTop())/3;
    w = getMeasuredWidth() - getPaddingLeft() - getPaddingRight()/3;
  }
  else
  {
    h = (getMeasuredHeight() - getPaddingBottom()-getPaddingTop())/3;
    w = getMeasuredWidth() - getPaddingLeft() - getPaddingRight()/3;
  }

  child.setCurrentAngle(angleOffset);
  // modify the diameter.
  Calculate3DPosition(child, w*(getAdapter().getCount()/4), angleOffset);

  // Measure child
  child.measure(w, h);

  int childLeft;

  // Position vertically based on gravity setting
  int childTop = calculateTop(child, true);

  childLeft = 0;

  child.layout(childLeft, childTop, w, h);
}


calculate3position函数中的更改如下

float x = (float) (-diameter/2 * Math.cos(angleOffset) * 0.00001);
float z = diameter/2 * (1.0f - (float)Math.cos(angleOffset));
float y = (float) (diameter/2 * Math.sin(angleOffset)) + diameter/2 - child.getWidth();
child.setX(x);
child.setZ(z);
child.setY(y);

最佳答案

我认为这样计算:

float x = (float) (-diameter/2 * Math.cos(angleOffset) * 0.00001);
float z = diameter/2 * (1.0f - (float)Math.cos(angleOffset));
float y = (float) (diameter/2 * Math.sin(angleOffset)) + diameter/2 - child.getWidth();


应该是这样的:

float x = 0.0f
float z = diameter/2.0f * (1.0f - (float)Math.cos(angleOffset));
float y = (diameter/2.0f * Math.sin(angleOffset)) + diameter/2.0f - child.getHeight()/2.0f;


您的x位置应始终为零,您的y位置应基于正弦,并且应偏移孩子高度的1/2而不是宽度的1/2。

09-12 13:03