編輯:關於Android編程
import android.content.Context; import android.util.DisplayMetrics; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; public class GestureHelper implements OnGestureListener { private GestureDetector gesture_detector; private int screen_width; private OnFlingListener listener_onfling; public static abstract class OnFlingListener { public abstract void OnFlingLeft(); public abstract void OnFlingRight(); } public GestureHelper(Context context) { DisplayMetrics dm = context.getResources().getDisplayMetrics(); screen_width = dm.widthPixels; gesture_detector = new GestureDetector(context, this); } public void setOnFlingListener(OnFlingListener listener) { listener_onfling = listener; } public boolean onTouchEvent(MotionEvent event) { return gesture_detector.onTouchEvent(event); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 觸發條件 : // X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒 final int FLING_MIN_DISTANCE = (int) (screen_width / 3.0f), FLING_MIN_VELOCITY = 200; if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { listener_onfling.OnFlingLeft(); } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { listener_onfling.OnFlingRight(); } return true; } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }
使用方法如下:
public class TestActivity extends Activity { private GestureHelper gh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); gh = new GestureHelper(this); gh.setOnFlingListener(new OnFlingListener() { @Override public void OnFlingLeft() { //向左滑動 } @Override public void OnFlingRight() { //向右滑動 } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gh.onTouchEvent(event); } }
諸如向上向下滑等因為需求不多所以沒有加上去,有需求的朋友可以自己加上。
最近有很多朋友問我,android6.0下載文件怎樣設置文件的保存路徑。今天就把android6.0下載文件路徑設置總結一下。在android 6.0以前,你可以只關注外
內存洩露排查手記 Time:2013.09.02 Author:sodino 問題現象: 這裡內存洩露是指已實例化的對象長期被hold住且無法釋放或不能
AppWidgetProvider 用來在HOME頁面顯示插件實現步驟:1、為AppWidget提供一個元布局文件AppWigdetProvider_Provider.x
期待已久的MIUI 8終於上線啦!經過全新設計的MIUI 8靈感來源於變幻萬千的“萬花筒”,在色彩、交互動畫、系統字體等方面的大膽改