gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:50:50 24 4
gpt4 key购买 nike

我想在触摸屏幕时为布局设置动画。如果您触摸屏幕,其上包含按钮的布局应向下移出屏幕,再次触摸屏幕时,布局应在其原始位置出现在屏幕中。

当我通过编程解决问题时,我遇到了问题。当我通过 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
}

最佳答案

我在一个非常相似的问题上挣扎了比我愿意承认的更多的时间。我有两个观点,我正在一起移动和交叉淡入淡出。来回取决于状态。一个方向行得通,但另一个方向行不通。一个是通过键盘操作激活的,另一个是通过按下按钮激活的。

因为其中一个是文本输入,所以我使用 View.GONE 来设置可见性。我认为这就是问题所在。我相信直到动画完成后 View 才被设置为 View.VISIBLE(此时我还是执行了 View.GONE)。我无法验证这一点,但是当我删除所有 setVisibility() 调用并进行纯粹的 alpha 更改时,一切开始为我工作。

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

关于Android:翻译动画是第一次而不是第二次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3466271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com