Xamarin Imageview拖动样本 [英] Xamarin Imageview Drag Sample

查看:132
本文介绍了Xamarin Imageview拖动样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(带有Visual Studio 2015的Xamarin)我想实现一个带有Imageview的简单活动,可以通过触摸来移动/拖动它:这是我已经实现的,但是图像闪烁且移动速度较慢。您能举个例子如何实现吗?
感谢您的帮助!

(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();
    }


推荐答案

我试图实现可拖动的imageview进行测试。在Android模拟器中拖动速度很慢。但是,通过在真实设备中对其进行测试,它可以正常工作并快速移动。

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.

请尝试以下代码示例:

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();
        }
    }
}

请参考源代码以 github

这篇关于Xamarin Imageview拖动样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆