Android:翻译动画第一次工作,而不是第二次

时间:2010-08-12 09:25:00

标签: android

我想在触摸屏幕时为布局设置动画。 如果您触摸屏幕,其上的布局包含按钮应从屏幕向下平移,再次触摸屏幕时,布局应在屏幕原点位置显示。

当我通过编程完成时,我遇到了问题。 当我通过XML完成它时,它就可以工作。

在编程approch中,它第一次起作用,第二次起作用不起作用。 即使我已经观察到,如果我们点击屏幕它会读取所有启动动画的代码,但它不会被动画,但之后如果我们点击布局中添加的按钮,动画就会起作用。

以下是代码:

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);
    navigationBarInnerFrameLayout.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
    }

1 个答案:

答案 0 :(得分:1)

我在一个非常类似的问题上挣扎了很长时间而不是我想承认的。我有两个观点,我正在移动并交叉渐渐消失。来回取决于国家。一个方向是工作但不是另一个方向。一个是通过键盘操作激活的,另一个是通过按下按钮激活的。

因为一个是文本输入,我使用View.GONE来设置可见性。我认为这就是问题所在。我认为直到动画完成之后视图才被设置为View.VISIBLE(此时我还是使用了View.GONE)。我无法验证这一点,但当我删除所有setVisibility()次调用并进行纯alpha更改时,事情开始对我有用。

我还应该注意到我没有使用setFillAfter()之类的东西,而是自己计算位置,因为只有视图上的绘制部分才会移动,而不是可操作的(可点击的)区域。