机器人3d垂直旋转木马视图

时间:2013-05-26 02:53:36

标签: android carousel coverflow

我正在使用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);

attached o/p of 4 list items enter image description here

o/p of 12 list items , diameter movew upward

2 个答案:

答案 0 :(得分:1)

我认为这个计算:

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位置应该基于sin,并且应该偏离孩子身高的1/2而不是宽度的1/2。

答案 1 :(得分:0)

您好试试此代码并在Calculate3DPosition方法中替换此代码

 angleOffset = angleOffset * (float) (Math.PI / 180.0f);
    float y = (float) (((diameter * 60) / 100) * Math.sin(angleOffset)) + ((diameter * 50) / 100);
    float z = diameter / 2 * (1.0f - (float) Math.cos(angleOffset));
    float x = (float) (((diameter * 5) / 100) * Math.cos(angleOffset) * 0.3);
    child.setItemX(x);
    child.setItemZ((z * 30) / 100);
    child.setItemY(-(y));

解决我的问题请试试这个