Drag and driop image xamarin android

Shay Wilner 1,746 Reputation points
2021-10-29T12:54:40+00:00

Hello

I have an imageview and i would like to drag and drop

 <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:id="@+id/queen1"
        android:layout_below="@+id/boardgame"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"/>

in the mainactivity.cs

public class MainActivity : Activity
    {

        ImageView queen1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

             queen1 = FindViewById<ImageView>(Resource.Id.queen1);   
                Loadimage();

            };



private async void Loadimage()
        {
            var stream = Assets.Open("queens.png");
            var bitmap = await BitmapFactory.DecodeStreamAsync(stream);
            queen1.SetImageBitmap(bitmap);
            stream.Close();

        }

        }

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,376 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,471 Reputation points Microsoft Vendor
    2021-11-01T07:50:27.42+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I have an imageview and i would like to drag and drop

    If you want to drag or drop image in the xamarin.android, we can achieve it by ImageView's SetOnTouchListener. Then detect MotionEventActions.Move event, we can set the X, Y position in the RelativeLayout.

    Here is demo code.

       protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
         
                   //Get the screensize  
                   DisplayMetrics displaymetrics = new DisplayMetrics();  
                   WindowManager.DefaultDisplay.GetMetrics(displaymetrics);  
                   int screenWidth = displaymetrics.WidthPixels;  
                   int screenHeight = displaymetrics.HeightPixels;  
         
                   SetContentView(Resource.Layout.activity_main);  
         
                   ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);  
                   imageView1.SetOnTouchListener(new MyTouchListener(imageView1, screenWidth, screenHeight));  
               }  
    

    Here is MyTouchListener.cs.

       internal class MyTouchListener : Java.Lang.Object, View.IOnTouchListener  
           {  
               public MyTouchListener(ImageView imageView1, int screenWidth, int screenHeight)  
               {  
                   ImageView1 = imageView1;  
                   ScreenWidth = screenWidth;  
                   ScreenHeight = screenHeight;  
               }  
         
               public ImageView ImageView1 { get; }  
               public int ScreenWidth { get; }  
               public int ScreenHeight { get; }  
         
               public bool OnTouch(View v, MotionEvent e)  
               {  
                  var layoutParams1 = (RelativeLayout.LayoutParams)ImageView1.LayoutParameters;  
                   // throw new System.NotImplementedException();  
         
                   switch (e.ActionMasked)  
                 {  
                   case MotionEventActions.Down:  
                       break;  
                   case MotionEventActions.Move:  
         
                       int x_cord = (int) e.RawX;  
                       int y_cord = (int)e.RawY;  
                       if (x_cord > ScreenWidth) {  
                           x_cord = ScreenWidth;  
                       }  
                       if (y_cord > ScreenHeight) {  
                           y_cord = ScreenHeight;  
                       }  
                       //You can adjust the Image postion when you are draging.  
                       layoutParams1.LeftMargin = x_cord ;  
                       layoutParams1.TopMargin = y_cord-55 ;  
                      ImageView1.LayoutParameters=layoutParams1;  
                       break;  
         
                   default:  
                       break;  
                  }  
                   return true;  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.