2012-10-16 14 views
5

(एंड्रॉयड 2.2 और इसके बाद के संस्करण पर) अगले एनीमेशन बनाने के लिए की आवश्यकता है नीचे से ऊपर (उसे फिर से क्लिक करने के बाद)।नीचे करने के लिए शीर्ष का अनुवाद एनीमेशन

पहला एनीमेशन ठीक काम करता है, लेकिन दूसरा नहीं, बीटीएन नीचे "ऊपर" से ऊपर और ऊपर नहीं जाता है।

कोड:

public class MainActivity extends Activity { 

static RelativeLayout relativeLayout; 
static Button btn; 
static Boolean isUp = true; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn = (Button) findViewById(R.id.button1); 
    relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); 

    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      if(isUp){ 
       isUp = false; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0)); 
      }else{ 
       isUp = true; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0)); 
      } 
     } 
    }); 
} 


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset) 
{ 
    TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition); 
    translateAnimation.setDuration(duration); 
    translateAnimation.setInterpolator(new AccelerateInterpolator()); 
    translateAnimation.setStartOffset(startOffset); 

    //Stop animation after finishing. 
    //translateAnimation.setFillAfter(true); 

    translateAnimation.setAnimationListener(new AnimationListener() 
    { 
    public void onAnimationStart(Animation animation) { } 
    public void onAnimationRepeat(Animation animation) { } 
    public void onAnimationEnd(Animation animation) { 
     btn.setY(toYPosition);   
    } 
    }); 

    return translateAnimation; 
    } 
} 

लेआउट:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/relative_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 

</RelativeLayout> 

उत्तर

10

ठीक है, मैं इसे हल किया।

कुछ issuses आप एनीमेशन के बारे में पता होना चाहिए रहे हैं:

  1. एनीमेशन paremeters सरल "(स्थिर स्थिति) से" नहीं हैं -> "(ठीक स्थिति) के लिए" के रूप में आपको लगता है चाहिए। "से (वर्तमान स्थिति/0)" -> "कितने कदम हैं और किस दिशा में (नकारात्मक के लिए सकारात्मक/शून्य के लिए बहाना)"

  2. एनीमेशन वास्तविक स्थिति को नहीं बदलता स्क्रीन पर देखने की, इसलिए यदि आप अंत की स्थिति में एनीमेशन बंद करना चाहते हैं, तो आप का उपयोग करना चाहिए:

    animation.setFillAfter(true); 
    
  3. आप आप दृश्य मानकों को अपडेट करना चाहिए देखने के असली स्थिति बदलने के लिए चाहते हैं, तो "ऑनएनीमेशन एंड" (नीचे कोड की तरह) पर, या स्थिति की गणना करें और वाई/एक्स स्थिति मैन्युअल रूप से सेट करें (फिर से "ऑनएनीमेशन एंड" पर), जैसे:

    animatedView.setY(stopPosition); 
    

कोड:

public class AnimationActivity extends Activity { 

    private boolean isUp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 

}

+0

यह वास्तव में मददगार .मैं है पता है कि कैसे लगातार और अपनी स्थिति को हर बार हो रही ऊपर से नीचे तक एक दृश्य स्थानांतरित करने के लिए चाहता हूँ कि यह प्रत्येक चरण से अगले चरण तक चलता है। एनीमेशन का उपयोग करके इसे कैसे कार्यान्वित करें? क्या उनकी कोई अन्य विधि उपलब्ध है? मेरा अंतिम उद्देश्य एक चलती दृश्य और इस दृश्य के बीच टकराव का पता लगाने के लिए है? सहायता – Ajay

-1
public class AnimationActivity extends Activity { 

    private boolean isUp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 
+0

के लिए धन्यवाद आप बटन पर –

+0

पर कास्ट किए बिना ऑनक्लिक लिस्टनर सेट कर सकते हैं आपने अभी से ऊपर से कोड कॉपी किया है और इसे चिपकाया है .... – Denny

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