问题
I am using eclipes the create an android card game.For some reason my application crashes when i call the animation. One change i had to make was in the Flip Class and that was changing the layoutview to imageview because in the animation method "createDisplayNextView" had an error.
////////ANIMATION
private ImageView FrontView;
private ImageView BackView;
private boolean isFirstImage = true;
private void applyRotation(float start, float end) {
// Find the center of image
FrontView = (ImageView) findViewById(R.id.imgC1);
BackView = (ImageView) findViewById(R.drawable.clubs2);
BackView.setVisibility(View.GONE);
final float centerX = FrontView.getWidth() / 2.0f;
final float centerY = FrontView.getHeight() / 2.0f;
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final cardFlip rotation = new cardFlip(start, end, centerX, centerY);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(rotation.createDisplayNextView(isFirstImage, FrontView, BackView));
if (isFirstImage) {
FrontView.startAnimation(rotation);
} else {
BackView.startAnimation(rotation);
}
}
///////////////
This is the Flip class:
////////////////FLIP CLASS//////////////
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class cardFlip extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public cardFlip(float fromDegrees, float toDegrees, float centerX, float centerY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
public DisplayNextView createDisplayNextView (boolean isFirstImage, ImageView llFrontView, ImageView llBackView)
{
return new DisplayNextView(isFirstImage,llFrontView, llBackView);
}
@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;
ImageView image1;
ImageView image2;
public DisplayNextView(boolean currentView, ImageView llFrontView, ImageView llBackView) {
mCurrentView = currentView;
this.image1 = llFrontView;
this.image2 = llBackView;
}
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;
ImageView image1;
ImageView image2;
public SwapViews(boolean isFirstView, ImageView image12, ImageView 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;
cardFlip rotation;
if (mIsFirstView) {
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
image2.requestFocus();
rotation = new cardFlip(90, 0, centerX, centerY);
} else {
image2.setVisibility(View.GONE);
image1.setVisibility(View.VISIBLE);
image1.requestFocus();
rotation = new cardFlip(-90, 0, centerX, centerY);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
if (mIsFirstView) {
image2.startAnimation(rotation);
} else {
image1.startAnimation(rotation);
}
}
}
}
}
回答1:
You can try this ..
public class SplashActivity extends Activity {
// protected boolean _active = true;
protected int _splashTime = 5000;
private ImageView imag;
AnimationDrawable frameAnimation ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashactivity);
imag = (ImageView) findViewById(R.id.splashactivity_imag);
imag.setBackgroundResource(R.drawable.splash_animation);
frameAnimation = (AnimationDrawable) imag
.getBackground();
Thread splashTread = new Thread() {
public void run() {
try {
sleep(_splashTime);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent;
intent = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
};
splashTread.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
frameAnimation.start();
}
}
drawable in create new xml...
splash_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/ac"
android:duration="200"/>
<item
android:drawable="@drawable/ab"
android:duration="200"/>
<item
android:drawable="@drawable/aa"
android:duration="200"/>
<item
android:drawable="@drawable/z"
android:duration="200"/>
<item
android:drawable="@drawable/y"
android:duration="200"/>
<item
android:drawable="@drawable/x"
android:duration="200"/>
<item
android:drawable="@drawable/w"
android:duration="200"/>
<item
android:drawable="@drawable/v"
android:duration="200"/>
<item
android:drawable="@drawable/u"
android:duration="200"/>
<item
android:drawable="@drawable/t"
android:duration="200"/>
<item
android:drawable="@drawable/s"
android:duration="200"/>
<item
android:drawable="@drawable/q"
android:duration="200"/>
<item
android:drawable="@drawable/p"
android:duration="200"/>
<item
android:drawable="@drawable/n"
android:duration="300"/>
<item
android:drawable="@drawable/o"
android:duration="300"/>
<item
android:drawable="@drawable/m"
android:duration="200"/>
<item
android:drawable="@drawable/l"
android:duration="200"/>
<item
android:drawable="@drawable/k"
android:duration="200"/>
<item
android:drawable="@drawable/j"
android:duration="200"/>
<item
android:drawable="@drawable/i"
android:duration="200"/>
<item
android:drawable="@drawable/h"
android:duration="200"/>
<item
android:drawable="@drawable/g"
android:duration="200"/>
<item
android:drawable="@drawable/f"
android:duration="200"/>
<item
android:drawable="@drawable/e"
android:duration="200"/>
<item
android:drawable="@drawable/d"
android:duration="200"/>
<item
android:drawable="@drawable/c"
android:duration="200"/>
<item
android:drawable="@drawable/b"
android:duration="200"/>
<item
android:drawable="@drawable/a"
android:duration="200"/>
</animation-list>
来源:https://stackoverflow.com/questions/9833988/android-3d-flip-animation