OnGestureListener小结

一个activity中new一个GestureDetector之后,里面会实现一下的方法:

onSingleTapUp

onShowPress

onScroll

onLongPress

onFling

onDown

说一下这些方法都是由那些MotionEvent触发的

今天的项目用到了onSingleTapUp和onShowPress和onLongPress和onFling;

1、onSingleTapUp、onSingleTapConfirmed

用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发

这个事件执行的顺序是onDown-》onShowPress-》onSingleTapUp

区别:

点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed 
点击一下稍微慢点的(不滑 动)Touchup:on
Down->onShowPress->onSingleTapUp->onSingleTapConfirmed

 

2、onShowPress

用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发

它与onDown()的区别,强调的是没有松开或者拖动的状态

 

3、onDown

onDown也是由一个MotionEventACTION_DOWN触发的,但是他没有任何限制,也就是说当用户点击的时候,首先MotionEventACTION_DOWN,onDown就会执行,如果在按下的瞬间没有松开或者是拖动的时候onShowPress就会执行,如果是按下的时间超过瞬间(这块我也不太清楚瞬间的时间差是多少,一般情况下都会执行onShowPress),拖动了,就不执行onShowPress

 

4、onLongPress 

用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发

这个事件执行的顺序是onDown-》onShowPress-》onLongPress

 

5、onFling

用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE,1个ACTION_UP触发

 

6、onScroll

Touch了 滑动时触发。 

另外需要说的一点就是:

如果你是在一个大的view上装载了一个一个的小view,如果你想让大的view有界面动态效果(比如左右向下滑动),那么你必须将这些小的view加入setOnTouchListener,然后你可以在OnTouchListener方法中加入如下方法将你的MotionEvent事件传到gestureDetector中。如下:

switch (event.getAction()) {
 
                      case MotionEvent.ACTION_MOVE: {
 
                          gestureDetector.onTouchEvent(event);
 
                          break;
 
                      }// inner case MOVE
 
                      case MotionEvent.ACTION_UP: {
 
                          v.setPressed(false);
 
                          Log.i("cat", "CCCCCCCCCCCCCCCC");
 
                          gestureDetector.onTouchEvent(event);
 
                          // TODO animation
 
                          return true;
 
                      }// inner case UP
 
                      case MotionEvent.ACTION_DOWN: {
 
                          v.setPressed(true);
 
                          gestureDetector.onTouchEvent(event);
 
                          break;
 
                      }// inner case UP
 
                  }// inner switch


6、滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper

   通过GestureDetector、OnGestureListener实现滑屏事件。ViewFlipper是继承至FrameLayout的,所以它是一个Layout里面可以放置多个View。示例中第一页仅放了一个按钮BUTTON,向下滑屏时,每页都只放了一个TEXTVIEW。

页面:

<?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/ViewFlipper01"
android:layout_width="fill_parent"android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>

 

代码:
public class Flip extends Activityimplements OnGestureListener {

private GestureDetector detector;
private ViewFlipper flipper;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

flipper = (ViewFlipper)this.findViewById(R.id.ViewFlipper01);

flipper.addView(addButtonByText("按钮"),newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

detector = new GestureDetector(this);
}

public View addButtonByText(String text){
Button btn = new Button(this);
btn.setText(text);
return btn;
}
public View addTextByText(String text){
TextView tv = new TextView(this);
tv.setText(text);
tv.setGravity(1);
return tv;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("Fling", "Activity onTouchEvent!");
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,
float velocityY) {
// TODO Auto-generated method stub
Log.i("Fling", "Fling Happened!");
if (e1.getX() - e2.getX() > 120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
this.flipper.addView(addTextByText("文本框"),newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out));
this.flipper.showPrevious();
return true;
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, floatdistanceX,
float distanceY) {
// 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;
}
}


动画源文件: (在res目录下,创建一个anim文件夹,把下面的文件都放在这里)

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.1"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.1"
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.1"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.1"
android:duration="500" />
</set>  


你可能感兴趣的:(android,String,layout,action,button,encoding)