android 屏幕切换
16lz
2021-01-26
引用:http://blog.sina.com.cn/s/blog_78e3ae430100py4p.html
幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。
通过查看 OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数: lsetInAnimation:设置 View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。
- setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
- showNext: 调用该函数来显示FrameLayout里面的下一个View。
- showPrevious: 调用该函数来显示FrameLayout里面的上一个View。
- isFlipping: 用来判断View切换是否正在进行
- setFilpInterval:设置View之间切换的时间间隔
- startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
- stopFlipping: 停止View切换
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ViewFlipperandroid:id="@+id/details"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:persistentDrawingCache="animation"
- android:flipInterval="1000"
- android:inAnimation="@anim/push_left_in"
- android:outAnimation="@anim/push_left_out"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image1"
- android:src="@drawable/dell1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image2"
- android:src="@drawable/lg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image3"
- android:src="@drawable/lenovo"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- </ViewFlipper>
- </LinearLayout>
- res\anim\push_left_in.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="100%p"
- android:toXDelta="0"
- android:duration="500"/>
- <alpha
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="500"/>
- </set>
- res\anim\push_left_out.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="0"
- android:toXDelta="-100%p"
- android:duration="500"/>
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:duration="500"/>
- </set>
- publicclassTestActivityextendsActivity{
- privateViewFlippermViewFlipper;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ButtonbuttonNext1=(Button)findViewById(R.id.Button_next1);
- mViewFlipper=(ViewFlipper)findViewById(R.id.flipper);
- buttonNext1.setOnClickListener(newView.OnClickListener(){
- publicvoidonClick(Viewview){
- //在layout中定义的属性,也可以在代码中指定
- //mViewFlipper.setInAnimation(getApplicationContext(),R.anim.push_left_in);
- //mViewFlipper.setOutAnimation(getApplicationContext(),R.anim.push_left_out);
- //mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
- //mViewFlipper.setFlipInterval(1000);
- mViewFlipper.showNext();
- //调用下面的函数将会循环显示mViewFlipper内的所有View。
- //mViewFlipper.startFlipping();
- }
- });
- ButtonbuttonNext2=(Button)findViewById(R.id.Button_next2);
- buttonNext2.setOnClickListener(newView.OnClickListener(){
- publicvoidonClick(Viewview){
- mViewFlipper.showNext();
- }
- });
- ButtonbuttonNext3=(Button)findViewById(R.id.Button_next3);
- buttonNext3.setOnClickListener(newView.OnClickListener(){
- publicvoidonClick(Viewview){
- mViewFlipper.showNext();
- }
- });
- }
- }
- classTestActivityextendsActivityimplementsOnGestureListener,OnDoubleTapListener
- publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
- floatvelocityY){
- Log.d(tag,"...onFling...");
- if(e1.getX()>e2.getX()){//movetoleft
- mViewFlipper.showNext();
- }elseif(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);
- }else{
- returnfalse;
- }
- returntrue;
- }
- publicbooleanonDoubleTap(MotionEvente){
- Log.d(tag,"...onDoubleTap...");
- if(mViewFlipper.isFlipping()){
- mViewFlipper.stopFlipping();
- }else{
- mViewFlipper.startFlipping();
- }
- returntrue;
- }
- publicGestureDetector(OnGestureListenerlistener){
- this(null,listener,null);
- }
- publicGestureDetector(Contextcontext,OnGestureListenerlistener,Handlerhandler){
- if(handler!=null){
- mHandler=newGestureHandler(handler);
- }else{
- mHandler=newGestureHandler();
- }
- mListener=listener;
- if(listenerinstanceofOnDoubleTapListener){
- setOnDoubleTapListener((OnDoubleTapListener)listener);
- }
- init(context);
更多相关文章
- 迅速理解Android事件传递
- Android之AppWidget的按钮事件开启Activity
- Android(安卓)点击Notification事件处理
- android linphone中opengl显示的实现
- gradle切换为阿里云镜像
- Android——实现【搜索框】与点击搜索事件监听 —— SearchView
- 微信自动回复和自动抢红包实现原理(一):AccessibilityService的介绍
- Android(安卓)代码模拟ListView item的点击事件并且改变背景颜色
- Android简单实现 高德地图的定位与显示,点击按钮切换地图图层