2009-11-06 12 views
5

मैं दो बटनों की स्थिति स्वैप करने की कोशिश कर रहा हूं। मेरे अदला-बदली कोड दिखाई देता है:दो बटनों की एनिमेटेड स्वैप स्थिति

private void exchangeButtons(Button btn1, Button btn2) { 
    // Create the animation set 
    AnimationSet exchangeAnimation = new AnimationSet(true); 
    TranslateAnimation translate = new TranslateAnimation(
     Animation.RELATIVE_TO_SELF, btn2.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn1.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn2.getRight(), 
     Animation.RELATIVE_TO_SELF, btn1.getRight()); 
    translate.setDuration(500); 
    exchangeAnimation.addAnimation(translate); 
    //int fromX = btn1.getLeft(); 
    //int fromY = btn1.getRight(); 
    //int toX = btn2.getLeft(); 
    //int toY = btn2.getRight(); 
    Log.d("ArrangeMe", 
     "view1 pos:" + btn1.getLeft() + ", 
     " +btn1.getRight() + "view2 pos:" + 
     btn2.getLeft() + ", " + btn2.getRight()); 
    AnimationSet exchangeAnimation1 = new AnimationSet(true); 
    TranslateAnimation translate1 = new TranslateAnimation( 
     Animation.RELATIVE_TO_SELF, btn1.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn2.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn1.getRight(), 
     Animation.RELATIVE_TO_SELF, btn2.getRight()); 
    translate1.setDuration(500); 
    exchangeAnimation1.addAnimation(translate1); 
    // EXECUTE btn1.startAnimation(exchangeAnimation); 
    btn2.startAnimation(exchangeAnimation1); 
} 

मैं के रूप में नीचे दिए गए कोड को फोन:

exchangeButtons(button1, button2); 

मेरे लेआउट के रूप में नीचे दिखता है:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:text="One" 
     android:id="@+id/button1" 
     android:layout_height="70px" 
     android:layout_width="70px" 
     android:layout_weight="1"> 
    </Button> 
    <Button 
     android:text="Two" 
     android:id="@+id/button2" 
     android:layout_height="70px" 
     android:layout_width="70px" 
     android:layout_weight="1"> 
    </Button> 
</LinearLayout> 

जब मैं निष्पादित कोड है क्या होता है:

अपने पदों का आदान-प्रदान करने वाले बटनों के बजाय, वे कुछ समय के लिए गायब हो जाते हैं [ 500 एमएस हो सकता है] और मूल रूप से वे फिर से दिखाई देते हैं।

इस समस्या को हल करने के लिए कैसे करें? क्या यह डिवाइस में ठीक तरह से काम करेगा?

+0

में कोड रखो प्रयास करें? –

उत्तर

1

Android के TranslateAnimation अपने घटकों का स्थान नहीं है, यह सिर्फ संक्रमण आप क्या करना चाहते एनिमेट - उसके बाद, यह अपने घटक के लेआउट पैरामीटर (इस post पर एक नज़र डालें) बदलने के लिए आप पर निर्भर है।

पीएस आप एक संपूर्ण नए एनीमेशन सेट का उपयोग किये बिना सीधे अनुवाद एनीमेशन का उपयोग कर सकते हैं। एक और अच्छी बात यह है कि जब आपका लेआउट फुलाया जाता है और नए आयामों का उपयोग करके उन्हें फिर से बनाने के लिए onSizeChanged(int, int, int, int) को ओवरराइड करने के लिए एनिमेशन बनाना होगा।

आप किस लेआउट का उपयोग कर रहे हैं?

3

आपको ऑनरेट में getRight मान प्राप्त नहीं होगा क्योंकि यूआई स्क्रीन पर नहीं खींचा गया है, इसलिए यूआई तत्वों में से कोई भी उपाय नहीं है। इस

ViewTreeObserver vto = btn1.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     btn1.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int left = btn1.getLeft(); 
    } 
}); 

आप इस समस्याओं का समाधान हो गया onCreate विधि

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