简单实现Android图片翻转动画效果

移动开发 Android
下面给大家分享一个有趣的动画:这里比较适合一张图片的翻转,如果是多张图片,可以参考APIDemo里的例子,就是加个ArrayAdapter,还是简单的,也可以自己发挥修改,实现自己想要的。这里的代码基本上可以直接运行项目了。

什么都不说,先看效果

这是原始图片的样子

这是翻转后的效果图

如果是你想要的效果,那么继续往下看,如果不是,那可以跳过了。这是一个动画,而不是用matrix实现的直接翻转图片。

我这个是根据APIDemo简单修改写的
需要一个Rotate3d 类,继承Animation

public class Rotate3d 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 Rotate3d(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);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

这个类可以直接拷过去,不用做任何的修改。其中的方法自己找相关资料研究。

#p#
main.xml里加个ImageView,如

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<ImageView 
android:id="@+id/image" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Rotate" 
android:textSize="50px" 
android:layout_x="150px"   
android:layout_y="30px" 
android:src="@drawable/ro"> 
></ImageView> 
</FrameLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

这个不需要解释吧,都可以看懂的

***,还需要一个activity类

如:

public class TestRotate extends Activity implements OnClickListener{  
 private mageView imageview;  
 private ViewGroup mContainer;  
 /**  
  *这个变量设置的是图片,如果是多张图片,那么可以用数组,如  
  *private static final int IMAGE = new int[]{  
  * R.drawable.ro,  
  * R.drawable.icon  
  *};  
  *有多少图片就放多少,我这里做的只是一张图片的翻转  
  *  
  */  
 private static final int IMAGE = R.drawable.ro;  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);          
        imageview = (ImageView) findViewById(R.id.image);  
        mContainer = (ViewGroup) findViewById(R.id.container);          
        /**  
         * 设置***显示的图片  
         * 如果是数组,那么可以写成IMAGE[int]  
         *   
         */  
        imageview.setImageResource(IMAGE);          
        /**  
         *   
         * 设置ImageView的OnClickListener  
         *   
         */          
        imageview.setClickable(true);  
        imageview.setFocusable(true);  
        imageview.setOnClickListener(this);  
    }  
    private void applyRotation(int position, float start, float end) {  
        // Find the center of the container  
        final float centerX = mContainer.getWidth() / 2.0f;  
        final float centerY = mContainer.getHeight() / 2.0f;  
        final Rotate3d rotation =  
                new Rotate3d(start, end, centerX, centerY, 310.0f, true);  
        rotation.setDuration(500);  
        rotation.setFillAfter(true);  
        rotation.setInterpolator(new AccelerateInterpolator());  
        rotation.setAnimationListener(new DisplayNextView(position));  
        mContainer.startAnimation(rotation);  
    }      
 @Override  
 public void onClick(View v) {  
  // TODO Auto-generated method stub  
  /**  
   *   
   * 调用这个方法,就是翻转图片  
   * 参数很简单,大家都应该看得懂  
   * 简单说下,***个是位置,第二是开始的角度,第三个是结束的角度  
   * 这里需要说明的是,如果是要回到上一张  
   * 把***个参数设置成-1就行了  
   *   
   */  
  applyRotation(0,0,90);  
 }  
 private final class DisplayNextView implements Animation.AnimationListener {  
        private final int mPosition;  
        private DisplayNextView(int position) {  
            mPosition = position;  
        }  
        public void onAnimationStart(Animation animation) {  
        }  
        public void onAnimationEnd(Animation animation) {  
            mContainer.post(new SwapViews(mPosition));  
        }  
        public void onAnimationRepeat(Animation animation) {  
        }  
    }  
    /**  
     * This class is responsible for swapping the views and start the second  
     * half of the animation.  
     */  
    private final class SwapViews implements Runnable {  
        private final int mPosition;  
        public SwapViews(int position) {  
            mPosition = position;  
        }  
        public void run() {  
            final float centerX = mContainer.getWidth() / 2.0f;  
            final float centerY = mContainer.getHeight() / 2.0f;  
            Rotate3d rotation;             
            if (mPosition > -1) {  
             imageview.setVisibility(View.VISIBLE);  
             imageview.requestFocus();  
                rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);  
            } else {  
             imageview.setVisibility(View.GONE);  
                rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);  
            }  
            rotation.setDuration(500);  
            rotation.setFillAfter(true);  
            rotation.setInterpolator(new DecelerateInterpolator());  
            mContainer.startAnimation(rotation);  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.

【编辑推荐】

Android学习笔记:Activity跳转

Android开发中插入新的Activity

Android开发:Activity之间的传值

Android应用开发教程:两个运行的Activity之间的通信

责任编辑:zhaolei 来源: 互联网
相关推荐

2017-02-06 13:00:49

Android翻转卡片动画效果

2011-07-08 10:15:15

IPhone 动画

2017-05-03 16:36:32

Android图片动画

2016-03-29 10:18:48

Android图片代码

2012-06-04 14:47:42

HTML5

2011-07-22 18:20:04

IOS View 动画

2022-03-29 11:28:24

HarmonyOS动画css

2015-01-23 16:29:44

2024-03-20 09:40:27

动画技巧CSS逐帧动画

2011-08-12 14:04:53

iPhone动画

2011-07-08 15:08:16

iPhone 图片

2011-08-16 18:13:42

IPhone开发UIView动画

2011-07-29 13:55:10

IPhone 动画

2009-09-22 12:59:58

ibmdwDojo

2011-08-10 14:40:23

iPhone动画

2012-05-17 13:17:26

HTML5

2015-06-18 10:33:02

iOS粘性动画

2011-07-19 13:07:26

iOS4 HTML5 动画

2011-02-17 10:54:59

CSS3变换 简单快捷

2015-09-16 09:20:34

WWDC苹果动画效果
点赞
收藏

51CTO技术栈公众号