gpt4 book ai didi

android - 直到 onResume() 才触发 OnClickListener

转载 作者:行者123 更新时间:2023-11-29 16:23:00 25 4
gpt4 key购买 nike

我有一个 Activity ,其中有多个 View 具有动画效果。单击包含一些图像的 RelativeLayout 时会触发动画。它在 onCreate() 中设置如下:

mContainer.setOnClickListener(new ContainerClicked());

ContainerClicked() 类如下所示:

private class ContainerClicked implements OnClickListener {

@Override
public void onClick(View arg0) {
Log.i(TAG, "TEST!");
mSomeButton.setOnClickListener(null);
mSomeButton.setEnabled(false);

mAnotherButton.setOnClickListener(null);
mAnotherButton.setEnabled(false);

mYetAnotherButton.setOnClickListener(null);
mYetAnotherButton.setEnabled(false);

mContainer.setOnClickListener(null);
if (mLogo.getVisibility() == View.VISIBLE)
new AnimateToParty().execute();
else
new AnimateFromParty().execute();
}
}

这有效,并且按照我想要的方式对所有内容进行动画处理。

AnimateToParty() 看起来像这样:

private class AnimateToParty extends AsyncTask<Void, Integer, Void> {

@Override
protected void onProgressUpdate(final Integer... values) {
final View theView = findViewById(values[0]);
if (!(values[0] == mBackgroundImage.getId() || values[0] == mContainer
.getId())) {
final Animation animation = AnimationUtils.loadAnimation(
DashboardActivity.this, values[1]);
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation arg0) {
}

@Override
public void onAnimationRepeat(Animation arg0) {
}

@Override
public void onAnimationEnd(Animation arg0) {
theView.setVisibility(View.INVISIBLE);
}
});
theView.startAnimation(animation);
} else if (values[0] == mBackgroundImage.getId()) {
TransitionDrawable transition = (TransitionDrawable) theView
.getBackground();
transition.startTransition(getResources().getInteger(
android.R.integer.config_mediumAnimTime));
} else {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
-60, getResources().getDisplayMetrics()));
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation arg0) {
}

@Override
public void onAnimationRepeat(Animation arg0) {
}

@Override
public void onAnimationEnd(Animation arg0) {
LayoutParams lp = (LayoutParams) mContainer
.getLayoutParams();
lp.bottomMargin = 0;
RelativeLayout parent = (RelativeLayout) mContainer
.getParent();
mSomeButton.setVisibility(View.GONE);
mAnotherButton.setVisibility(View.GONE);
mYetAnotherButton.setVisibility(View.GONE);
mLogo.setVisibility(View.GONE);
// mContainer.setLayoutParams(lp);
// This caused a visible flicker, so I went with the solution below.
parent.removeView(mContainer);
parent.addView(mContainer, lp);
}
});
animation.setDuration(1000);
mContainer.startAnimation(animation);
}
}

@Override
protected Void doInBackground(Void... arg0) {
try {
publishProgress(mContainer.getId());
publishProgress(mLogo.getId(), R.anim.slide_out_up);
Thread.sleep(100);
publishProgress(mSomeButton.getId(), R.anim.slide_out_left);
Thread.sleep(110);
publishProgress(mAnotherButton.getId(), R.anim.slide_out_left);
Thread.sleep(120);
publishProgress(mYetAnotherButton.getId(), R.anim.slide_out_left);
Thread.sleep(130);
publishProgress(mBackgroundImage.getId());
Thread.sleep(550);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
mContainer.setOnClickListener(new LighterClicked());
}
}

上面的类将一些按钮和 Logo 设置为动画,然后显示容器。为了将它保留在那里,我更改了它的布局参数。只需更改它们就会产生可见的闪烁,其中容器向上跳跃 60 度并持续大约一帧(动画似乎还没有完全完成),然后才停留在我想要的位置。我在某处阅读以将其从其父项中删除并放回原处,但这不会产生相同的闪烁。但是,现在重新绑定(bind)onclicklistener不起作用!

我几乎尝试了所有方法,现在我注意到,如果我按下主页按钮并再次打开应用程序,则每次未“通过”的点击都会触发。

有没有更好的方法来避免“闪烁”?制作动画的更好方法? (我对动画 View 很陌生)为什么在我重新打开应用程序时会触发点击事件?

最佳答案

为了避免您看到的闪烁,您必须在 onAnimationEnd

开始时调用 clearAnimation()
@Override
public void onAnimationEnd(Animation arg0) {
theView.clearAnimation()
...
}

关于android - 直到 onResume() 才触发 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6377769/

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