自定义Animation、3D动画旋转_html/css_WEB-ITnose

WBOY
Release: 2016-06-21 09:13:21
Original
1118 people have browsed it

    

/**

 * 自定义动画,利用Camera可以实现3D效果 在Activity中调用一下方法 image = (ImageView)

 * findViewById(R.id.image) ; Bitmap bitmap =

 * BitmapFactory.decodeResource(getResources(), R.drawable.main_back_pic) ;

 * image.setImageBitmap(bitmap); image.startAnimation(new

 * MyAnimation(bitmap.getWidth()/2, bitmap.getHeight()/2, 3500));

 * */

public class MyAnimation extends Animation {


private int width;

private int height;

private int duration;// 持续的时间

private Camera camera = new Camera();


public MyAnimation(int width, int height, int duration) {

this.height = height;

this.width = width;

this.duration = duration;

}


@Override

public void initialize(int width, int height, int parentWidth,

int parentHeight) {

setDuration(duration);// 设置动画的执行时间

setFillAfter(true);// 动画结束后动画停留在动画的最后一帧,setFillBefore(true):动画结束后停留在第一帧

// 动画设置成匀速运动,设置运动状态,在XML文件里设置没有作用,必须用在Java代码中设置

setInterpolator(new LinearInterpolator());

super.initialize(width, height, parentWidth, parentHeight);

}


@Override

protected void applyTransformation(float interpolatedTime,

Transformation trans) {

/**

* 此方法中的两个参数,第一个interpolatedTime,代表了抽象动画进行的时间,不管时间进行多久,其参数数值都是从0到1

* 0代表动画开始,1代表动画结束, Transformation是对动画所做的改变 Camera提供的方法:getMatrix(Matrix

* matrix)将Camera所做的变化应用到matrix上 rotateX(float deg)将目标组件沿着X轴进行旋转

* rotateY(float deg)将目标组件沿着Y轴进行旋转 totateZ(float deg)将组件沿着Z轴进行旋转

* translate(float x,float y,float z)将目标组件在三维空间内进行位移转换

* applyToCanvas(Canvas canvas)把Camera所做的变化应用到Canvas

* */

super.applyTransformation(interpolatedTime, trans);

camera.save();

// 把目标组件在三维视图中进行切换

// 在第一次调用的时候nterpolatedTime的值为0,相当于把view移动了10个像素,以后越来越少,一个周期过去,再变向转动

camera.translate(0.0f, 0.0f, (10 - 10 * interpolatedTime));

// camera.rotateX(360 * interpolatedTime);

camera.rotateY(360 * interpolatedTime);

// camera.rotateZ(360 * interpolatedTime);

Matrix matrix = trans.getMatrix();

camera.getMatrix(matrix);

// preTranslate是指在setScale前,平移,postTranslate是指在setScale后平移

// 注意他们参数是平移的距离,而不是平移目的地的坐标!

// 由于缩放是以(0,0)为中心的,所以为了把界面的中心与(0,0)对齐,就要preTranslate(-centerX,

// -centerY),

// setScale完成后,调用postTranslate(centerX,

// centerY),再把图片移回来,这样看到的动画效果就是activity的界面图片从中心不停的缩放了

// centerX和centerY是界面中心的坐标

matrix.preTranslate(-width, -height);

matrix.postTranslate(width, height);


camera.restore();

}

}


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!