标签:
activity_main.xml
<LinearLayout 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" android:orientation="vertical" > <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ViewFlipper> </LinearLayout>
package com.example.viewflipper; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.widget.ImageView; import android.widget.ViewFlipper; public class MainActivity extends Activity { private ViewFlipper flipper; private int[] resId = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4 }; private float startX; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flipper = (ViewFlipper) findViewById(R.id.flipper); // 动态导入的方式,加入子view for (int i = 0; i < resId.length; i++) { flipper.addView(getImageView(resId[i])); } // 进入动画--取消自动播放功能 // flipper.setInAnimation(this, R.anim.left_in); // flipper.setOutAnimation(this, R.anim.left_out); // flipper.setFlipInterval(3000); // ; // flipper.startFlipping(); } // 通过id获取图片 private ImageView getImageView(int resId) { ImageView imageView = new ImageView(this); // 铺满屏幕 imageView.setBackgroundResource(resId); return imageView; } /** * 处理手势事件 */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 获取起始点的x轴的坐标 startX = event.getX(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: // 向右滑动 if (event.getX() > startX + 100) { flipper.setInAnimation(this, R.anim.left_in); flipper.setOutAnimation(this, R.anim.rightf_out); // 显示前一页 flipper.showPrevious(); } // 向左边滑动 if (event.getX() + 100 < startX) { flipper.setInAnimation(this, R.anim.right_in); flipper.setOutAnimation(this, R.anim.left_out); // 显示前一页 flipper.showNext(); } break; default: break; } return super.onTouchEvent(event); } }
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="-100%p" android:toXDelta="0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u013210620/article/details/47364951