2013-01-05 12 views
13

मैं एक सर्कल के चारों ओर विचारों की श्रृंखला को लेआउट करने का एक तरीका समझने की कोशिश कर रहा हूं, इस तरह से प्रत्येक दृश्य को सर्कल से बाहर का सामना करने के लिए घुमाया जाता है। नीचे दी गई तस्वीर मैं जो खोज रहा हूं उसका एक मोटा स्केच है। बाहरी ब्लॉक लेआउट/व्यू ग्रुप है, लाल वर्ग उन दृश्यों का प्रतिनिधित्व करते हैं जिन्हें मैं घूमना चाहता हूं।एंड्रॉइड लेआउट विचार घूर्णन और एक सर्कल के चारों ओर दूरी?

Rough Sketch of what I want to do

मैं PivotX, PivotY, और रोटेशन दृश्य गुण से परिचित हूँ और संदेह है मैं किसी तरह से इन का इस्तेमाल कर रही हो जाएगा, लेकिन मैं कैसे एक साथ मिलकर इन उपयोग करने के लिए यकीन नहीं है वांछित प्रभाव प्राप्त करने के लिए उपयुक्त लेआउट।

उत्तर

24

यहां एक उदाहरण है जो यह करता है। मैंने एक नया एंड्रॉइड प्रोजेक्ट बनाया और के साथ RelativeLayout को पहले से ही बदल दिया है। यह View में> = केवल अनुवाद और बारी बारी से कॉल की वजह से एपीआई 11 है:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/hello_world" /> 
</FrameLayout> 

मैं कोड में कुछ त्वरित दृश्य बना देंगे, सिर्फ उन्हें जो कुछ भी देखा गया आप के साथ बदलें। मैं उन्हें LayoutParams से Gravity.CENTER की गुरुत्वाकर्षण सेट करके लेआउट के केंद्र में रख रहा हूं। फिर, मैं अनुवाद कर रहा हूँ और उन्हें अपने सही पदों के लिए घूर्णन:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final FrameLayout main = (FrameLayout)findViewById(R.id.main); 

    int numViews = 8; 
    for(int i = 0; i < numViews; i++) 
    { 
     // Create some quick TextViews that can be placed. 
     TextView v = new TextView(this); 
     // Set a text and center it in each view. 
     v.setText("View " + i); 
     v.setGravity(Gravity.CENTER); 
     v.setBackgroundColor(0xffff0000); 
     // Force the views to a nice size (150x100 px) that fits my display. 
     // This should of course be done in a display size independent way. 
     FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(150, 100); 
     // Place all views in the center of the layout. We'll transform them 
     // away from there in the code below. 
     lp.gravity = Gravity.CENTER; 
     // Set layout params on view. 
     v.setLayoutParams(lp); 

     // Calculate the angle of the current view. Adjust by 90 degrees to 
     // get View 0 at the top. We need the angle in degrees and radians. 
     float angleDeg = i * 360.0f/numViews - 90.0f; 
     float angleRad = (float)(angleDeg * Math.PI/180.0f); 
     // Calculate the position of the view, offset from center (300 px from 
     // center). Again, this should be done in a display size independent way. 
     v.setTranslationX(300 * (float)Math.cos(angleRad)); 
     v.setTranslationY(300 * (float)Math.sin(angleRad)); 
     // Set the rotation of the view. 
     v.setRotation(angleDeg + 90.0f); 
     main.addView(v); 
    } 
} 

और यह परिणाम है:

Image of 8 views in a circle

+1

@daniel रहा डीपी में सभी हार्डकोडेड मूल्यों दे दिया है, लेकिन अभी भी चक्र उपकरणों पर एक ही नहीं है का उपयोग कर सकते विभिन्न आकार और संकल्प – Naruto

+0

@ डैनियल जोहानसन: टेक्स्टव्यू के बजाय, मैंने बटन का उपयोग किया। मुझे http://stackoverflow.com/questions/39489343/how-to-detect-a-button-in-framelayout-when-swipe-buttons – Jame

+0

पर आपके कोड के बारे में कोई समस्या है, यह बहुत उपयोगी था, धन्यवाद! – mmmyum

1

जवाब @Daniel से महान है।

आप अधिक कार्यक्षमता की जरूरत है आप GitHub पर यह अच्छा पुस्तकालय बुलाया Android-CircleMenu

enter image description here

संबंधित मुद्दे