Xamarin Imageview Drag Sample

泄露秘密 提交于 2019-12-12 04:35:12

问题


(Xamarin with Visual Studio 2015 ) I want to implement a simple Activity inwith an Imageview, which can be moved/dragged with touch: This is what I have implemented, but the Image is flickering and moving slower. Can you give me an example how to implement this? Thanks for your help!

    private void TouchMeImageViewOnTouch(object sender, View.TouchEventArgs touchEventArgs)
    {
        View bild = (View)sender;
        RelativeLayout.LayoutParams layouti = (RelativeLayout.LayoutParams)bild.LayoutParameters;
        switch (touchEventArgs.Event.Action & MotionEventActions.Mask)
        {
            case MotionEventActions.Down:
                xDelta = touchEventArgs.Event.GetX()-layouti.LeftMargin;
                yDelta = touchEventArgs.Event.GetX() - layouti.LeftMargin;
                break;
            case MotionEventActions.Move:
                int wert = (int)touchEventArgs.Event.GetX();
                yvalue = touchEventArgs.Event.GetY()-yDelta;
                xvalue = touchEventArgs.Event.GetX()-xDelta;
                float xdpi = (int) Resources.DisplayMetrics.Density;
                layouti.LeftMargin = (int)xvalue;
                layouti.TopMargin = (int)yvalue;
                container.Invalidate();
                break;

            case MotionEventActions.Up:
                break;

            default:
                break;
        }

        xPositionText.Text = xvalue.ToString();
        yPositionText.Text = yvalue.ToString();
    }

回答1:


I have tried to implement a dragable imageview for testing. the drag is slow in the android emulator. But by testing it in the real device it works fine and move fast.

Try the following code sample:

public class MainActivity : Activity, IOnTouchListener
{
    Button dragAbleBt;
    ImageView imgV1;
    int screenWidth = 0;
    int screenHeight = 0;
    int lastX = 0, lastY = 0;
    public bool OnTouch(View v, MotionEvent e)
    {

        MotionEventActions ea = e.Action;
        switch (ea) {
            case MotionEventActions.Down:
                lastX = (int)e.RawX;
                lastY = (int)e.RawY;                
                break;
            case MotionEventActions.Move:
                int dx = (int)e.RawX - lastX;
                int dy = (int)e.RawY - lastY;
                int left = v.Left + dx;
                int right = v.Right + dx;
                int top = v.Top + dy;
                int bottom = v.Bottom + dy;
                if (left < 0)
                {
                    left = 0;
                    right = left + v.Width;
                }
                if (right > screenWidth)
                {
                    right = screenWidth;
                    left = right - v.Width;
                }
                if (top < 0)
                {
                    top = 0;
                    bottom = top + v.Height;
                }
                if (bottom > screenHeight)
                {
                    bottom = screenHeight;
                    top = bottom - v.Height;
                }
                v.Layout(left, top, right, bottom);
                lastX = (int) e.RawX;
                lastY = (int) e.RawY;
                v.PostInvalidate();
                break;
            case MotionEventActions.Up:                  
                break;                   
        }
        if (v.Id == Resource.Id.imageView1)
        {
            return true;
        }
        return false;
    }
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView (Resource.Layout.Main);
        //DisplayMetrics dm = Resources.DisplayMetrics;
        //screenWidth = dm.WidthPixels;
        //screenHeight = dm.HeightPixels;
        dragAbleBt = FindViewById<Button>(Resource.Id.button1);
        imgV1 = FindViewById<ImageView>(Resource.Id.imageView1);
        dragAbleBt.SetOnTouchListener(this);
        imgV1.SetOnTouchListener(this);
    }

    public override void OnWindowFocusChanged(bool hasFocus)
    {
        base.OnWindowFocusChanged(hasFocus);
        if (hasFocus)
        {
            Rect outRect = new Rect();
            this.Window.FindViewById(Window.IdAndroidContent).GetDrawingRect(outRect);
            screenWidth = outRect.Width();
            screenHeight = outRect.Height();
        }
    }
}

Please refer the source code to the github



来源:https://stackoverflow.com/questions/42489687/xamarin-imageview-drag-sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!