user853341
user853341

Reputation: 149

android 3d vertical carousel view

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

Upvotes: 2

Views: 3639

Answers (2)

Saif
Saif

Reputation: 713

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

Upvotes: 0

HalR
HalR

Reputation: 11073

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.

Upvotes: 1

Related Questions