当前位置:操作系统 > 安卓/Android >>

Android中轴旋转特效实现,制作别样的图片浏览器

Android API Demos中有很多非常Nice的例子,这些例子的代码都写的很出色,如果大家把API Demos中的每个例子研究透了,那么恭喜你已经成为一个真正的Android高手了。这也算是给一些比较迷茫的Android开发者一个指出了一个提升自我能力的方向吧。API Demos中的例子众多,今天我们就来模仿其中一个3D变换的特效,来实现一种别样的图片浏览器。
 
既然是做中轴旋转的特效,那么肯定就要用到3D变换的功能。在Android中如果想要实现3D效果一般有两种选择,一是使用Open GL ES,二是使用Camera。Open GL ES使用起来太过复杂,一般是用于比较高级的3D特效或游戏,像比较简单的一些3D效果,使用Camera就足够了。
 
Camera中提供了三种旋转方法,分别是rotateX()、rotateY()和rotateZ,调用这三个方法,并传入相应的角度,就可以让视图围绕这三个轴进行旋转,而今天我们要做的中轴旋转效果其实就是让视图围绕Y轴进行旋转。 
 
                                  
 
那我们就开始动手吧,首先创建一个Android项目,起名叫做RotatePicBrowserDemo, 
 
而API Demos中已经给我们提供了一个非常好用的3D旋转动画的工具类Rotate3dAnimation,这个工具类就是使用Camera来实现的,我们先将这个这个类复制到项目中来,代码如下所示:
 
[java]  
/** 
 * 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);  
    }  
}  
可以看到,这个类的构造函数中接收一些3D旋转时所需用到的参数,比如旋转开始和结束的角度,旋转的中心点等。然后重点看下applyTransformation()方法,首先根据动画播放的时间来计算出当前旋转的角度,然后让Camera也根据动画播放的时间在Z轴进行一定的偏移,使视图有远离视角的感觉。接着调用Camera的rotateY()方法,让视图围绕Y轴进行旋转,从而产生立体旋转的效果。最后通过Matrix来确定旋转的中心点的位置。
 
有了这个工具类之后,我们就可以借助它非常简单地实现中轴旋转的特效了。接着创建一个图片的实体类Picture,代码如下所示:
 
[java] 
public class Picture {  
  
    /** 
     * 图片名称 
     */  
    private String name;  
  
    /** 
     * 图片对象的资源 
     */  
    private int resource;  
  
    public Picture(String name, int resource) {  
        this.name = name;  
        this.resource = resource;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public int getResource() {  
        return resource;  
    }  
  
}  
这个类中只有两个字段,name用于显示图片的名称,resource用于表示图片对应的资源。
然后创建图片列表的适配器PictureAdap
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,