Download Code

Xamarin Image Picker Gesture Android

In this tutorial you will learn how to pick an image from android phone gallery.Apply zoom in/out gestures on that image using xamarin image picker gesture android app.Let’s talk breifly about our concerning classes.

Intent

An intent is an abstract description of an operation to be performed.In this topic you will ACTION_PICK constant of an intent.

MediaStore

The Media provider contains meta data for all available media on both internal and external storage devices.

Cursor

This interface provides random read-write access to the result set returned by a database query.

Gestures

There are different types of Gestures like Multi Touch,Tracking Movement etc.But you will use Touch Gesture.

A “touch gesture” occurs when a user places one or more fingers on the touch screen.Your application interprets that pattern of touches as a particular gesture.

Let’s start create new android application project.Go to Main.axml and add ImageView,Button.

But one thing you should keep in mind that your layout should RelativeLayout.Why,because it expand according to the gesture.And also set ImageView scaleType to Matrix.

Main.axml

xamarin image picker,xamarin image picker gesture,xamarin image picker gesture android,image picker gesture android,

Now go to MainActivity.cs and do the following steps.

  • Create variables and refer UI Components(ImageView,Button).
  • Apply click event on Button.
  • OnButton click start intent to pick Image from gallery with OnActivityResult().
  • Create a class (Zoomer) and extend with ScaleGestureDetector.
  • (Zoomer class will rescale your image or any other components).

MainActivity.cs

namespace XamarinPickerGesture
{
	[Activity(Label = "XamarinPickerGesture", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, View.IOnClickListener
	{

		Button pickImageBtn;
		ImageView pickedImage;
		Bitmap bitImage;
		ScaleGestureDetector detector;
		private Matrix matrix = new Matrix();
		private float scale = 1f;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();

		}
		public void initialize()
		{
			pickedImage = (ImageView)FindViewById(Resource.Id.image);
			pickImageBtn = (Button)FindViewById(Resource.Id.pickImage);
			pickImageBtn.SetOnClickListener(this);
			detector = new ScaleGestureDetector(this, new Zoomer(this));
		}

		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.pickImage:
					//image from gallery intent
					Intent p = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
					StartActivityForResult(p, 1);
					break;
			}
		}


		protected override void OnActivityResult(int requestCode,Result resultCode, Intent data)
		{
			
			if ((requestCode == 1) && (resultCode == Result.Ok) && (data!=null)) {
				Android.Net.Uri selectedImage = data.Data;

				String[] filePathColumn = { MediaStore.Images.Media.InterfaceConsts.Data };

				ICursor cursor = ContentResolver.Query(selectedImage,
						filePathColumn, null, null, null);
				cursor.MoveToFirst();

				int columnIndex = cursor.GetColumnIndex(filePathColumn[0]);
				String picturePath = cursor.GetString(columnIndex);
				cursor.Close();

				pickedImage.SetImageURI(selectedImage);

			}

		}

		public override  bool OnTouchEvent(MotionEvent ev)
		{
			detector.OnTouchEvent(ev);
			return true;
		}

		private class Zoomer : ScaleGestureDetector.SimpleOnScaleGestureListener
		{
			MainActivity mainActivity;

			public Zoomer(MainActivity mainActivity)
			{
				this.mainActivity = mainActivity;
			}
			public override bool OnScale(ScaleGestureDetector detector)
			{
				// TODO Auto-generated method stub
				mainActivity.scale *= detector.ScaleFactor;
				mainActivity.scale = Math.Max(0.1f, Math.Min(mainActivity.scale, 5.0f));

				mainActivity.matrix.SetScale(mainActivity.scale, mainActivity.scale);
				mainActivity.pickedImage.ImageMatrix = mainActivity.matrix;
				return true;
			}


		}
	}
}

Go to Android Manifest.xml file and add permission EXTERNAL_STORAGE.

Run your xamarin image picker gesture android application project.

xamarin image picker,xamarin image picker gesture,xamarin image picker gesture android,image picker gesture android,

That’s it for xamarin image picker gesture android tutorial.

For Java example visit AndroidCodec.

Thanks.