Android中轴旋转特效实现,几张图片自动切换

来源:互联网 发布:行知艺术师范学院 编辑:程序博客网 时间:2024/05/01 13:49

几张图片以3D立体旋转的效果进行切换(此例由此处参考而来:

public class MainActivity extends Activity{    private LinearLayout layout;  //根布局;    private ImageView picture, picture1;  //用于展示图片详细的ImageView;    int[] imgs = {R.drawable.bird, R.drawable.winter, R.drawable.great_wall, R.drawable.water_fall};  //创建一个数组,存放要切换的图片;    Boolean flag = true;      int index = -1;                                       //要加载的图片脚标;    @Override    protected void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        layout = (LinearLayout) findViewById(R.id.layout);        picture = (ImageView) findViewById(R.id.picture);           picture1 = (ImageView) findViewById(R.id.picture1);             picture.setVisibility(View.GONE);        new Thread()        {            @Override            public void run()             {                while(flag)                {                    if(index < imgs.length-1)                    {                                       index++;                        handler.sendEmptyMessage(0);                    }                               else if(index >= imgs.length-1)                    {                        flag = false;                    }                    try                     {                        Thread.sleep(3500);                    } catch (InterruptedException e)                     {                        e.printStackTrace();                    }                }            }        }.start();    }    Handler handler = new Handler()    {        @Override        public void handleMessage(Message msg)         {            super.handleMessage(msg);            if(msg.what == 0)            {                if(index % 2 == 1)                {                    leftToRigth();                }                else if(index%2==0)                {                    rigthToLeft();                }            }        }    };    public void rigthToLeft()    {        // 奇数张图片转换到偶数张;        picture.setImageResource(imgs[index]);        // 获取布局的中心点位置,作为旋转的中心点        float centerX = layout.getWidth() / 2f;        float centerY = layout.getHeight() / 2f;        // 构建3D旋转动画对象,旋转角度为360到270度,这使得ImageView将会从可见变为不可见,并且旋转的方向是相反的        final Rotate3dAnimation rotation = new Rotate3dAnimation(360, 270, centerX,                centerY, 310.0f, true);        // 动画持续时间500毫秒        rotation.setDuration(500);        // 动画完成后保持完成的状态        rotation.setFillAfter(true);        rotation.setInterpolator(new AccelerateInterpolator());        // 设置动画的监听器        rotation.setAnimationListener(new TurnToListView());        layout.startAnimation(rotation);    }    public void leftToRigth()    {        // 偶数张转换到奇数张图片;        picture1.setImageResource(imgs[index]);        // 获取布局的中心点位置,作为旋转的中心点        float centerX = layout.getWidth() / 2f;        float centerY = layout.getHeight() / 2f;        // 构建3D旋转动画对象,旋转角度为0到90度,这使得ListView将会从可见变为不可见        final Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,                310.0f, true);        // 动画持续时间500毫秒        rotation.setDuration(500);        // 动画完成后保持完成的状态        rotation.setFillAfter(true);        rotation.setInterpolator(new AccelerateInterpolator());        // 设置动画的监听器        rotation.setAnimationListener(new TurnToImageView());        layout.startAnimation(rotation);            }    /**     * 注册在ListView点击动画中的动画监听器,用于完成ListView的后续动画。     */    class TurnToImageView implements AnimationListener     {        @Override        public void onAnimationStart(Animation animation)         {        }        /**         * 当ListView的动画完成后,还需要再启动ImageView的动画,让ImageView从不可见变为可见         */        @Override        public void onAnimationEnd(Animation animation)         {            // 获取布局的中心点位置,作为旋转的中心点            float centerX = layout.getWidth() / 2f;            float centerY = layout.getHeight() / 2f;            picture.setVisibility(View.GONE);            picture1.setVisibility(View.VISIBLE);            picture1.requestFocus();            // 构建3D旋转动画对象,旋转角度为270到360度,这使得ImageView将会从不可见变为可见            final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,                    310.0f, false);            // 动画持续时间500毫秒            rotation.setDuration(500);            // 动画完成后保持完成的状态            rotation.setFillAfter(true);            rotation.setInterpolator(new AccelerateInterpolator());            layout.startAnimation(rotation);        }        @Override        public void onAnimationRepeat(Animation animation)         {        }    }    /**     * 注册在ImageView点击动画中的动画监听器,用于完成ImageView的后续动画。     */    class TurnToListView implements AnimationListener     {        @Override        public void onAnimationStart(Animation animation)         {        }        /**         * 当ImageView的动画完成后,还需要再启动ListView的动画,让ListView从不可见变为可见         */        @Override        public void onAnimationEnd(Animation animation)         {            // 获取布局的中心点位置,作为旋转的中心点            float centerX = layout.getWidth() / 2f;            float centerY = layout.getHeight() / 2f;            picture1.setVisibility(View.GONE);            picture.setVisibility(View.VISIBLE);            picture.requestFocus();            // 构建3D旋转动画对象,旋转角度为90到0度,这使得ListView将会从不可见变为可见,从而回到原点            final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,                    310.0f, false);            // 动画持续时间500毫秒            rotation.setDuration(500);            // 动画完成后保持完成的状态            rotation.setFillAfter(true);            rotation.setInterpolator(new AccelerateInterpolator());            layout.startAnimation(rotation);        }        @Override        public void onAnimationRepeat(Animation animation)        {        }    }}

自定义类继承Animation:

package com.lu.animotion;import android.graphics.Camera;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.Transformation;/*可以看到,这个类的构造函数中接收一些3D旋转时所需用到的参数,比如旋转开始和结束的角度, * 旋转的中心点等。然后重点看下applyTransformation()方法,首先根据动画播放的 * 时间来计算出当前旋转的角度,然后让Camera也根据动画播放的时间在Z轴进行一定的偏移, * 使视图有远离视角的感觉。接着调用Camera的rotateY()方法,让视图围绕Y轴进行旋转, * 从而产生立体旋转的效果。最后通过Matrix来确定旋转的中心点的位置。有了这个工具类之后, * 我们就可以借助它非常简单地实现中轴旋转的特效了。 *//** * An animation that rotates the view on the Y axis between two specified angles. * This animation also adds a translation on the Z axis (depth) to improve the effect. */public class Rotate3dAnimation 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;    /**     * Creates a new 3D rotation on the Y axis. The rotation is defined by its     * start angle and its end angle. Both angles are in degrees. The rotation     * is performed around a center point on the 2D space, definied by a pair     * of X and Y coordinates, called centerX and centerY. When the animation     * starts, a translation on the Z axis (depth) is performed. The length     * of the translation can be specified, as well as whether the translation     * should be reversed in time.     *     * @param fromDegrees the start angle of the 3D rotation     * @param toDegrees the end angle of the 3D rotation     * @param centerX the X center of the 3D rotation     * @param centerY the Y center of the 3D rotation     * @param reverse true if the translation should be reversed, false otherwise     */    public Rotate3dAnimation(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);    }}
0 0
原创粉丝点击