Exploring Gesture Detection With An Animation In Xamarin With Visual Studio 2015

Introduction

In this article, I will explain how we can recognize simple touch gestures in an an Android application with view animation.

Following are the steps on how to detect touch gesture and an animation in Xamarin.

Step 1

Create a new project for an Android Application
 
 

I have selected “Blank App(Android)” template for this article.

Step 2

Add simple GestureDetectorListener class in MainActivity

For touch detection, OnTouchEvent override method is used. In this article, I am doing simple gesture detection. For this, GestureDetector is a base class entry point to detect any gestures without having to deal directly with the touch events. Here, I am using IOnGestureListener event of GestureDetector class. There is also a ScaleGestureDetector class for scaling the specific events such as pinch to zoom. I will cover ScaleGestureDetector example in the next article.

In this article, I am detecting fling gesture to remove image from the screen. For that first we need to initialize GestureDetector class with Listener, pass touch event to the detector. GestureDetector class has six methods as follows: 

 MethodDescription

 OnDown( )

When the user is doing down motion tap on the screen, this event will be triggered.              

 OnFling( )

When the user flings motion tap on the screen, this event will be triggered.

 OnLongPress( )                              

When the user taps on the screen for long time, this event will be triggered.

 OnScroll( )

When the user scrolls tap on the screen, this event will be triggered.

 OnShowPress( )

When the user taps on the screen, this event will be triggered.

 OnSingleTap( )

When the user does single tap on the screen, this event will be triggered.

Step 3

Add an animation in MainActivity class

When the user flings on the screen OnFling() method call, I have used ScaleAnimation, TranslateAnimation and alphaanimation to remove animated images from the screen. The code snippet of “Main” Android XML and MainActivity class is shown below.

Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:minWidth="25px"  
  7.     android:minHeight="25px"  
  8.     android:background="@android:color/white">  
  9.     <ImageView  
  10.         android:src="@drawable/image1"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:id="@+id/imageView1" />  
  14. </LinearLayout>  

I have added one image in Resource->drawable folder and set an image in ImageView, as shown in the code above. The screenshot of the final layout is shown below.

 

MainActivit.cs

  1. public class MainActivity : Activity, GestureDetector.IOnGestureListener  
  2. {  
  3.         private GestureDetector myGestureDetection;  
  4.         private ImageView imageview;  
  5.         protected override void OnCreate(Bundle bundle)  
  6.         {  
  7.             base.OnCreate(bundle);  
  8.             SetContentView(Resource.Layout.Main);  
  9.             imageview = FindViewById<ImageView>(Resource.Id.imageView1);  
  10.             myGestureDetection = new GestureDetector(this);  
  11.         }  
  12.         public bool OnDown(MotionEvent e)  
  13.         {  
  14.             return false;  
  15.         }  
  16.         public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)  
  17.         {  
  18.                 var alpha = new AlphaAnimation(1.0f, 0.0f);  
  19.                 var translate = new TranslateAnimation(0.0f, (float)WindowManager.DefaultDisplay.Width, 0.0f, 0.0f);  
  20.                 var set = new AnimationSet(shareInterpolator: true);  
  21.                 var scaleAnimation = new ScaleAnimation(1, 0.7f, 1, 0.7f, Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);  
  22.                 set.AddAnimation(alpha);  
  23.                 set.AddAnimation(translate);  
  24.                 set.AddAnimation(scaleAnimation);  
  25.                 set.Duration = 2000;  
  26.                 scaleAnimation.RepeatCount = 1;  
  27.                 scaleAnimation.RepeatMode = RepeatMode.Reverse;  
  28.                 imageview.StartAnimation(set);  
  29.                 return true;  
  30.         }  
  31.         public void OnLongPress(MotionEvent e)  
  32.         {  
  33.             Toast.MakeText(this"This is OnLongPress Method", ToastLength.Short).Show();  
  34.         }  
  35.         public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)  
  36.         {  
  37.             return false;  
  38.         }  
  39.         public void OnShowPress(MotionEvent e)  
  40.         {  
  41.                     }  
  42.         public bool OnSingleTapUp(MotionEvent e)  
  43.         {  
  44.             return false;  
  45.         }  
  46.         public override bool OnTouchEvent(MotionEvent e)  
  47.         {  
  48.             myGestureDetection.OnTouchEvent(e);  
  49.             return false;  
  50.         }  
  51. }  

In the code given above, I have added three types of animation (alpha, scale and translate) in one property, using AnimationSet, duration to 2000 ms, repeate count to 1.

Output

Summary

In this article, we learned how to detect touch gestures with an animation in Xamarin with Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all