我沒有看到這個問題呢。但是如果你需要在佈局之間翻轉,我可以提供我自己的代碼如何使用翻轉功能。首先,您應該在res.layout文件夾中創建未來佈局的單獨頁面,如page_1.xml。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/GreenColor"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="1"
android:textSize="120dip">
</TextView>
</LinearLayout>
你做了之後,描述一個main.xml中文件包括外部XML的網頁:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper android:id="@+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include android:id="@+id/libraryView1" layout="@layout/page_1" />
<include android:id="@+id/libraryView2" layout="@layout/page_2" />
<include android:id="@+id/libraryView3" layout="@layout/page_3" />
</ViewFlipper>
</RelativeLayout>
比類文件,不要忘記申報VF =(ViewFlipper)findViewById( R.id.ViewFlipper01); 你應該下一步覆蓋的onTouchEvent來計算新的座標並顯示翻頁的動畫:
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValueX = touchevent.getX();
oldTouchValueY = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
//if(this.searchOk==false) return false;
float currentX = touchevent.getX();
float currentY = touchevent.getY();
float deltaX = getAbs(oldTouchValueX - currentX);
float deltaY = getAbs(oldTouchValueY - currentY);
if (deltaX >= deltaY)
{
if (oldTouchValueX < currentX)
{
vf.setInAnimation(inFromLeftAnimation());
vf.setOutAnimation(outToRightAnimation());
vf.showNext();
}
if (oldTouchValueX > currentX)
{
vf.setInAnimation(inFromRightAnimation());
vf.setOutAnimation(outToLeftAnimation());
vf.showPrevious();
}
} //if deltaX
else
{
if (oldTouchValueY < currentY)
{
vf.setInAnimation(inFromUpAnimation());
vf.setOutAnimation(outToDownAnimation());
vf.showNext();
}
if (oldTouchValueY > currentY)
{
vf.setInAnimation(inFromDownAnimation());
vf.setOutAnimation(outToUpAnimation());
vf.showPrevious();
}
} //Else
break;
}
}
return false;
}
,最後一個 - 描述了在的onTouchEvent提到動畫的方法:
public static Animation inFromRightAnimation()
{
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
inFromRight.setDuration(350);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation()
{
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
outtoLeft.setDuration(350);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
寫類似其餘的進/出翻轉方向。
感謝您的迅速回復。你的代碼是非常有用的,但不幸的是,我想我沒有描述那個問題,它並沒有回答這個問題。我有一個包含兩個不同廣告網絡瀏覽的佈局的小視圖快速翻轉器,當第一個失敗時,當我調用vf.shownext()時,視圖應該翻轉到第二個佈局。但它永遠不會翻轉。只有當我爲第一個網絡啓用TEST廣告時,該視圖才能正確翻轉到第二個網絡,我不知道爲什麼。 – Jason