双击手机屏幕上的手势

时间:2014-03-19 10:20:52

标签: java android gesture-recognition

我想知道我们可以在android中识别SCREEN本身的双击动作。

我发现很多代码与双击手势有关,但没有找到SCREEN。如果API中有,请告诉我。

就像我们点击手机的屏幕一样,当我们双击手机屏幕时它会像Windows手机一样开始动作,它将进入模式稳定模式。

我正在使用此代码进行双击手势。

package a.b.c;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;



public class SampleActivity extends Activity implements OnDoubleTapListener,OnGestureListener
{
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if(e.getAction()==1)
        {
        Toast.makeText(getBaseContext(), "onDoubleTap", Toast.LENGTH_LONG).show();
        }
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        if(e.getAction()==1)
        {
        Toast.makeText(getBaseContext(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
        }
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        if(e.getAction()==1)
        {
        Toast.makeText(getBaseContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

将以下代码添加到您的活动中..

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity implements OnGestureListener, OnDoubleTapListener
{    
    private LinearLayout main;    
    private TextView viewA;

    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        gestureScanner = new GestureDetector(this);

        main = new LinearLayout(this);
        main.setBackgroundColor(Color.GRAY);
        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));

        viewA = new TextView(this);
        viewA.setBackgroundColor(Color.YELLOW);
        viewA.setTextColor(Color.BLACK);
        viewA.setTextSize(16);
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
        main.addView(viewA);

        setContentView(main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me)
    {
     return gestureScanner.onTouchEvent(me);
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
     viewA.setText("-" + "DOWN" + "-");
     return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
     viewA.setText("-" + "FLING" + "-");
     return true;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
     viewA.setText("-" + "LONG PRESS" + "-");
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
     viewA.setText("-" + "SCROLL" + "-");
     return true;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
     viewA.setText("-" + "SHOW PRESS" + "-");
    }    

    @Override
    public boolean onSingleTapUp(MotionEvent e)    
    {
     viewA.setText("-" + "SINGLE TAP UP" + "-");
     return true;
    }

 @Override
 public boolean onDoubleTap(MotionEvent e) {
  viewA.setText("-" + "DOUBLE TAP" + "-");
  return true;
 }

 @Override
 public boolean onDoubleTapEvent(MotionEvent e) {
  viewA.setText("-" + "DOUBLE TAP EVENT" + "-");
  return true;
 }

 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
  viewA.setText("-" + "SINGLE TAP CONFORMED" + "-");
  return true;
 }
}

我希望它会对你有所帮助:)。