本文主要是介绍实现滑动切换效果:ViewFlipper、OnGestureListener,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例子中用到的三个主要控件:ViewFlipper、OnGestureListener、OnDoubleTapListener
GestureSlide_act.java文件
/*** 实现滑动切换效果:ViewFlipper、OnGestureListener* * * 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.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;import com.example.R;public class GestureSlide_Act extends Activity implements OnGestureListener, OnDoubleTapListener {private ViewFlipper mViewFlipper; private GestureDetector mGestureDetector; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.gestureslide_act);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) { return false; } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 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) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } public void onShowPress(MotionEvent e) { } public boolean onSingleTapUp(MotionEvent e) { return false; } //以下是OnDoubleTapListener需要实现的方法 public boolean onDoubleTap(MotionEvent e) { mViewFlipper.startFlipping(); //双击自动切换界面 Toast.makeText(GestureSlide_Act.this, "自动切换界面", Toast.LENGTH_SHORT).show();return true; } public boolean onDoubleTapEvent(MotionEvent e) { return false; } public boolean onSingleTapConfirmed(MotionEvent e) { if(mViewFlipper.isFlipping()){ //单击结束自动切换 mViewFlipper.stopFlipping(); Toast.makeText(GestureSlide_Act.this, "结束自动切换", Toast.LENGTH_SHORT).show();} return false; }
}
gestureslide_act.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ViewFlipperandroid:id="@+id/flipper"android:layout_width="fill_parent"android:layout_height="fill_parent"android:flipInterval="3000"android:inAnimation="@anim/push_left_in"android:outAnimation="@anim/push_left_out"android:persistentDrawingCache="animation" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/Button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Next1" ></Button><ImageViewandroid:id="@+id/image1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" ></ImageView></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/Button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Next2" ></Button><ImageViewandroid:id="@+id/image2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" ></ImageView></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/Button3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Next3" ></Button><ImageViewandroid:id="@+id/image3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" ></ImageView></LinearLayout></ViewFlipper></LinearLayout>
参考文章:http://gundumw100.iteye.com/blog/905033
这篇关于实现滑动切换效果:ViewFlipper、OnGestureListener的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!