onclicklistener onresume()まで発射しない
-
28-10-2019 - |
質問
いくつかのビューがアニメーション化するアクティビティがあります。アニメーションは、いくつかの画像を含むRelativeLayoutがクリックされたときにトリガーされます。このようにoncreate()でセットアップされています。
mContainer.setOnClickListener(new ContainerClicked());
そして、conterclicked()クラスは次のようになります。
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());
}
}
上のクラスは、いくつかのボタンとロゴを邪魔にならないアニメーション化し、コンテナを明らかにします。それをそこに保つために、私はそれがレイアウトパラムであることを変更します。それらを変更するだけで、目に見えるフリッカーが生成されます。そこでは、コンテナが60をジャンプして約1つのフレーム(アニメーションが完全に終了していないようです)を使用してから、希望する場所に落ち着きます。私はどこかを読んで、その親からそれを取り除き、元に戻しましたが、これは同じちらつきを生成しません。ただし、OnClickListenerの再包装は機能しません!
私はほぼすべてを試してみましたが、ホームボタンを押してアプリケーションを再度開くと、クリックが「通過」しなかったすべてのクリックが「通過」しなかったことに気づきました。
「ちらつき」を避けるためのより良い方法はありますか?アニメーションを行うためのより良い方法は? (私はビューをアニメーション化するのは非常に新しいです)アプリケーションを再開するときにクリックイベントが発砲するのはなぜですか?
解決
あなたが見ているちらつきを避けるために、あなたは電話しなければなりません clearAnimation() の最初に OnanimationEnd
@Override
public void onAnimationEnd(Animation arg0) {
theView.clearAnimation()
...
}