Android动画闪烁
我有一个使用TranslateAnimation设置动画的ImageView。在动画结束时,我想切换它显示的可绘制对象。它可以工作,但在显示图像之前会闪烁。我最初使用的是AnimationListener,然后我按照我找到的示例创建了一个自定义视图,该视图扩展了ImageView并覆盖了OnAnimationEnd,但没有任何效果 在我的活动中:Android动画闪烁,android,Android,我有一个使用TranslateAnimation设置动画的ImageView。在动画结束时,我想切换它显示的可绘制对象。它可以工作,但在显示图像之前会闪烁。我最初使用的是AnimationListener,然后我按照我找到的示例创建了一个自定义视图,该视图扩展了ImageView并覆盖了OnAnimationEnd,但没有任何效果 在我的活动中: TranslateAnimation = new TranslateAnimation(0, 150, 0, 150); translateAnima
TranslateAnimation = new TranslateAnimation(0, 150, 0, 150);
translateAnimation.setDuration(ANIMATION_DURATION);
btn.setNextImage(buttons.get(2));
btn.startAnimation(3000);
在我的自定义视图中,我有以下内容:
protected void onAnimationEnd() {
super.onAnimationEnd();
//this.clearAnimation();
if(_nextImage != null)
this.setImageDrawable(_nextImage);
}
private Drawable _nextImage;
public void setNextImage(Drawable d)
{
_nextImage = d;
}
将其作为OnAnimationEnd中的第一条语句调用 应用场景动画的视图。clearAnimation()
这应该解决它,因为它解决了我的问题。我确实面临着与OnAnimationEnd中的第一句话相同的问题 应用场景动画的视图。clearAnimation()
这应该解决它,因为它解决了我的问题。我也遇到过同样的问题,我有一个想法,就是使用FrameLayout在屏幕应用程序上显示3D翻转动画。有两种不同的布局(magicnumber.xml和selectteam.xml),您要在FrameLayout中翻转它们的位置 有以下Xml代码 Container.xml
<FrameLayout >
<xmlns:android="http://schemas.android.com/apk/res/android" >
<android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container"android:background="#000000" />
<include android:id="@+id/tshirtlist" layout="@layout/magicnumber" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<include > android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/selectteam" android:id="@+id/Searchlist" ></include> </FrameLayout>
2.DisplayNextView.java类
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Flip3dAnimation extends Animation
{
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public Flip3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY)
{
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
public final class DisplayNextView implements Animation.AnimationListener
{
private boolean mCurrentView;
RelativeLayout image1;
RelativeLayout image2;
public DisplayNextView(boolean currentView, RelativeLayout rl_front, RelativeLayout rl_back)
{
mCurrentView = currentView;
this.image1 = rl_front;
this.image2 = rl_back;
}
public void onAnimationStart(Animation animation)
{
}
public void onAnimationEnd(Animation animation)
{
image1.post(new SwapViews(mCurrentView, image1, image2));
}
public void onAnimationRepeat(Animation animation)
{
}
}
public final class SwapViews implements Runnable
{
private boolean mIsFirstView;
RelativeLayout image1;
RelativeLayout image2;
public SwapViews(boolean isFirstView, RelativeLayout image12, RelativeLayout image22)
{
mIsFirstView = isFirstView;
this.image1 = image12;
this.image2 = image22;
}
public void run()
{
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;
Flip3dAnimation rotation;
if (mIsFirstView)
{
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
image2.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
else
{
image2.setVisibility(View.GONE);
image1.setVisibility(View.VISIBLE);
image1.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
if (mIsFirstView)
{
image2.startAnimation(rotation);
}
else
{
image1.startAnimation(rotation);
}
}
}
now use following method to here u want to use Flip
protected void applyRotation(float start, float end) {
final float centerX = Rl_Main.getWidth() / 2.0f;
final float centerY = Rl_Select.getHeight() / 2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(100);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, Rl_Main, Rl_Select ));
if (isFirstImage)
Rl_Main.startAnimation(rotation);
else
Rl_Select.startAnimation(rotation);
}
3.SwapViews.java类
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Flip3dAnimation extends Animation
{
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public Flip3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY)
{
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
public final class DisplayNextView implements Animation.AnimationListener
{
private boolean mCurrentView;
RelativeLayout image1;
RelativeLayout image2;
public DisplayNextView(boolean currentView, RelativeLayout rl_front, RelativeLayout rl_back)
{
mCurrentView = currentView;
this.image1 = rl_front;
this.image2 = rl_back;
}
public void onAnimationStart(Animation animation)
{
}
public void onAnimationEnd(Animation animation)
{
image1.post(new SwapViews(mCurrentView, image1, image2));
}
public void onAnimationRepeat(Animation animation)
{
}
}
public final class SwapViews implements Runnable
{
private boolean mIsFirstView;
RelativeLayout image1;
RelativeLayout image2;
public SwapViews(boolean isFirstView, RelativeLayout image12, RelativeLayout image22)
{
mIsFirstView = isFirstView;
this.image1 = image12;
this.image2 = image22;
}
public void run()
{
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;
Flip3dAnimation rotation;
if (mIsFirstView)
{
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
image2.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
else
{
image2.setVisibility(View.GONE);
image1.setVisibility(View.VISIBLE);
image1.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
if (mIsFirstView)
{
image2.startAnimation(rotation);
}
else
{
image1.startAnimation(rotation);
}
}
}
now use following method to here u want to use Flip
protected void applyRotation(float start, float end) {
final float centerX = Rl_Main.getWidth() / 2.0f;
final float centerY = Rl_Select.getHeight() / 2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(100);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, Rl_Main, Rl_Select ));
if (isFirstImage)
Rl_Main.startAnimation(rotation);
else
Rl_Select.startAnimation(rotation);
}
此方法在要显示翻转效果的位置使用调用
applyRotation(0, 90);
isFirstImage = !isFirstImage;
在哪里
我有一个想法,使用FrameLayout在屏幕上应用3D翻转动画。有两种不同的布局(magicnumber.xml和selectteam.xml),您要在FrameLayout中翻转它们的位置 有以下Xml代码 Container.xml
<FrameLayout >
<xmlns:android="http://schemas.android.com/apk/res/android" >
<android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container"android:background="#000000" />
<include android:id="@+id/tshirtlist" layout="@layout/magicnumber" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<include > android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/selectteam" android:id="@+id/Searchlist" ></include> </FrameLayout>
2.DisplayNextView.java类
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Flip3dAnimation extends Animation
{
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public Flip3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY)
{
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
public final class DisplayNextView implements Animation.AnimationListener
{
private boolean mCurrentView;
RelativeLayout image1;
RelativeLayout image2;
public DisplayNextView(boolean currentView, RelativeLayout rl_front, RelativeLayout rl_back)
{
mCurrentView = currentView;
this.image1 = rl_front;
this.image2 = rl_back;
}
public void onAnimationStart(Animation animation)
{
}
public void onAnimationEnd(Animation animation)
{
image1.post(new SwapViews(mCurrentView, image1, image2));
}
public void onAnimationRepeat(Animation animation)
{
}
}
public final class SwapViews implements Runnable
{
private boolean mIsFirstView;
RelativeLayout image1;
RelativeLayout image2;
public SwapViews(boolean isFirstView, RelativeLayout image12, RelativeLayout image22)
{
mIsFirstView = isFirstView;
this.image1 = image12;
this.image2 = image22;
}
public void run()
{
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;
Flip3dAnimation rotation;
if (mIsFirstView)
{
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
image2.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
else
{
image2.setVisibility(View.GONE);
image1.setVisibility(View.VISIBLE);
image1.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
if (mIsFirstView)
{
image2.startAnimation(rotation);
}
else
{
image1.startAnimation(rotation);
}
}
}
now use following method to here u want to use Flip
protected void applyRotation(float start, float end) {
final float centerX = Rl_Main.getWidth() / 2.0f;
final float centerY = Rl_Select.getHeight() / 2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(100);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, Rl_Main, Rl_Select ));
if (isFirstImage)
Rl_Main.startAnimation(rotation);
else
Rl_Select.startAnimation(rotation);
}
3.SwapViews.java类
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Flip3dAnimation extends Animation
{
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public Flip3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY)
{
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
public final class DisplayNextView implements Animation.AnimationListener
{
private boolean mCurrentView;
RelativeLayout image1;
RelativeLayout image2;
public DisplayNextView(boolean currentView, RelativeLayout rl_front, RelativeLayout rl_back)
{
mCurrentView = currentView;
this.image1 = rl_front;
this.image2 = rl_back;
}
public void onAnimationStart(Animation animation)
{
}
public void onAnimationEnd(Animation animation)
{
image1.post(new SwapViews(mCurrentView, image1, image2));
}
public void onAnimationRepeat(Animation animation)
{
}
}
public final class SwapViews implements Runnable
{
private boolean mIsFirstView;
RelativeLayout image1;
RelativeLayout image2;
public SwapViews(boolean isFirstView, RelativeLayout image12, RelativeLayout image22)
{
mIsFirstView = isFirstView;
this.image1 = image12;
this.image2 = image22;
}
public void run()
{
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;
Flip3dAnimation rotation;
if (mIsFirstView)
{
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
image2.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
else
{
image2.setVisibility(View.GONE);
image1.setVisibility(View.VISIBLE);
image1.requestFocus();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
if (mIsFirstView)
{
image2.startAnimation(rotation);
}
else
{
image1.startAnimation(rotation);
}
}
}
now use following method to here u want to use Flip
protected void applyRotation(float start, float end) {
final float centerX = Rl_Main.getWidth() / 2.0f;
final float centerY = Rl_Select.getHeight() / 2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(100);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, Rl_Main, Rl_Select ));
if (isFirstImage)
Rl_Main.startAnimation(rotation);
else
Rl_Select.startAnimation(rotation);
}
此方法在要显示翻转效果的位置使用调用
applyRotation(0, 90);
isFirstImage = !isFirstImage;
在哪里
您尝试以下代码为什么在onAnimationEnd回调中注释clearAnimation()调用?您尝试以下代码为什么在onAnimationEnd回调中注释clearAnimation()调用?尝试避免super.onAnimationEnd()同时调用doc说总是调用SuperShill试试看结果。@Javanator:我假设你假设他使用了一个监听器,在这种情况下,super是不必要的,因为它不存在(但它也不做任何事情,所以调用它没有害处)试着避免super.onAnimationEnd()也调用医生说总是调用SuperShill试试看结果。@Javanator:我假设你假设他使用了一个侦听器,在这种情况下,super是不必要的,因为它不存在(但它也不起任何作用,所以调用它没有害处)