您的位置:首页 > 其它

ViewFlipper结合手势OnGestureListener制作的滑动切换效果

2017-04-10 18:16 351 查看
先要了解ViewFlipper,详细见:

http://gundumw100.iteye.com/admin/blogs/896840

OnGestureListener和OnDoubleTapListener接口定义:

public interface OnGestureListener {
// Touch down时触发, e为down时的MotionEvent
boolean onDown(MotionEvent e);
// 在Touch down之后一定时间(115ms)触发,e为down时的MotionEvent
void onShowPress(MotionEvent e);
// Touch up时触发,e为up时的MotionEvent
boolean onSingleTapUp(MotionEvent e);
// 滑动时触发,e1为down时的MotionEvent,e2为move时的MotionEvent
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
// 在Touch down之后一定时间(500ms)触发,e为down时的MotionEvent
void onLongPress(MotionEvent e);
// 滑动一段距离,up时触发,e1为down时的MotionEvent,e2为up时的MotionEvent
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
}

public interface OnDoubleTapListener {
// 完成一次单击,并确定没有二击事件后触发(300ms),e为down时的MotionEvent
boolean onSingleTapConfirmed(MotionEvent e);
// 第二次单击down时触发,e为第一次down时的MotionEvent
boolean onDoubleTap(MotionEvent e);
// 第二次单击down,move和up时都触发,e为不同时机下的MotionEvent
boolean onDoubleTapEvent(MotionEvent e);
}


实例:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class FlingSlideActivity extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
Button button1 = (Button) findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
Button button2 = (Button) findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
Button button3 = (Button) findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
mGestureDetector = new GestureDetector(this);
}
//别忘了覆盖onTouchEvent方法
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
//以下是OnGestureListener需要实现的方法
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if(e1.getX() > e2.getX()) {//向左滑动
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
mViewFlipper.showNext();
}else if(e1.getX() < e2.getX()) {//向右滑动
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);
mViewFlipper.showPrevious();
}else {
return false;
}
return true;
}

public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
//以下是OnDoubleTapListener需要实现的方法
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
mViewFlipper.startFlipping(); //双击自动切换界面
return true;
}
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
if(mViewFlipper.isFlipping()){ //单击结束自动切换
mViewFlipper.stopFlipping();
}
return false;
}

}


main.xml

<?xml version="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"
>
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:persistentDrawingCache="animation"
android:flipInterval="3000"
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="Next1"
android:id="@+id/Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image1"
android:src="@drawable/icon"
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="Next2"
android:id="@+id/Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image2"
android:src="@drawable/icon"
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="Next3"
android:id="@+id/Button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image3"
android:src="@drawable/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>


4个动画文件:

push_left_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_left_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>
push_right_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_right_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>


AndroidFlingSlide.rar (48 KB)

下载次数: 492
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐