Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Xamarin Imageview拖动示例_Android_Xamarin_Imageview_Drag - Fatal编程技术网

Android Xamarin Imageview拖动示例

Android Xamarin Imageview拖动示例,android,xamarin,imageview,drag,Android,Xamarin,Imageview,Drag,(Xamarin与Visual Studio 2015合作)我想用Imageview实现一个简单的活动,它可以通过触摸移动/拖动:这是我已经实现的,但图像闪烁且移动速度较慢。你能给我一个如何实现这一点的例子吗? 谢谢你的帮助 private void TouchMeImageViewOnTouch(object sender, View.TouchEventArgs touchEventArgs) { View bild = (View)sender;

(Xamarin与Visual Studio 2015合作)我想用Imageview实现一个简单的活动,它可以通过触摸移动/拖动:这是我已经实现的,但图像闪烁且移动速度较慢。你能给我一个如何实现这一点的例子吗? 谢谢你的帮助

    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 emulator中,拖动速度很慢。但通过在真实设备上测试,它工作良好,运行迅速

请尝试以下代码示例:

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();
        }
    }
}
public类main活动:活动,IOnTouchListener
{
按钮式牵引架;
ImageView imgV1;
int屏幕宽度=0;
int屏幕高度=0;
int lastX=0,lastY=0;
公共bool OnTouch(视图v,运动事件e)
{
MotionEventActions ea=e.动作;
开关(ea){
案例MotionEventActions.Down:
lastX=(int)e.RawX;
拉斯蒂=(int)e.RawY;
打破
case MotionEventActions。移动:
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(左<0)
{
左=0;
右=左+v.宽度;
}
如果(右>屏幕宽度)
{
右=屏幕宽度;
左=右-v.宽度;
}
如果(顶部<0)
{
top=0;
底部=顶部+v.高度;
}
如果(底部>屏幕高度)
{
底部=屏幕高度;
顶部=底部-v.高度;
}
v、 布局(左、上、右、下);
lastX=(int)e.RawX;
拉斯蒂=(int)e.RawY;
v、 后验证();
打破
case MotionEventActions.Up:
打破
}
if(v.Id==Resource.Id.imageView1)
{
返回true;
}
返回false;
}
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//DisplayMetrics dm=Resources.DisplayMetrics;
//屏幕宽度=dm.WidthPixels;
//屏幕高度=dm.HeightPixels;
dragAbleBt=findviewbyd(Resource.Id.button1);
imgV1=findviewbyd(Resource.Id.imageView1);
dragAbleBt.SetOnTouchListener(此);
imgV1.SetOnTouchListener(此);
}
WindowFocusChanged上的公共覆盖无效(布尔hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
如果(hasFocus)
{
Rect outRect=新的Rect();
this.Window.findviewbyd(Window.IdAndroidContent.GetDrawingRect(outRect);
screenWidth=outRect.Width();
屏幕高度=outRect.Height();
}
}
}

请将源代码参考

我尝试实现一个可拖动的imageview进行测试。在android emulator中,拖动速度很慢。但通过在真实设备上测试,它工作良好,运行迅速

请尝试以下代码示例:

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();
        }
    }
}
public类main活动:活动,IOnTouchListener
{
按钮式牵引架;
ImageView imgV1;
int屏幕宽度=0;
int屏幕高度=0;
int lastX=0,lastY=0;
公共bool OnTouch(视图v,运动事件e)
{
MotionEventActions ea=e.动作;
开关(ea){
案例MotionEventActions.Down:
lastX=(int)e.RawX;
拉斯蒂=(int)e.RawY;
打破
case MotionEventActions。移动:
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(左<0)
{
左=0;
右=左+v.宽度;
}
如果(右>屏幕宽度)
{
右=屏幕宽度;
左=右-v.宽度;
}
如果(顶部<0)
{
top=0;
底部=顶部+v.高度;
}
如果(底部>屏幕高度)
{
底部=屏幕高度;
顶部=底部-v.高度;
}
v、 布局(左、上、右、下);
lastX=(int)e.RawX;
拉斯蒂=(int)e.RawY;
v、 后验证();
打破
case MotionEventActions.Up:
打破
}
if(v.Id==Resource.Id.imageView1)
{
返回true;
}
返回false;
}
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//DisplayMetrics dm=Resources.DisplayMetrics;
//屏幕宽度=dm.WidthPixels;
//屏幕高度=dm.HeightPixels;
dragAbleBt=findviewbyd(Resource.Id.button1);
imgV1=findviewbyd(Resource.Id.imageView1);
dragAbleBt.SetOnTouchListener(此);
imgV1.SetOnTouchListener(此);
}
WindowFocusChanged上的公共覆盖无效(布尔hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
如果(hasFocus)
{
Rect outRect=新的Rect();
this.Window.findviewbyd(Window.IdAndroidContent.GetDrawingRect(outRect);
screenWidth=outRect.Width()