android:滑屏ViewFlipper的使用
来源:互联网 发布:csgo的fps优化 编辑:程序博客网 时间:2024/06/06 15:45
屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。
通过查看OPhoneAPI文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数:
- setOutAnimation:设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
- showNext: 调用该函数来显示FrameLayout里面的下一个View。
- showPrevious: 调用该函数来显示FrameLayout里面的上一个View。
- isFlipping: 用来判断View切换是否正在进行
- setFilpInterval:设置View之间切换的时间间隔
- startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
- stopFlipping: 停止View切换
- <?xml
version= "1.0"encoding= "utf-8"?> - <LinearLayout
-
xmlns:android= -
android:orientation= -
android:layout_width= -
android:layout_height= -
<ViewFlipper android:id= -
android:layout_width= -
android:layout_height= -
android:persistentDrawingCache= -
android:flipInterval= -
android:inAnimation= - android:outAnimation="@anim/push_left_out"
- >
-
<LinearLayout -
android:orientation= -
android:layout_width= -
android:layout_height= -
<Button -
android:text= -
android:id= -
android:layout_width= -
android:layout_height= -
</Button> -
<ImageView -
android:id= -
android:src= -
android:layout_width= -
android:layout_height= -
</ImageView> -
</LinearLayout> -
-
<LinearLayout -
android:orientation= -
android:layout_width= -
android:layout_height= -
<Button -
android:text= -
android:id= -
android:layout_width= -
android:layout_height= -
</Button> -
<ImageView -
android:id= -
android:src= -
android:layout_width= -
android:layout_height= -
</ImageView> -
</LinearLayout> -
-
<LinearLayout -
android:orientation= -
android:layout_width= -
android:layout_height= -
<Button -
android:text= -
android:id= -
android:layout_width= -
android:layout_height= -
</Button> -
<ImageView -
android:id= -
android:src= -
android:layout_width= -
android:layout_height= -
</ImageView> -
</LinearLayout> -
-
</ViewFlipper> -
- </LinearLayout>
- res\anim\push_left_in.xml
- <?xml
version= "1.0"encoding= "utf-8"?> - <set
xmlns:android= "http://schemas.android.com/apk/res/android"> -
<translate -
android:fromXDelta= -
android:toXDelta= -
android:duration= -
<alpha -
android:fromAlpha= -
android:toAlpha= -
android:duration= /> - </set>
- res\anim\push_left_out.xml
- <?xml
version= "1.0"encoding= "utf-8"?> - <set
xmlns:android= "http://schemas.android.com/apk/res/android"> -
<translate -
android:fromXDelta= -
android:toXDelta= -
android:duration= -
<alpha -
android:fromAlpha= -
android:toAlpha= -
android:duration= /> - </set>
- public
class TestActivity extendsActivity { -
ViewFlipper mViewFlipper; -
-
void onCreate(Bundle savedInstanceState) { -
-
setContentView(R.layout.main); -
-
Button buttonNext1 = (Button) findViewById(R.id.Button_next1); -
mViewFlipper = (ViewFlipper) findViewById(R.id.flipper); -
buttonNext1.setOnClickListener( View.OnClickListener() { -
void onClick(View view) { -
- //
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); - //
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); - //
mViewFlipper.setPersistentDrawingCach e(ViewGroup.PERSISTENT_ALL_CACHES); - //
mViewFlipper.setFlipInterval(1000); -
mViewFlipper.showNext(); -
- //
mViewFlipper.startFlipping(); -
} -
}); -
-
Button buttonNext2 = (Button) findViewById(R.id.Button_next2); -
buttonNext2.setOnClickListener( View.OnClickListener() { -
void onClick(View view) { -
mViewFlipper.showNext(); -
} -
-
}); -
Button buttonNext3 = (Button) findViewById(R.id.Button_next3); -
buttonNext3.setOnClickListener( View.OnClickListener() { -
void onClick(View view) { -
mViewFlipper.showNext(); -
} -
-
}); -
-
} -
}
- class
TestActivity extendsActivity implementsOnGestureListener , OnDoubleTapListener
- public
boolean onFling(MotionEvent floate1, MotionEvent e2, velocityX, -
velocityY) { -
Log.d(tag, -
> //movee2.getX()) { to left -
mViewFlipper.showNext(); -
} if(e1.getX() < e2.getX()) { -
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in); -
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out); -
mViewFlipper.showPrevious(); -
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); -
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); -
} { -
false; -
} -
true; -
}
- public
boolean onDoubleTap(MotionEvent e) { -
Log.d(tag, -
{ -
mViewFlipper.stopFlipping(); -
} { -
mViewFlipper.startFlipping(); -
} -
true; -
}
-
GestureDetector(OnGestureListener listener) { -
listener, null); - }
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
public class MyActivity extends Activity implementsOnClickListener, OnGestureListener{