2013-05-26 68 views
2

我使用http://www.codeproject.com/Articles/146145/Android-3D-Carousel創建垂直轉盤視圖的代碼.i可以使用代碼中的下列更改來查看垂直轉盤,但中心項目未正確放置在屏幕中,並且如果列表項目大小增加,直徑向上移動。android 3D垂直轉盤視圖

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

o/p of 12 list items , diameter movew upward

回答

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位置應根據的罪,應該由孩子的高度的1/2,而不是1所抵消/ 2的寬度。

+0

感謝您的答覆,我嘗試了上面的計算,但它無法正常工作:http://i.stack.imgur.com/DDnkw.png – user853341

+0

這對我來說是一個非常令人驚訝的結果。也許我不瞭解該演示的座標系。您是否嘗試添加寬度偏移量?像這樣:float y =(diameter/2.0f * Math.sin(angleOffset))+ diameter/2.0f + child.getHeight()/ 2.0f; – HalR

+0

是的,我試過相同的。如果我做y = y-250,那麼我可以看到中心位置的列表項,但它不會支持多個屏幕分辨率和列表項。 – user853341

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

它解決我的問題,請試試這個