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

WBOY
풀어 주다: 2016-06-21 09:13:21
원래의
1119명이 탐색했습니다.

    

/**

 * 自定义动画,利用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();

}

}


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!