亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android手勢(shì)操作示例(上/下/左/右的判斷)

 更新時(shí)間:2016年06月14日 10:16:00   作者:ztp800201  
這篇文章主要介紹了Android手勢(shì)操作方法,包含了針對(duì)上、下、左、右等方向的判斷,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android手勢(shì)操作方法。分享給大家供大家參考,具體如下:

Android中提供了判斷手勢(shì)的接口,所以我們可以根據(jù)提供的API來實(shí)現(xiàn)各種各樣的手勢(shì)功能來提高手機(jī)應(yīng)用的用戶體驗(yàn)。

下面是我寫的一段小Demo:

GestureActivity.Java

public class GestureActivity extends Activity {
  private GestureDetector gestureDetector;
  private Screen screen;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    gestureDetector = new GestureDetector(this,onGestureListener);
    //得到屏幕的大小
    screen = GestureUtils.getScreenPix(this);
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
  }
  GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
      float x = e2.getX() - e1.getX();
      float y = e2.getY() - e1.getY();
      //限制必須得劃過屏幕的1/3才能算劃過
      float x_limit = screen.widthPixels / 3;
      float y_limit = screen.heightPixels / 3;
      float x_abs = Math.abs(x);
      float y_abs = Math.abs(y);
      if(x_abs >= y_abs){
        //gesture left or right
        if(x > x_limit || x < -x_limit){
          if(x>0){
            //right
            show("right");
          }else if(x
            //left
            show("left");
          }
        }
      }else{
        //gesture down or up
        if(y > y_limit || y < -y_limit){
          if(y>0){
            //down
            show("down");
          }else if(y
            //up
            show("up");
          }
        }
      }
      return true;
    }
  };
  private void show(String value){
    Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
  }
}

GestureUtils.java

public class GestureUtils {
  //獲取屏幕的大小
  public static Screen getScreenPix(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    return new Screen(dm.widthPixels,dm.heightPixels);
  }
  public static class Screen{
    public int widthPixels;
    public int heightPixels;
    public Screen(){
    }
    public Screen(int widthPixels,int heightPixels){
      this.widthPixels=widthPixels;
      this.heightPixels=heightPixels;
    }
    @Override
    public String toString() {
      return "("+widthPixels+","+heightPixels+")";
    }
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論