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

user853341
  • 149
  • 2
  • 12

2 Answers2

1

I think that this calculation:

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

should be this:

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;

Your x position should always be zero, and your y position should be based on the sin, and should be offset by 1/2 of the height of the child instead of 1/2 of the width.

HalR
  • 11,411
  • 5
  • 48
  • 80
  • thanks for the reply, I tried the above calculation but it is not working: http://i.stack.imgur.com/DDnkw.png – user853341 May 26 '13 at 05:55
  • That is a very surprising result to me. Perhaps I'm not understanding the coordinate system for that demo. Did you try adding the width offset instead? Like this: float y = (diameter/2.0f * Math.sin(angleOffset)) + diameter/2.0f + child.getHeight()/2.0f; – HalR May 26 '13 at 13:25
  • yes, I tried the same.if i do y=y-250 ,then i can see the list item in center position but it won't support multiple screen resolution and no of list items. – user853341 May 27 '13 at 01:49
  • Have u find any solution for this.? – Mstack Sep 22 '16 at 18:19
0

Hello try this code and replace with this code in your Calculate3DPosition method

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

its solve my problem please try this one

Saif
  • 723
  • 6
  • 21