android 动画2

来源:互联网 发布:淘宝一键复制有违规吗 编辑:程序博客网 时间:2024/04/30 08:47
 

package com.anim;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class My3DAnimation extends Animation {
 
 private final float mFromDegrees;// 动画旋转的起始角度
 private final float mToDegrees;//动画旋转的结束角度
 private final float mCenterX;// 动画旋转原点的x坐标
 private final float mCenterY;// 动画旋转原点的y坐标
 private final float mDepthZ;// 在动画旋转时,在z轴上有一个来回的效果,该值表示在z轴上平移的最大距离
 private final boolean mReverse;// 如果为true,则动画反向旋转
 private Camera mCamera = new Camera();
 
 public My3DAnimation(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
 protected void applyTransformation(float interpolatedTime, Transformation t) {
  // 参数说明:
  // interpolatedTime:代表动画执行的进度,为一个大于等于0,小雨等于1的浮点数
  // t:为动画变换的载体,该载体即为每一帧变换信息的载体,主要包含两个属性,
  //  1.Alpha:即该帧的透明度值  2. Matrix:图形变换信息,包含旋转,缩放,平移等信息
//  super.applyTransformation(interpolatedTime, 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();
  // 根据对pre和post的理解(分别将变换效果置于变换最前和最后),
  // 结合Canvas变换中缩放原点的实现原理,即该动画会以(centerX,centerY)为原点在y轴上产生旋转效果
  matrix.preTranslate(-centerX, -centerY);
  matrix.postTranslate(centerX, centerY);
 }
 
 /**
  * Matrix介绍
  * Matrix可以方便的设置各种图形 变换信息,使用Canvas的concat()方法将Matrix设置的
  * 变换信息作用于Canvas上,实现变换效果,主要方法如下:
  * setTranslate(float x, float y);设置平移信息,x和y分别表示在x轴和y轴上平移的距离
  * setScale(float sx, float sy, float px, float py);设置缩放信息,sx和sy分别表示在x轴和y轴上的缩放倍数,px和py表示一个坐标,即缩放原点
  * setRotate(float degrees, float px, float px);设置旋转信息,degrees为旋转角度,后两个参数表示旋转原点坐标
  * setSinCos(float sinValue , float cosValue , float px , float py);利用sin或cos的值来表示转动的角度,后两位表示转动原点(z轴上的转动)
  * setSkew(float kx, float ky, float px, float py);设置倾斜信息,kx和ky表示在x轴和y轴上想倾斜度,后两位为倾斜原点
  * setConcat(Matrix a,Matrix b);将两个矩阵信息合并
  * 每种变换方法都对应有pre和post两种方法,因为在Matrix中设置各种变换信息是有顺序的。
  * 如: preTranslate()是将该平移操作放置在最开始执行
  *       postTranslate()是将该平移操作放置在最后执行
  *
  * Camera介绍
  * Camera主要实现了三维的平移和旋转,主要方法如下:
  * translate(float x,float y,float z);设置旋转信息,参数分别是在x,y,z轴上平移的角度
  * rotateX(float degrees);以x轴为轴心旋转degrees角度
  * rotateY(float degrees);以y轴为轴心旋转degrees角度
  * rotateZ(float degrees);以z轴为轴心旋转degrees角度
  * save(),restore(),用于保存和恢复变换的状态,当Camera变换完毕后,可将其变换值作用于Matrix上,使用Camera.getMatrix()方法
  */
}

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#40ffffff">
 <ImageView
  android:id="@+id/imageview1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/icon"></ImageView>
 <ImageView
  android:id="@+id/imageview2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/icon" />
 <ImageView
  android:id="@+id/imageview3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/icon" />
 <ImageView
  android:id="@+id/imageview4"
  android:layout_gravity="center"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/chat_input" />
 <ImageView
  android:id="@+id/imageview5"
  android:layout_gravity="center"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/chat_input" />
</LinearLayout>

 

 

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromAlpha="0.0"
 android:toAlpha="1.0"
 android:duration="1000" />

 

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromDegrees="0.0" android:toDegrees="-360.0"
 android:pivotX="50%" android:pivotY="50%"
 android:duration="3000" />
 <!-- 正数表示顺时针旋转 -->

 

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromXScale="2.0" android:toXScale="1.0"
 android:fromYScale="2.0" android:toYScale="1.0"
 android:pivotX="50%" android:pivotY="50%"
 android:duration="1000" />
 <!-- android:pivotX="" android:pivotY=""
  当我们用xml文件来定义缩放动画的时候,pivotX的值为浮点数时,缩放类型
  为Animation.ABSOLUTE,即缩放的轴心点为相对组件左上角的距离值,
  为百分比时,缩放类型为Animation.RELATIVE_TO_SELF,即相对组件本身大小的比例
  之来表示,,如上面的50%,当在后面加上一个p字母时,如50%p,
  则为Animation.RELATIVE_TO_PARENT ,以父组件的总长度来计算-->

 

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromXDelta="0.0" android:toXDelta="200.0"
 android:fromYDelta="0.0" android:toYDelta="200.0"
 android:duration="2000" />

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator" >
 <alpha
  android:fromAlpha="0.0"
  android:toAlpha="1.0"
  android:duration="1000" />
 <translate
  android:fromXDelta="0.0" android:toXDelta="200.0"
  android:fromYDelta="0.0" android:toYDelta="200.0"
  android:duration="1000" />
 <scale
  android:fromXScale="2.0" android:toXScale="1.0"
  android:fromYScale="2.0" android:toYScale="1.0"
  android:pivotX="50%" android:pivotY="50%"
  android:duration="1000" />
</set>

 


 

原创粉丝点击