Android中有沒有類似onLeftSwipeListener和onRightSwipeListener的東西?我想切換視圖向前和向後滑動手指。我在裏面使用了一個FrameLayout和一個ImageView。在Android中刷卡聽衆
回答
它叫做GestureDetector和SimpleOnGestureListener它有onFling()
。其所謂一扔,而不是在這方面:)
下面是有關實現手勢檢測相關quetion刷卡:
Fling gesture detection on grid layout
你也可以使用一個ViewFlipper有一些動畫的視圖之間切換,它看起來好,這裏是爲使上viewflipper左/右輕掃的範例:
http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html
我已經刷卡我的一個小例子ñ安卓我想與你分享代碼。
赤這個。
// ///佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Soy A"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Soy B"
android:textColor="#BBBFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Soy C"
android:textColor="#FFBBFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Soy D"
android:textColor="#FFFFAF" />
</ViewFlipper>
</LinearLayout>
///活動///
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;
public class SwipeActivity extends Activity {
private Animation mInFromRight;
private Animation mOutToLeft;
private Animation mInFromLeft;
private Animation mOutToRight;
private ViewFlipper mViewFlipper;
/** 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.view_flipper);
mViewFlipper.setDisplayedChild(0);
initAnimations();
}
private void initAnimations() {
mInFromRight = 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);
mInFromRight.setDuration(500);
AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
mInFromRight.setInterpolator(accelerateInterpolator);
mInFromLeft = 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);
mInFromLeft.setDuration(500);
mInFromLeft.setInterpolator(accelerateInterpolator);
mOutToRight = 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);
mOutToRight.setDuration(500);
mOutToRight.setInterpolator(accelerateInterpolator);
mOutToLeft = 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);
mOutToLeft.setDuration(500);
mOutToLeft.setInterpolator(accelerateInterpolator);
final GestureDetector gestureDetector;
gestureDetector = new GestureDetector(new MyGestureDetector());
mViewFlipper.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
} else {
return true;
}
}
});
}
private class MyGestureDetector extends SimpleOnGestureListener {
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;
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
System.out.println(" in onFling() :: ");
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(mInFromRight);
mViewFlipper.setOutAnimation(mOutToLeft);
mViewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(mInFromLeft);
mViewFlipper.setOutAnimation(mOutToRight);
mViewFlipper.showPrevious();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}
你的onFling方法幫助了我,謝謝! – techieWings 2013-04-06 12:04:44
好的,這是想法,幫助他人。 – shontauro 2013-04-08 22:06:16
yourView.setOnTouchListner(onThumbTouch);
OnTouchListener onThumbTouch = new OnTouchListener() {
float previouspoint = 0 ;
float startPoint=0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()) {
case R.id.tvDetailsalaujairiyat: // Give your R.id.sample ...
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
startPoint=event.getX();
System.out.println("Action down,..."+event.getX());
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_CANCEL:
previouspoint=event.getX();
if(previouspoint > startPoint){
//Right side swipe
}else{
// Left side swipe
}
break;
}
break;
}
return true;
}
};
最佳答案...客觀! – 2014-01-11 01:04:55
- 1. android sim卡聽衆
- 2. 聽衆sd卡移除
- 3. 如何把聽衆放在android中的選項卡上
- 4. Onclick Android的聽衆
- 5. Android Shift鍵聽衆
- 6. 普通春卡夫卡聽衆
- 7. 春天卡夫卡消費者 - 並行聽衆與非併發聽衆雲
- 8. Spinner聽衆未在Android中執行
- 9. 傾聽聽衆
- 10. Android聽衆序列化
- 11. Android:聽衆模式內onCreate()
- 12. android的關鍵聽衆
- 13. 耳機插孔聽衆Android
- 14. Android MediaPlayer - 聽衆 - dismissDialog幫助
- 15. Android中不同類型的聽衆
- 16. 在Android的按鈕點擊聽衆
- 17. 如何設置聽衆在android
- 18. 雙擊在Android中刷卡?
- 19. 在Tabhost中的Android刷卡
- 20. 聽衆可以在Chrome窗格中選擇事件被刷新
- 21. 多SeekBars聽衆
- 22. 靜態聽衆
- 23. Doctrine 2聽衆
- 24. HAML的聽衆?
- 25. Java onclick聽衆
- 26. Javascript onclick聽衆
- 27. 聽衆sharedpreferences
- 28. 處理聽衆
- 29. ANDROID刷卡UI
- 30. AS3監聽一類聽衆
哦,我明白了。但是,它能區分扔給左邊還是扔給右邊? – lomza 2011-05-12 09:33:26
你可以在這裏找到細節:http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onFling(android.view.MotionEvent,android.view.MotionEvent,float,float) – alopix 2011-05-12 09:37:15
和一個更多的問題。它是否足以使用意圖來切換視圖或者它是一種愚蠢的方式? – lomza 2011-05-12 10:07:29