Android的3D翻转动画 [英] Android 3d flip animation

查看:200
本文介绍了Android的3D翻转动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipes的创建Android卡game.For某种原因,我的应用程序崩溃时我称之为动画。一个变化我不得不做出是在翻转类。那是因为在动画法createDisplayNextView改变layoutview到ImageView的有一个错误。

  //////// ANIMATION
    私人ImageView的前视图;
    私人ImageView的BackView;
    私人布尔isFirstImage = TRUE;
私人无效applyRotation(浮点启动,浮结束){
    //查找图像的中心    前视图=(ImageView的)findViewById(R.id.imgC1);
    BackView =(ImageView的)findViewById(R.drawable.clubs2);
    BackView.setVisibility(View.GONE);    最终浮动的centerX = FrontView.getWidth()/ 2.0F;
    最终浮动centerY = FrontView.getHeight()/ 2.0F;    //创建与所提供的参数的新3D旋转
    //动画监听器被用来触发下一个动画
    最后cardFlip旋转=新cardFlip(开始,结束的centerX,centerY);
    rotation.setDuration(500);
    rotation.setFillAfter(真);
    rotation.setInterpolator(新AccelerateInterpolator());
    rotation.setAnimationListener(rotation.createDisplayNextView(isFirstImage,前视图,BackView));    如果(isFirstImage){
        FrontView.startAnimation(旋转);
    }其他{
        BackView.startAnimation(旋转);
    }
}
///////////////

这是翻转类:

  //////////////// FLIP类//////////////    进口android.graphics.Camera;
    进口android.graphics.Matrix;
    进口android.view.View;
    进口android.view.animation.Animation;
    进口android.view.animation.DecelerateInterpolator;
    进口android.view.animation.Transformation;
    进口android.widget.ImageView;
    进口android.widget.LinearLayout;    公共类cardFlip扩展动画{
    私人最终浮动mFromDegrees;
    私人最终浮动mToDegrees;
    私人最终浮动mCenterX;
    私人最终浮动mCenterY;
    私人相机mCamera;    公共cardFlip(浮点fromDegrees,浮toDegrees,浮动的centerX,浮centerY){
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX =的centerX;
mCenterY = centerY;
    }
    公共DisplayNextView createDisplayNextView(布尔isFirstImage,ImageView的llFrontView,ImageView的llBackView)
    {
        返回新DisplayNextView(isFirstImage,llFrontView,llBackView);
    }    @覆盖
    公共无效初始化(INT宽度,高度INT,INT上级宽度,诠释上级高度){
        super.initialize(宽度,高度,上级宽度,上级高度);
        mCamera =新相机();
    }    @覆盖
    保护无效applyTransformation(浮动interpolatedTime,变换T){
        最终浮动fromDegrees = mFromDegrees;
        浮度= fromDegrees +((mToDegrees - fromDegrees)* interpolatedTime);        最终浮动的centerX = mCenterX;
        最终浮动centerY = mCenterY;
        最终的摄像头摄像头= mCamera;        最后矩阵的矩阵= t.getMatrix();
        camera.save();
        camera.rotateY(度);
        camera.getMatrix(矩阵);
        camera.restore();        。矩阵preTranslate(-centerX,-centerY);
        matrix.postTranslate(的centerX,centerY);    }
        公共final类DisplayNextView实现Animation.AnimationListener {
            私人布尔mCurrentView;
            ImageView的image1的;
            ImageView的IMAGE2;            公共DisplayNextView(布尔currentView,ImageView的llFrontView,ImageView的llBackView){
                mCurrentView = currentView;
                this.image1 = llFrontView;
                this.image2 = llBackView;
            }    公共无效onAnimationStart(动画动画){
    }    公共无效onAnimationEnd(动画动画){
        image1.post(新SwapViews(mCurrentView,此搜索,图像2));
    }    公共无效onAnimationRepeat(动画动画){
    }    公共final类SwapViews实现Runnable {
        私人布尔mIsFirstView;
        ImageView的image1的;
        ImageView的IMAGE2;    公共SwapViews(布尔isFirstView,ImageView的image12,ImageView的image22){
         mIsFirstView = isFirstView;
         this.image1 = image12;
         this.image2 = image22;
    }    公共无效的run(){
         最终浮动的centerX = image1.getWidth()/ 2.0F;
         最终浮动centerY = image1.getHeight()/ 2.0F;
         cardFlip转动;         如果(mIsFirstView){
          image1.setVisibility(View.GONE);
          image2.setVisibility(View.VISIBLE);
          image2.requestFocus();             旋转=新cardFlip(90,0,的centerX,centerY);
         }其他{
          image2.setVisibility(View.GONE);
          image1.setVisibility(View.VISIBLE);
          image1.requestFocus();             旋转=新cardFlip(-90,0,的centerX,centerY);
         }         rotation.setDuration(500);
         rotation.setFillAfter(真);
         rotation.setInterpolator(新DecelerateInterpolator());         如果(mIsFirstView){
             image2.startAnimation(旋转);
         }其他{
             image1.startAnimation(旋转);
         }
    }
}
    }    }


解决方案

您可以试试这个。

 公共类SplashActivity延伸活动{
    //保护布尔_active = TRUE;
    保护INT _splashTime = 5000;
    私人ImageView的IMAG;
    AnimationDrawable frameAnimation;
    公共无效的onCreate(捆绑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();             螺纹splashTread =新主题(){
                公共无效的run(){
                    尝试{
                        睡眠(_splashTime);
                    }赶上(InterruptedException的E){
                        e.printStackTrace();
                    } {最后
                        意图意图;
                        意图=新意图(SplashActivity.this,HomeActivity.class);
                        startActivity(意向);
                        完();
                    }
                }
            };
            splashTread.start();
        }    @覆盖
    公共无效onWindowFocusChanged(布尔hasFocus){
        super.onWindowFocusChanged(hasFocus);
        frameAnimation.start();    }}

在绘制的创建新的XML ......

splash_animation.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<动画列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:单稳=真正的>    <项目
        机器人:可绘制=@绘制/ AC
        机器人:时间=200/>
    <项目
        机器人:可绘制=@绘制/ AB
        机器人:时间=200/>
    <项目
        机器人:可绘制=@绘制/ AA
        机器人:时间=200/>
    <项目
        机器人:可绘制=@绘制/ Z
        机器人:时间=200/>
   <项目
        机器人:可绘制=@绘制/ Y
        机器人:时间=200/>
   <项目
        机器人:可绘制=@绘制/ X
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ W
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ V
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ U
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/吨
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ S
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ Q
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ P
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ N
        机器人:时间=300/>
<项目
        机器人:可绘制=@绘制/ O
        机器人:时间=300/>
<项目
        机器人:可绘制=@绘制/ M
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ L
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ K
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ J
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/我
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ H
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ G
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ F
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ E
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ D
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ C
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ B
        机器人:时间=200/>
<项目
        机器人:可绘制=@绘制/ A
        机器人:时间=200/>
< /动漫列表>

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

    }

解决方案

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>

这篇关于Android的3D翻转动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆