开发者

Android: translate Animation works for the first time and not second time

I want to animate a layout on touching the screen. If you touch the screen the layout containg buttons on it should translate down out of the screen and again on touching the screen the layout should come up into the screen at its original position.

I am getting the problem,when I do it through programming. and It works when I do it through XML.

In programming approch it works for the first time and on second time it does not works. Even I have observed that If we click the on screen It reads all the code of starting the animation but it does not get animated but after that if we click on the button added in layout, the animation works.

Here is the code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.Animation.AnimationListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class BrowserDemo2 extends Activity implements AnimationListener, OnTouchListener, OnClickListener
{
    WebView browser;
    RelativeLayout parentLayout;
    LinearLayout navigationBarOuterLinearLayout;
    FrameLayout navigationBarInnerFrameLayout;

    Button galleryButton;
    Button creditsButton;
    Button viewChangeButton;

    byte animationType;
    public static final byte ANIM_IN = 0;
    public static final byte ANIM_OUT = 1;
    boolean isNavigationBarVisible = true;
    boolean isAnimationInProgress;


    @Override
    public void onCreate(Bundle icicle)
    {
    super.onCreate(icicle);     
    setFullscreen();

    setContentView(R.layout.main);  
    parentLayout = (RelativeLayout) findViewById(R.id.parent);

    setNavigationBarLayout();
    parentLayout.addView(navigationBarOuterLinearLayout);

    animationType = ANIM_OUT;
    animController = new LayoutAnimationController(this, null);

    }   

    public void setNavigationBarLayout()
    {
    navigationBarOuterLinearLayout = new LinearLayout(this);
    LayoutParams navigationBarOuterLinearLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
    navigationBarOuterLinearLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    navigationBarOuterLinearLayout.setLayoutParams(navigationBarOuterLinearLayoutParams);   
    navigationBarOuterLinearLayout.setOrientation(LinearLayout.VERTICAL);   

    navigationBarInnerFrameLayout = new FrameLayout(this);
    LayoutParams navigationBarInnerFrameLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);   
    navigationBarInnerFrameLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    navigationBarInnerFrameLayout.setLayoutParams(navigationBarInnerFrameLayoutParams);
    navigationBarInnerFrameLayout.setBackgroundResource(R.drawable.uitabbar);
    navigationBarInnerFrameLayout.setPadding(0, 5, 0, 0);

    galleryButton = new Button(this);
    galleryButton.setOnClickListener(this);
    galleryButton.setText("Gallery");
    FrameLayout.LayoutParams galleryButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.LEFT);
    galleryButton.setLayoutParams(galleryButtonParams);

    creditsButton = new Button(this);
    creditsButton.setOnClickListener(this);
    creditsButton.setText("Credits");
    FrameLayout.LayoutParams creditsButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    creditsButton.setLayoutParams(creditsButtonParams);

    viewChangeButton = new Button(this);
    viewChangeButton.setOnClickListener(this);
    viewChangeButton.setText("Chnage View");
    FrameLayout.LayoutParams viewChangeButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT);
    viewChangeButton.setLayoutParams(viewChangeButtonParams);

    navigationBarInnerFrameLayout.addView(galleryButton);
    navigationBarInnerFrameLayout.addView(creditsButton);
    naviga开发者_StackOverflowtionBarInnerFrameLayout.addView(viewChangeButton);

    navigationBarOuterLinearLayout.addView(navigationBarInnerFrameLayout);
    }

    public void setFullscreen()
    {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }    

    public void setAnimations()
    {
    switch(animationType)
    {
        case ANIM_IN:
        {
        isAnimationInProgress = true;
        navigationBarOuterLinearLayout.setVisibility(View.VISIBLE);     
        animateLayout(R.anim.lower_navigation_bar_in, navigationBarOuterLinearLayout);
//      parentLayout.requestFocus();
//      parentLayout.requestFocusFromTouch();
//      parentLayout.requestLayout();
        break;
        }
        case ANIM_OUT:
        {   
        isAnimationInProgress = true;
        animateLayout(R.anim.lower_navigation_bar_out, navigationBarOuterLinearLayout);
        break;
        }
    }   
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
        Log.v("onTouch", "in touch realease..");
        setAnimations();
    }   
    return true;    
    }



    Animation layoutAnimation;
    LayoutAnimationController animController;
    public void animateLayout(int type, LinearLayout layout)
    {
    layoutAnimation = AnimationUtils.loadAnimation(this, type);
    layoutAnimation.setAnimationListener(this);
    animController.setAnimation(layoutAnimation);
    layout.setLayoutAnimation(animController);
    layout.startLayoutAnimation();
    }

    @Override
    public void onAnimationEnd(Animation animation)
    {
    if(animationType == ANIM_OUT && isNavigationBarVisible)
    {
        animationType = ANIM_IN;
        isNavigationBarVisible = false;
        navigationBarOuterLinearLayout.setVisibility(View.INVISIBLE);
    }
    else
    {
        animationType = ANIM_OUT;
        isNavigationBarVisible = true;      
    }
    isAnimationInProgress = false;
    }

    @Override
    public void onAnimationRepeat(Animation animation)
    {
    // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation)
    {
    // TODO Auto-generated method stub

    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
    return false;
    }

    @Override
    public void onClick(View v)
    {
    // TODO Auto-generated method stub
    }


I struggled with a very similar problem for more hours than I would like to admit. I had two views that I was moving and cross fading together. Back and forth depending on the state. One direction was working but not the other. One was activated by a keyboard action and the other by a button press.

Because one was a text-input, I went with using View.GONE to set visibility. I think this was where the problem was. I believe that the view was not getting set to View.VISIBLE until after the animation had completed (at which point I did View.GONE anyway). I cannot verify this, but when I removed all the setVisibility() calls and did pure alpha changes, things started to work for me.

I should also note that I did not use anything like setFillAfter(), but calculated the positions myself because only the drawn portion on the view was moving with that, not the actionable (clickable) area.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜