问题描述
我在执行一个MyGestureDetector扩展一个SimpleOnGestureListener。我从借来的类:<一href="http://www.$c$cshogun.com/blog/tag/view-flipper/">http://www.$c$cshogun.com/blog/tag/view-flipper/以允许在一个ViewFlipper轻扫动作。我不能得到,如果要在模拟器上运行。有什么建议?
下面是所有的code:
在main.java
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.GestureDetector;
进口android.view.KeyEvent;
进口android.view.MotionEvent;进口android.view.View;
进口android.view.GestureDetector.SimpleOnGestureListener;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.webkit.WebView;
进口android.widget.ViewFlipper;
公共类主要的扩展活动{
私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
私人GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
民营动画slideLeftIn;
民营动画slideLeftOut;
民营动画slideRightIn;
民营动画slideRightOut;
私人ViewFlipper viewFlipper;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
// ---------------------------------设置滑动动画到下一个视图 - &GT;
viewFlipper =(ViewFlipper)findViewById(R.id.AC4313);
slideLeftIn = AnimationUtils.loadAnimation(这一点,R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(这一点,R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(这一点,R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(这一点,R.anim.slide_right_out);
gestureDetector =新GestureDetector(新MyGestureDetector());
gestureListener =新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
如果(gestureDetector.onTouchEvent(事件)){
返回true;
}
返回false;
}
};
}
类MyGestureDetector扩展SimpleOnGestureListener {
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
尝试 {
如果(Math.abs(e1.getY() - e2.getY())&GT; SWIPE_MAX_OFF_PATH)
返回false;
//从右向左轻扫
如果(e1.getX() - e2.getX()&GT; SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
}否则,如果(e2.getX() - e1.getX()&GT; SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.show previous();
}
}赶上(例外五){
//什么
}
返回false;
}
}
@Override // ------------------------------通过重写onTouch()方法捕捉手势事件: - &GT;
公共布尔的onTouchEvent(MotionEvent事件){
如果(gestureDetector.onTouchEvent(事件))
返回true;
其他
返回false;
}
在动画*的.xml的:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;设置的xmlns:机器人=http://schemas.android.com/apk/res/android&GT;
&LT;翻译机器人:fromXDelta =100%p的机器人:toXDelta =0机器人:时间=800/&GT;
&LT; /集&gt;
我认为这个问题是你不覆盖 OnDown
事件。添加该code片段到你的 MyGestureDetector
类,它应该是工作。
@覆盖
公共布尔onDown(MotionEvent E){
返回true;
}
在 onDown
事件返回false在默认情况下,每一个手势开始与 onDown
让你的 onFling
事件将不会触发。
·伊什特万
I am implementing a MyGestureDetector that extends a SimpleOnGestureListener. I borrowed the class from: http://www.codeshogun.com/blog/tag/view-flipper/ to allow a swipe action in a ViewFlipper. I can not get if to function on the emulator. Any suggestions?
Below is all of the code:
The main.java
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ViewFlipper;
public class main extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ---------------------------------Sets Swipe Animation to Next View -->
viewFlipper = (ViewFlipper)findViewById(R.id.AC4313);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override // ------------------------------ Catch the Gesture Event by Overriding onTouch() method: -->
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
The anim *.xml's:
<?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="800"/>
</set>
I think the problem is that you don't override the OnDown
event. Add this code snippet to your MyGestureDetector
class, and it should be work.
@Override
public boolean onDown(MotionEvent e) {
return true;
}
The onDown
event return false by default, and every gesture start with onDown
so your onFling
event will not fire.
Istvan
这篇关于MyGestureDetector延伸SimpleOnGestureListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!