android翻转动画,界面不会颠倒

代码都是网上找的,原创是会我就不清楚了,不过开始的是有点问题的,布局会颠倒,我自己修改了一下,界面不会颠倒了,主要是一些参数的修改

package com.ljt.dome;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

public class CheckUtil {
/**

* @param position   0  或  -1      正面到反面 0 0 90   反面到正面 -1 270  360
* @param start  开始   
* @param end 结束
* @param mContainer  包含正反面的大布局
* @param rl_img  正面布局
* @param rl_imgs_content  反面布局
*/
public static void applyRotation(int position, float start, float end,final ViewGroup mContainer,RelativeLayout rl_img,RelativeLayout rl_imgs_content) {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
final RotateAnimation rotation = new RotateAnimation(start, end, centerX,
centerY, 310.0f, true);
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position,mContainer,rl_img,rl_imgs_content));
mContainer.startAnimation(rotation);
}
public static class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private final ViewGroup mContainer;
private final RelativeLayout rl_img;
private final RelativeLayout rl_imgs_content;
private DisplayNextView(int position,ViewGroup mContainer,RelativeLayout rl_img,RelativeLayout rl_imgs_content) {
mPosition = position;
this.rl_img=rl_img;
this.rl_imgs_content=rl_imgs_content;
this.mContainer=mContainer;
}
@Override
public void onAnimationStart(Animation animation) {
}


@Override
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews(mPosition,mContainer,rl_img,rl_imgs_content));
}

@Override
public void onAnimationRepeat(Animation animation) {
}
}
private static class SwapViews implements Runnable {
private final int mPosition;
private final ViewGroup mContainer;
private final RelativeLayout rl_img;
private final RelativeLayout rl_imgs_content;
public SwapViews(int position,ViewGroup mContainer,RelativeLayout rl_img,RelativeLayout rl_imgs_content) {
mPosition = position;
this.mContainer=mContainer;
this.rl_img=rl_img;
this.rl_imgs_content=rl_imgs_content;
}
@Override
public void run() {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
RotateAnimation rotation;
if (mPosition > -1) {
rl_img.setVisibility(View.GONE);
rl_imgs_content.setVisibility(View.VISIBLE);
rotation = new RotateAnimation(270, 360, centerX, centerY, 310.0f, false);
} else {
rl_imgs_content.setVisibility(View.GONE);
rl_img.setVisibility(View.VISIBLE);
rotation = new RotateAnimation(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
}



布局:

                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF"
                    android:gravity="center"
                    android:padding="8dp" >


                                            android:id="@+id/rl_img"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:gravity="center" >
                       
                   


                                            android:id="@+id/rl_imgs_content"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:gravity="center"
                        android:padding="8dp"
                        android:visibility="gone" >
                       
                   
               

使用

正面到反面

CheckUtil.applyRotation(0,0,90,mContainer,rl_img,rl_imgs_content);

反面到正面

CheckUtil.applyRotation(-1, 360, 270,mContainer,rl_img,rl_imgs_content);



动画类

package com.ljt.dome;


import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
public class RotateAnimation extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private Camera mCamera;


    public RotateAnimation(float fromDegrees, float toDegrees,
            float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }


    @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();
        if (mReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();


        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}

以上就是所有代码了

你可能感兴趣的:(Android)