1. 程式人生 > >ViewFlipper 3D旋轉動畫實現 介面切換

ViewFlipper 3D旋轉動畫實現 介面切換

先來看看效果:
[img]http://dl2.iteye.com/upload/attachment/0094/6152/35ad98ce-42ee-3c85-aee4-74642f902e68.png[/img]

簡單介紹一下,兩個介面,以水平中線旋轉180度,切換介面。

上程式碼:
佈局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#a8a8a8">

<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<include
android:id="@+id/one_layout1"
layout="@layout/activity_one" />

<include
android:id="@+id/two_layout2"
layout="@layout/activity_two"
android:visibility="gone" />
</ViewFlipper>

</RelativeLayout>


Activity關鍵程式碼:
	private void changeOneOrTwo()
{
float halfWidth = viewFlipper.getWidth() / 2.0f;
float halfHeight = viewFlipper.getHeight() / 2.0f;
int duration = 300;
int depthz = 0;// viewFlipper.getWidth()/2;

if (oneOrTwo == 1)
{
oneOrTwo = 2;
Rotate3dAnimation in = new Rotate3dAnimation(-90, 0, halfWidth,
halfHeight, depthz, false);
in.setDuration(duration);
in.setStartOffset(duration);

viewFlipper.setInAnimation(in);
Rotate3dAnimation out = new Rotate3dAnimation(0, 90, halfWidth,
halfHeight, depthz, false);
out.setDuration(duration);

viewFlipper.setOutAnimation(out);
viewFlipper.showNext();

} else
{
oneOrTwo = 1;
one_layout1.setVisibility(View.VISIBLE);
two_layout2.setVisibility(View.GONE);

Rotate3dAnimation in = new Rotate3dAnimation(90, 0, halfWidth,
halfHeight, depthz, false);
in.setDuration(duration);
in.setStartOffset(duration);
viewFlipper.setInAnimation(in);

Rotate3dAnimation out = new Rotate3dAnimation(0, -90, halfWidth,
halfHeight, depthz, false);
out.setDuration(duration);

viewFlipper.setOutAnimation(out);
viewFlipper.showPrevious();
}
}


程式碼中還有兩個動畫類,專案程式碼已分享到github:
[url]https://github.com/kk-java/MyViewFlipper[/url]