进阶六之Android UI介面之(介面3D旋转)

天道酬勤。也许你付出了不一定得到回报,但不付出一定得不到回报。本讲内容:介面3D旋转示例一效果图:                  下面是res/layout/activity_main.xml 布局文件:

天道酬勤。也许你付出了不一定得到回报,但不付出一定得不到回报。


本讲内容:介面3D旋转


示例一效果图:

                  


下面是res/layout/activity_main.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout_main"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"    		
	android:orientation="vertical">
	
	<TextView
    	android:id="@+id/title"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
    	android:layout_gravity="center"
		android:gravity="center"
    	android:textColor="#ff0000"
    	android:text="@string/txt_main"/>		    

	<LinearLayout 
		android:layout_marginTop="50dip"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:gravity="center"
  		android:layout_gravity="center" >			
		<Button
			android:id="@+id/main_last"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="上一頁"/>
		<Button 
			android:id="@+id/main_next"
			android:layout_marginLeft="50dip"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="下一頁"/>
	</LinearLayout>
			
</LinearLayout>

下面是res/layout/next.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout_next"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"    		
	android:orientation="vertical">
	
	<TextView
    	android:id="@+id/title"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
    	android:layout_gravity="center"
		android:gravity="center"
    	android:textColor="#ff0000"
    	android:text="@string/txt_next"/>		    

	<LinearLayout 
		android:layout_marginTop="5dip"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:gravity="center"
  		android:layout_gravity="center" >			
		<Button
			android:id="@+id/next_last"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="上一頁"/>
		<Button 
			android:id="@+id/next_next"
			android:layout_marginLeft="50dip"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="下一頁"/>
	</LinearLayout>
			
</LinearLayout>

下面是res/values/string.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Rotate3D</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="txt_main">第一页\n\n珍爱生命</string>
    <string name="txt_next">第二页\n远离IT</string>

</resources>

下面是Rotate3D.java文件:

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;	// 旋转角度(angle)
		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);		// 位移x
			mCamera.rotateY(degrees);
			mCamera.translate(0, 0, -centerX);
			mCamera.getMatrix(matrix);
			mCamera.restore();
		}

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

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {
	private ViewGroup layoutmain;
	private ViewGroup layoutnext;
	
	private Button btn_MainLast;
	private Button btn_MainNext;
	private Button btn_NextLast;
	private Button btn_NextNext;
	
	private Rotate3D lQuest1Animation;
	private Rotate3D lQuest2Animation;
	private Rotate3D rQuest1Animation;
	private Rotate3D rQuest2Animation;
	private int mCenterX = 160;		// 320x480 的宽一半
	private int mCenterY = 240;		// 320x480 的高一半
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initAnimation();
        initMain();
	}
	
	
	
	private void initMain(){
		layoutmain = (LinearLayout)findViewById(R.id.layout_main);
		btn_MainLast = (Button)findViewById(R.id.main_last);
		btn_MainNext = (Button)findViewById(R.id.main_next);
		
		btn_MainLast.setOnClickListener(listener);
		btn_MainNext.setOnClickListener(listener);
	}
	
	private void initNext(){
        setContentView(R.layout.next);

		layoutnext = (LinearLayout)findViewById(R.id.layout_next);
		btn_NextLast = (Button)findViewById(R.id.next_last);
		btn_NextNext = (Button)findViewById(R.id.next_next);
		
		btn_NextLast.setOnClickListener(listener);
		btn_NextNext.setOnClickListener(listener);
	}
	
	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.main_last:	// 上一页
				layoutmain.startAnimation(lQuest1Animation);	// 当前页向左旋转(0,-90)
				initNext();
				layoutnext.startAnimation(lQuest2Animation);	// 下一页向左旋转(90, 0)
				break;
			case R.id.main_next:	// 下一页
				layoutmain.startAnimation(rQuest1Animation);	// 当前页向右旋转(0,90)
				initNext();
				layoutnext.startAnimation(rQuest2Animation);	// 下一页向右旋转(-90, 0)
				break;
			case R.id.next_last:
				layoutnext.startAnimation(lQuest1Animation);
				initMain();
				layoutmain.startAnimation(lQuest2Animation);
				break;
			case R.id.next_next:
				layoutnext.startAnimation(rQuest1Animation);
				initMain();
				layoutmain.startAnimation(rQuest2Animation);
				break;
			}
		}
	};

	public void initAnimation() {
		// 获取旋转中心
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();
		mCenterX = dm.widthPixels / 2;
		mCenterY = dm.heightPixels / 2;
		
		// 定义旋转方向
		int duration = 1000;
		lQuest1Animation = new Rotate3D(0, -90, mCenterX, mCenterY);	// 下一页的旋转方向(从0度转到-90,参考系为水平方向为0度)
		lQuest1Animation.setFillAfter(true);
		lQuest1Animation.setDuration(duration);

		lQuest2Animation = new Rotate3D(90, 0, mCenterX, mCenterY);		// 下一页的旋转方向(从90度转到0,参考系为水平方向为0度)(起始第一题)
		lQuest2Animation.setFillAfter(true);
		lQuest2Animation.setDuration(duration);

		rQuest1Animation = new Rotate3D(0, 90, mCenterX, mCenterY);		// 上一页的旋转方向(从0度转到90,参考系为水平方向为0度)
		rQuest1Animation.setFillAfter(true);
		rQuest1Animation.setDuration(duration);

		rQuest2Animation = new Rotate3D(-90, 0, mCenterX, mCenterY);	// 上一页的旋转方向(从-90度转到0,参考系为水平方向为0度)
		rQuest2Animation.setFillAfter(true);
		rQuest2Animation.setDuration(duration);
	}

}

Take your time and enjoy it


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值