项目中 用到控件的旋转效果 ,研究了一下 这里做个记录
旋转类
Rotate3d
package com.appdoll.rotate3dtest;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3d extends Animation {
private float fromDegree; // 旋转起始角度
private float toDegree; // 旋转终止角度
private float mCenterX; // 旋转中心x
private float mCenterY; // 旋转中心y
private Camera mCamera;
public Rotate3d(float fromDegree, float toDegree, float centerX, float centerY) {
this.fromDegree = fromDegree;
this.toDegree = toDegree;
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, Transformation t) {
final float FromDegree = fromDegree;
float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime;
final float centerX = mCenterX;
final float centerY = mCenterY;
final Matrix matrix = 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
package com.appdoll.rotate3dtest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Rotate3dTestActivity extends Activity {
private ViewGroup layout1,layout2;
Button btnNext1, btnNext2;
private Rotate3d layout1Animation ,layout2Animation;
private int mCenterX, mCenterY;
Context context=this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout1 = (ViewGroup) findViewById(R.id.layout1);
layout2 = (ViewGroup) findViewById(R.id.layout2);
btnNext1 = (Button) layout1.findViewById(R.id.btn_next1);
btnNext2 = (Button) layout2.findViewById(R.id.btn_next2);
initAnimation();
btnNext1.setOnClickListener(listener);
btnNext2.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_next1:
Toast.makeText(context, "next1", 0).show();
layout1.startAnimation(layout1Animation);
layout2.startAnimation(layout2Animation);
break;
case R.id.btn_next2:
Toast.makeText(context, "next2", 0).show();
layout2.startAnimation(layout1Animation);
layout1.startAnimation(layout2Animation);
break;
}
}
};
public void initAnimation(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
mCenterX = dm.widthPixels / 2;
mCenterY = dm.heightPixels / 2;
int duration = 1000;
layout1Animation = new Rotate3d(0, -90, mCenterX, mCenterY);
layout1Animation.setFillAfter(true);
layout1Animation.setDuration(duration);
layout2Animation = new Rotate3d(90, 0, mCenterX, mCenterY);
layout2Animation.setFillAfter(true);
layout2Animation.setDuration(duration);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#eeeeee"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<RelativeLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cccccc"
android:gravity="center_vertical"
android:orientation="vertical"
>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo"
/>
<Button
android:id="@+id/btn_next2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next2"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/p2"
/>
<Button
android:id="@+id/btn_next1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="next1"
/>
</RelativeLayout>
</FrameLayout>
</LinearLayout>