2

I am using http://www.codeproject.com/Articles/146145/Android-3D-Carousel code to create a vertical carousel view.i can see the vertical carousel using below changes in the code but center item is not properly placed in the screen and if the list items size increased, diameter moves upwards.

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);
}

change in calculate3position function as below

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 itemsenter image description here

o/p of 12 list items , diameter movew upward

4

2 回答 2

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。

于 2013-05-26T05:07:39.460 回答
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));

它解决了我的问题,请试试这个

于 2016-11-04T05:06:46.243 回答