📜  Android-手势

📅  最后修改于: 2021-01-05 05:14:56             🧑  作者: Mango


Android提供了特殊类型的触摸屏事件,例如捏,双击,滚动,长按和退格。这些都称为手势。

Android提供了GestureDetector类来接收运动事件,并告诉我们这些事件是否与手势相对应。要使用它,您需要创建一个GestureDetector对象,然后使用GestureDetector.SimpleOnGestureListener扩展另一个类以充当侦听器并重写某些方法。其语法如下-

GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
   
class Gesture extends GestureDetector.SimpleOnGestureListener{
   public boolean onSingleTapUp(MotionEvent ev) {
   }
   
   public void onLongPress(MotionEvent ev) {
   }
   
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   }
   
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
   }
}

处理捏手势

Android提供了ScaleGestureDetector类来处理诸如捏等手势,为了使用它,您需要实例化此类的对象。它的语法如下-

ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());

第一个参数是上下文,第二个参数是事件侦听器。我们必须定义事件侦听器并重写函数OnTouchEvent使其起作用。其语法如下-

public boolean onTouchEvent(MotionEvent ev) {
   SGD.onTouchEvent(ev);
   return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   @Override
   public boolean onScale(ScaleGestureDetector detector) {
      float scale = detector.getScaleFactor();
      return true;
   }
}

除了捏合手势外,还有其他方法可以通知更多有关触摸事件的信息。它们在下面列出-

Sr.No Method & description
1

getEventTime()

This method get the event time of the current event being processed..

2

getFocusX()

This method get the X coordinate of the current gesture’s focal point.

3

getFocusY()

This method get the Y coordinate of the current gesture’s focal point.

4

getTimeDelta()

This method return the time difference in milliseconds between the previous accepted scaling event and the current scaling event.

5

isInProgress()

This method returns true if a scale gesture is in progress..

6

onTouchEvent(MotionEvent event)

This method accepts MotionEvents and dispatches events when appropriate.

这是一个演示ScaleGestureDetector类的用法的示例。它创建了一个基本应用程序,可让您通过捏放大和缩小。

要尝试使用此示例,您可以在实际设备上或在启用了触摸屏的仿真器中运行此示例。

Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add necessary code.
3 Modify the res/layout/activity_main to add respective XML components
4 Run the application and choose a running android device and install the application on it and verify the results

以下是修改后的主要活动文件src / MainActivity.java的内容

package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      iv=(ImageView)findViewById(R.id.imageView);
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   }

   public boolean onTouchEvent(MotionEvent ev) {
      SGD.onTouchEvent(ev);
      return true;
   }

   private class ScaleListener extends ScaleGestureDetector.
      SimpleOnScaleGestureListener {
      
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));
         matrix.setScale(scale, scale);
         iv.setImageMatrix(matrix);
         return true;
      }
   }
}

以下是xml res / layout / activity_main.xml的修改内容。

此处abc表示tutorialspoint的徽标


   
   
      
   
      
   
      

以下是res / values /字符串.xml的内容


   

以下是AndroidManifest.xml文件的内容。




   
      
      
         
         
            
            
         
         
      
   
   

让我们尝试运行您的应用程序。我假设您已将实际的Android Mobile设备与计算机连接。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击运行Eclipse运行图标工具栏上的图标。示例输出应如下所示:

Android手势教程

现在,只需将两根手指放在android屏幕上,并将它们分开一部分,您将看到android图像正在缩放。如下面的图片所示-

Android手势教程

现在再次将两根手指放在android屏幕上,并尝试关闭它们,您将看到android图像正在缩小。如下面的图片所示-

Android手势教程