- Android - Intents/Filters
- Android - Fragments
- Android - Content Providers
- Android - Broadcast Receivers
- Android - Services
- Android - Activities
- Android - Resources
- Android - Hello World Example
- Android - Application Components
- Android - Architecture
- Android - Environment Setup
- Android - Overview
- Android - Home
Android - User Interface
- Android - Custom Components
- Android - Styles and Themes
- Android - Event Handling
- Android - UI Controls
- Android - UI Layouts
Android Advanced Concepts
- Publishing Android Application
- Android - Phone Calls
- Android - Sending SMS
- Android - Sending Email
- Location Based Services
- Android - Notifications
- Android - Drag and Drop
Android Useful Examples
- Android - XML Parsers
- Android - Widgets
- Android - Wi-Fi
- Android - WebView Layout
- Android - UI Testing
- Android - UI Patterns
- Android - UI Design
- Android - Twitter Integration
- Android - TextureView
- Android - Text to Speech
- Android - Testing
- Android - Support Library
- Android - SQLite Database
- Android - Spelling Checker
- Android - SIP Protocol
- Android - Shared Preferences
- Android - Session Management
- Android - Sensors
- Android - SDK Manager
- Android - Screen Cast
- Android - RSS Reader
- Android - RenderScript
- Android - Push Notification
- Android - ProgressBar
- Android - Progress Circle
- Android - PHP/MySQL
- Android - NFC Guide
- Android - Network Connection
- Android - Navigation
- Android - Multitouch
- Android - MediaPlayer
- Android - Login Screen
- Android - Localization
- Android - Loading Spinner
- Android - Linkedin Integration
- Android - JSON Parser
- Android - JetPlayer
- Android - Internal Storage
- Android - ImageSwitcher
- Android - Image Effects
- Android - Google Maps
- Android - Gestures
- Android - Facebook Integration
- Android - Emulator
- Android - Developer Tools
- Android - Data Backup
- Android - Custom Fonts
- Android - Clipboard
- Android - Camera
- Android - Bluetooth
- Android - Best Practices
- Android - Auto Complete
- Android - AudioManager
- Android - Audio Capture
- Android - Animations
- Android - Alert Dialoges
Android Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Android - Gestures
Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and fpnch. These are all known as gestures.
Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not. To use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a pstener and override some methods. Its syntax is given below −
GestureDetector myG; myG = new GestureDetector(this,new Gesture()); class Gesture extends GestureDetector.SimpleOnGestureListener{ pubpc boolean onSingleTapUp(MotionEvent ev) { } pubpc void onLongPress(MotionEvent ev) { } pubpc boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { } pubpc boolean onFpng(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { } }
Handpng Pinch Gesture
Android provides ScaleGestureDetector class to handle gestures pke pinch e.t.c. In order to use it, you need to instantiate an object of this class. Its syntax is as follow −
ScaleGestureDetector SGD; SGD = new ScaleGestureDetector(this,new ScaleListener());
The first parameter is the context and the second parameter is the event pstener. We have to define the event pstener and override a function OnTouchEvent to make it working. Its syntax is given below −
pubpc boolean onTouchEvent(MotionEvent ev) { SGD.onTouchEvent(ev); return true; } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override pubpc boolean onScale(ScaleGestureDetector detector) { float scale = detector.getScaleFactor(); return true; } }
Apart from the pinch gestures , there are other methods available that notify more about touch events. They are psted below −
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 milpseconds between the previous accepted scapng event and the current scapng 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. |
Example
Here is an example demonstrating the use of ScaleGestureDetector class. It creates a basic apppcation that allows you to zoom in and out through pinch.
To experiment with this example , you can run this on an actual device or in an emulator with touch screen enabled.
Steps | Description |
---|---|
1 | You will use Android studio to create an Android apppcation under a package com.example.sairamkrishna.myapppcation. |
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 apppcation and choose a running android device and install the apppcation on it and verify the results |
Following is the content of the modified main activity file src/MainActivity.java.
package com.example.sairamkrishna.myapppcation; import android.app.Activity; import android.graphics.Matrix; import android.os.Bundle; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; pubpc 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()); } pubpc boolean onTouchEvent(MotionEvent ev) { SGD.onTouchEvent(ev); return true; } private class ScaleListener extends ScaleGestureDetector. SimpleOnScaleGestureListener { @Override pubpc 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; } } }
Following is the modified content of the xml res/layout/activity_main.xml.
Here abc indicates the logo of tutorialspoint
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:text="Gestures Example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_apgnParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:scaleType="matrix" android:layout_below="@+id/textView" android:layout_apgnParentLeft="true" android:layout_apgnParentStart="true" android:layout_apgnParentBottom="true" android:layout_apgnParentRight="true" android:layout_apgnParentEnd="true" /> </RelativeLayout>
Following is the content of the res/values/string.xml.
<resources> <string name="app_name>My Apppcation</string> </resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapppcation" > <apppcation android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapppcationMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </apppcation> </manifest>
Let s try to run your apppcation. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project s activity files and cpck Run icon from the toolbar.The sample output should be pke this −
Now just place two fingers over android screen , and separate them a part and you will see that the android image is zooming. It is shown in the image below −
Now again place two fingers over android screen, and try to close them and you will see that the android image is now shrinking. It is shown in the image below −
Advertisements