【问题标题】:android 3d vertical carousel viewandroid 3d垂直旋转木马视图
【发布时间】:2013-05-26 02:53:36
【问题描述】:

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

【问题讨论】:

    标签: android carousel coverflow


    【解决方案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。

    【讨论】:

    • 感谢回复,上面的计算方法我试过了,但还是不行:i.stack.imgur.com/DDnkw.png
    • 这对我来说是一个非常令人惊讶的结果。也许我不了解该演示的坐标系。您是否尝试添加宽度偏移量?像这样:float y = (diameter/2.0f * Math.sin(angleOffset)) + diameter/2.0f + child.getHeight()/2.0f;
    • 是的,我也试过了。如果我这样做 y=y-250 ,那么我可以在中心位置看到列表项,但它不支持多屏幕分辨率和列表项。
    • 你找到解决办法了吗?
    【解决方案2】:

    您好,试试这段代码,并在您的 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));
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多