我们今天就来说说android中的3D效果,那么android都用到哪些东西才能来实现一个3D的效果那,其实android中的3D效果是用animation配合camera就可以实现在apidemo里就有这样的实例我们首先做一个继承animation的类Rotate3d.java

package eoe.3d;


public class Rotate3d extends Animation{
private float mFromDegree;
private float mToDegree;
private float mCenterX;
private float mCenterY;
private float mLeft;
private float mTop;
private CameramCamera;
private static final StringTAG = " Rotate3d " ;

public Rotate3d( float fromDegree, float toDegree, float left, float top,
float centerX, float centerY){
this .mFromDegree = fromDegree;
this .mToDegree = toDegree;
this .mLeft = left;
this .mTop = top;
this .mCenterX = centerX;
this .mCenterY = centerY;

}

@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,Transformationt){
final float FromDegree = mFromDegree;
float degrees = FromDegree + (mToDegree - mFromDegree)
* interpolatedTime;
final float centerX = mCenterX;
final float centerY = mCenterY;
final Matrixmatrix = t.getMatrix();

if (degrees <= - 76.0f ){
degrees
= - 90.0f ;
mCamera.save();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
}
else if (degrees >= 76.0f ){
degrees
= 90.0f ;
mCamera.save();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
}
else {
mCamera.save();
// 这里很重要哦。
mCamera.translate( 0 , 0 ,centerX);
mCamera.rotateY(degrees);
mCamera.translate(
0 , 0 , - centerX);
mCamera.getMatrix(matrix);
mCamera.restore();
}

matrix.preTranslate(
- centerX, - centerY);
matrix.postTranslate(centerX,centerY);
}
}

public class Rotate3d extends Animation{

private float mFromDegree;

private float mToDegree;

private float mCenterX;

private float mCenterY;

private float mLeft;

private float mTop;

private CameramCamera;

private static final StringTAG = " Rotate3d " ;


public Rotate3d( float fromDegree, float toDegree, float left, float top,

float centerX, float centerY){

this .mFromDegree = fromDegree;

this .mToDegree = toDegree;

this .mLeft = left;

this .mTop = top;

this .mCenterX = centerX;

this .mCenterY = centerY;


}


@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,Transformationt){

final float FromDegree = mFromDegree;

float degrees = FromDegree + (mToDegree - mFromDegree)

* interpolatedTime;

final float centerX = mCenterX;

final float centerY = mCenterY;

final Matrixmatrix = t.getMatrix();


if (degrees <= - 76.0f ){

degrees
= - 90.0f ;

mCamera.save();

mCamera.rotateY(degrees);

mCamera.getMatrix(matrix);

mCamera.restore();

}
else if (degrees >= 76.0f ){

degrees
= 90.0f ;

mCamera.save();

mCamera.rotateY(degrees);

mCamera.getMatrix(matrix);

mCamera.restore();

}
else {

mCamera.save();

// 这里很重要哦。

mCamera.translate(
0 , 0 ,centerX);

mCamera.rotateY(degrees);

mCamera.translate(
0 , 0 , - centerX);

mCamera.getMatrix(matrix);

mCamera.restore();

}


matrix.preTranslate(
- centerX, - centerY);

matrix.postTranslate(centerX,centerY);

}

}

有了这个类一切都会变得简单的,接着只要在activity中写两个Rotate3d的对象,让两个view,分别做这两个对象的animation就好了;

//下面两句很关键哦,
Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY);

leftAnimation.setFillAfter(true);
leftAnimation.setDuration(1000);
rightAnimation.setFillAfter(true);
rightAnimation.setDuration(1000);

mImageView1.startAnimation(leftAnimation);
mImageView2.startAnimation(rightAnimation);

最后就是还要写一下xml.

<? xmlversion="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
="wrap_content"
>

< FrameLayout
android:layout_width ="fill_parent"
android:layout_height
="fill_parent" >

< ImageView
android:id ="@+id/image1"
android:layout_gravity
="center_horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:src
="@drawable/image1"
/>
< ImageView
android:id ="@+id/image2"
android:background
="#ffff0000"
android:layout_gravity
="center_horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:src
="@drawable/image2"
/>

</ FrameLayout >
</ LinearLayout >




也不知道我理解的对不对,希望大家多多的指点我,这样我才能在短时间内有所进步,谢谢大家的支持。

// 下面两句很关键哦,
Rotate3dleftAnimation = new Rotate3d( - 0 , - 90 , 0 , 0 ,mCenterX,mCenterY);
Rotate3drightAnimation
= new Rotate3d( - 0 + 90 , - 90 + 90 , 0.0f , 0.0f ,mCenterX,mCenterY);

leftAnimation.setFillAfter(
true );
leftAnimation.setDuration(
1000 );
rightAnimation.setFillAfter(
true );
rightAnimation.setDuration(
1000 );

mImageView1.startAnimation(leftAnimation);
mImageView2.startAnimation(rightAnimation);



更多相关文章

  1. Android中的Parcel机制 实现Bundle传递对象
  2. android shape
  3. Android(安卓)TextView设置跑马灯无效?
  4. Android应用开发Android(安卓)常见的设计模式
  5. android高仿抖音、点餐界面、天气项目、自定义view指示、爬取美
  6. android酷炫翻页效果+图形分析
  7. android之单选框
  8. android的Material Design点击涟漪效果
  9. Android7.0中文文档(API)-- RemoteViews

随机推荐

  1. Android自动化测试之MonkeyRunner-startA
  2. Ubuntu10.04 64(32)位 android开发环境建
  3. Android中各种Span的用法
  4. Android:如何设计一个完全不可见的Activi
  5. 代码设置android:icon,android:label
  6. Android(安卓)浏览网页
  7. android屏幕截图
  8. android sdk命令行操作笔记
  9. Android的NDK开发(1)————Android(安
  10. Android(安卓)问题记录