2010-08-12 137 views
1

我想要在觸摸屏幕時爲佈局設置動畫。 如果您觸摸屏幕,包含其上的按鈕的佈局應向下翻出屏幕,並在觸摸屏幕時再次將佈局呈現在屏幕中原始位置。Android:翻譯動畫第一次工作,而不是第二次

我遇到了問題,當我通過編程的時候。 它通過XML來完成工作。

在編程方面,它第一次工作,第二次不起作用。 即使我觀察到,如果我們點擊屏幕上它讀取所有開始動畫的代碼,但它不會得到動畫,但之後,如果我們點擊在佈局中添加的按鈕,動畫的作品。

下面是代碼:

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

我有更多的時間一個非常類似的問題掙扎,比我想承認。我有兩種觀點,我正在移動並一起淡化。來回取決於狀態。一個方向正在工作,但沒有另一個方向。其中一個通過鍵盤操作激活,另一個通過按鍵按下。

因爲一個是文本輸入,我用View.GONE去設置可見性。我認爲這是問題所在。我相信直到動畫完成之後纔會將視圖設置爲View.VISIBLE(無論如何,我仍然執行View.GONE)。我無法驗證這一點,但是當我刪除所有的setVisibility()調用並做了純粹的alpha更改時,事情就開始爲我工作。

我還應該注意,我沒有使用任何類似setFillAfter()的東西,但我自己計算了位置,因爲只有視圖上的繪製部分隨該移動,而不是可操作(可點擊)區域。

+0

我也從我的代碼中刪除了View.GONE。儘管如此,在使其變得不可見之後,我正在語法上改變佈局的高度。 而且它的工作正常。感謝提示:) – Aakanksha 2016-08-26 14:03:57

相關問題