动态切换view视图viewflipper组件

本文详细介绍了如何在Android应用中使用ViewFlipper组件实现图片轮播效果,包括布局设置、动画配置以及手动控制轮播等功能。通过设置ViewFlipper的翻转间隔、启动翻转以及通过按钮控制轮播,可以实现流畅的图片展示体验。
摘要由CSDN通过智能技术生成

个人理解viewflipper,他其实就是切换view视图很方便而已,在做广告的时候有利用的价值,它可以自己来进行图片的每隔多长时间来进行切换

Viewflipper是一个组件,可以在transitions中找到

我照着网页上面的步骤做了如下的操作,是可以实现的

Main.xml

<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" >

 

    <ViewFlipper

        android:id="@+id/viewFlipper1"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

       android:inAnimation="@anim/push_in"

        android:outAnimation="@anim/push_out"

        android:persistentDrawingCache="animation" >

 

        <LinearLayout

            android:id="@+id/lin1"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical" >

 

            <Button

                android:id="@+id/button1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="next1" />

 

            <TextView

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:text="@string/content" />

        </LinearLayout>

 

        <LinearLayout

            android:id="@+id/lin2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical" >

 

            <Button

                android:id="@+id/button2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="next2" />

 

            <TextView

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:text="@string/content" />

        </LinearLayout>

 

        <LinearLayout

            android:id="@+id/lin3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical" >

 

            <Button

                android:id="@+id/button3"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="next3" />

 

            <TextView

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:text="@string/content" />

        </LinearLayout>

    </ViewFlipper>

 

</RelativeLayout>

 

Push_in.xml

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

<set xmlns: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>

Push_out.xml

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

<set xmlns: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>

 

ViewFlipperActivity

public class ViewFlipperActivity extends Activity implements OnClickListener,OnGestureListener{

 

         private ViewFlipper vf;

         private Button btn1,btn2,btn3;

         private GestureDetector gd;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        gd=new GestureDetector(this);

        vf=(ViewFlipper)findViewById(R.id.viewFlipper1);

       vf.setFlipInterval(1000);//每个view切换的时间是200微秒

        vf.startFlipping();//开始不停地循环的显示

        btn1=(Button)findViewById(R.id.button1);

        btn2=(Button)findViewById(R.id.button2);

        btn3=(Button)findViewById(R.id.button3);

        btn1.setOnClickListener(this);

        btn2.setOnClickListener(this);

        btn3.setOnClickListener(this);

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

         @Override

         public void onClick(View v) {

                   if (vf.isFlipping()) {

                            vf.stopFlipping();//停止自动切换

                   }

                   switch (v.getId()) {

                   case R.id.button1:

                            vf.showNext();

                            break;

                   case R.id.button2:

                            vf.showNext();

                            break;

                   case R.id.button3:

                            vf.showNext();

                            break;

 

                   default:

                            break;

                   }

         }

 

         //这一步是必不可少的,主要就是要他的返回值的,因为我进过测试如果不用触摸事件的返回值,那么我的手势的onfling()就不会被调用

         @Override

         public boolean onTouchEvent(MotionEvent event) {

                   return gd.onTouchEvent(event);//要的就是这个返回值

         }

         @Override

         public boolean onDown(MotionEvent e) {

                   // TODO Auto-generated method stub

                   return false;

         }

 

         @Override

         public void onShowPress(MotionEvent e) {

                   // TODO Auto-generated method stub

                  

         }

 

         @Override

         public boolean onSingleTapUp(MotionEvent e) {

                   // TODO Auto-generated method stub

                   return false;

         }

 

         @Override

         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

                            float distanceY) {

                   // TODO Auto-generated method stub

                   return false;

         }

 

         @Override

         public void onLongPress(MotionEvent e) {

                   // TODO Auto-generated method stub

                  

         }

 

         @Override

         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

                            float velocityY) {

                   if (e1.getX()>e2.getX()) {

                            Toast.makeText(ViewFlipperActivity.this, "你是向←滑动的", Toast.LENGTH_SHORT).show();

                   }else {

                            Toast.makeText(ViewFlipperActivity.this, "你是向→滑动的", Toast.LENGTH_SHORT).show();

                           

                   }

                   return false;

         }

}

 

转载于:https://my.oschina.net/f839903061/blog/69049

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值