重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如何Android项目中使用ViewFlipper与GestrueDetector实现滑屏效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
公司主营业务:网站设计、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出改则免费做网站回馈大家。
1.main.xml
<?xml version="1.0" encoding="utf-8"?>
注:layout1和layout2 布局很简单,就是有一个textview和一个button,就不在这里写出了。其中包含了两个特效xml文件,放在res/anim下
1.push_left_in
<?xml version="1.0" encoding="utf-8"?>
2 push_left_out
<?xml version="1.0" encoding="utf-8"?>
主类:
package com.wj.gesture; 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.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; public class GestureDetectorTest extends Activity implements OnClickListener, OnTouchListener,OnGestureListener,OnDoubleTapListener{ GestureDetector gestureDetector; private Button next = null,up = null; private ViewFlipper flipper = null; private static final int FLING_MIN_DISTANCE = 100; private static final int FLING_MIN_VELOCITY = 200; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initview(); setListener(); } private void initview(){ gestureDetector = new GestureDetector(this); next = (Button)this.findViewById(R.id.button1); up = (Button)this.findViewById(R.id.button2); next.setOnClickListener(this); up.setOnClickListener(this); flipper = (ViewFlipper)this.findViewById(R.id.viewFlipper1); flipper.setLongClickable(true); } private void setListener(){ flipper.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { //Toast.makeText(this, "ontouch", 1000).show(); return gestureDetector.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) { 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()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY) { flipper.setInAnimation(inFromRightAnimation()); flipper.setOutAnimation(outToLeftAnimation()); flipper.showNext(); }else if (e2.getX()-e1.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX) > FLING_MIN_VELOCITY) { flipper.setInAnimation(inFromLeftAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showPrevious(); } return false; } protected Animation inFromRightAnimation() { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(500); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } protected Animation outToLeftAnimation() { Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoLeft.setDuration(500); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; } protected Animation inFromLeftAnimation() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(500); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } protected Animation outToRightAnimation() { Animation outtoRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoRight.setDuration(500); outtoRight.setInterpolator(new AccelerateInterpolator()); return outtoRight; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTap(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onClick(View v) { if (v==next) { flipper.showNext(); }else{ flipper.showPrevious(); } } }
关于如何Android项目中使用ViewFlipper与GestrueDetector实现滑屏效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。