3
我正在嘗試動畫ImageButton。點擊該按鈕後,我希望它向右滑動。當它再次被點擊時,它應該滑回原始位置。Android幻燈片動畫閃爍
當動畫結束時,它會回到原來的位置,因此我重新定位ImageButton。我有一個小的「閃光燈」,在ImageButton重新定位之前,ImageButton和動畫都處於原始位置。任何人都可以告訴我如何擺脫這個「閃光」?
我寫了下面的代碼來開始動畫,當動畫結束時,移動ImageButton本身。 它是什麼,是一個用於滑動ImageButton的onClickListener和一個用於此的AnimationListener。還有一個onClickListener和一個用於將ImageButton滑回的AnimationListener。
private class SceneIndicatorListenerIn implements ImageButton.OnClickListener {
ImageButton imageButton;
public SceneIndicatorListenerIn (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onClick(View view) {
// Create animation
Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_in);
anim.setAnimationListener(new SceneIndicatorListenerInDidEnd(imageButton));
view.startAnimation(anim);
}
}
private class SceneIndicatorListenerInDidEnd implements AnimationListener {
ImageButton imageButton;
public SceneIndicatorListenerInDidEnd (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onAnimationEnd(Animation anim) {
Log.d(LOG_TAG, "In animation did end");
// This is for density pixels
float dp = context.getResources().getDisplayMetrics().density;
// Keep position after animation
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
params.setMargins((int) (0 * dp), imageButton.getTop(), 0, 0);
imageButton.setLayoutParams(params);
// Change on click listener
imageButton.setOnClickListener(new SceneIndicatorListenerOut(imageButton));
}
@Override
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationStart(Animation arg0) {}
}
private class SceneIndicatorListenerOut implements ImageButton.OnClickListener {
ImageButton imageButton;
public SceneIndicatorListenerOut (ImageButton imageButton) {
Log.d(LOG_TAG, "My imageButton was set");
this.imageButton = imageButton;
}
@Override
public void onClick(View view) {
Log.d(LOG_TAG, "You clicked me");
// Create animation
Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_out);
anim.setAnimationListener(new SceneIndicatorListenerOutDidEnd(imageButton));
view.startAnimation(anim);
}
}
private class SceneIndicatorListenerOutDidEnd implements AnimationListener {
ImageButton imageButton;
public SceneIndicatorListenerOutDidEnd (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onAnimationEnd(Animation anim) {
Log.d(LOG_TAG, "Out animation did end");
// This is for density pixels
float dp = context.getResources().getDisplayMetrics().density;
// Keep position after animation
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
params.setMargins((int) (-199 * dp), imageButton.getTop(), 0, 0);
imageButton.setLayoutParams(params);
// Change on click listener
imageButton.setOnClickListener(new SceneIndicatorListenerIn(imageButton));
}
@Override
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationStart(Animation arg0) {}
}
這是我在滑動動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0%"
android:toXDelta="83%"
android:duration="750" />
</set>
,這是我的滑回動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0%"
android:toXDelta="-80%"
android:duration="750" />
</set>