Android 3d翻转动画

时间:2012-03-23 03:57:19

标签: android class animation imageview flip

我正在使用eclipes创建一个Android卡游戏。由于某种原因,当我调用动画时,我的应用程序崩溃了。我必须做的一个改变是在Flip类中,那就是将layoutview更改为imageview,因为在动画方法中“createDisplayNextView”出错了。

////////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);
    }
}


///////////////

这是Flip类:

////////////////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 个答案:

答案 0 :(得分:0)

你可以试试这个..

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>