android viewflipper实现左右滑动切换显示图片
本文实例为大家分享了android viewflipper实现左右滑动切换显示图片的具体代码,供大家参考,具体内容如下
1.首先定义四个动画文件,表示当view切换的时候的显示效果
in_leftright.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
in_rightleft.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
out_leftright.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
out_rightleft.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
2.在main.xml中添加ViewFlipper控件,里面放三个LinearLayout,表示3个view
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ViewFlipper android:id="@+id/viewFlipper" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- first page --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/a001" /> </LinearLayout> <!-- second page --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/a002" /> </LinearLayout> <!-- third page --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/a003" /> </LinearLayout> </ViewFlipper> </LinearLayout>
3.最后在activity里的onTouchEvent事件中,来判断是往哪个方向移动
package org.example.viewflipper; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.widget.ViewFlipper; public class ViewFlipperActivity extends Activity { private ViewFlipper viewFlipper = null; float startX; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // init widget viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); break; case MotionEvent.ACTION_UP: if (event.getX() > startX) {// flip to right viewFlipper.setInAnimation(this, R.anim.in_leftright); viewFlipper.setOutAnimation(this, R.anim.out_leftright); viewFlipper.showNext(); } else {// flip to left viewFlipper.setInAnimation(this, R.anim.in_rightleft); viewFlipper.setOutAnimation(this, R.anim.out_rightleft); viewFlipper.showPrevious(); } } return super.onTouchEvent(event); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)