如何避免离开屏幕?

时间:2018-06-12 02:56:17

标签: android xamarin imageview zoom

我说英语不好。

如果您不理解这个问题,我们深表歉意。

LinearLayout _Main_Contact_Layout;
FrameLayout _Main_Frame; 
ImageView _Main_Background;
private List<string> _DrawerList_Item;
private ListView _DrawerList;

float _X, _Y;
float _XX, _YY;
int offset = 0, duration = 100;
float scaleX = 1.0f, scaleY = 1.0f;
float maxZoomLimit = 2.6f, minZoomLimit = 1.0f;

public bool OnTouch(View v, MotionEvent e)
{
    switch (e.Action)
    {
        case MotionEventActions.Down:

            _X = e.GetX();
            _Y = e.GetY();

            float _Start_X = 760, _Start_Y = 160;
            float _End_X = 1150, _End_Y = 490;

            WindowManagerLayoutParams _Params = new WindowManagerLayoutParams();
            _Params.X = (int)e.RawX;
            _Params.Y = (int)e.RawY;

            LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
            View _Popup = inflater.Inflate(Resource.Layout.Custom_Popup, null);

            PopupWindow _Window = new PopupWindow(_Popup, WindowManagerLayoutParams.WrapContent, WindowManagerLayoutParams.WrapContent);
            _Window.SetBackgroundDrawable(new BitmapDrawable());
            _Window.OutsideTouchable = true;

            TextView _SetupX = (TextView)_Popup.FindViewById(Resource.Id.x_text);
            TextView _SetupY = (TextView)_Popup.FindViewById(Resource.Id.y_text);

            _SetupX.Text = "X 좌표 : " + _X.ToString("#.##");
            _SetupY.Text = "Y 좌표 : " + _Y.ToString("#.##");

            DrawerLayout _lstDrawer = (DrawerLayout)v.FindViewById(Resource.Id._DrawerLayout);


            if(_Start_X > _X | _Start_Y > _Y | _End_X < _X | _End_Y < _Y )
            {
                _Window.Focusable = true;
                _Window.ShowAsDropDown(_Popup, _Params.X, _Params.Y);
                _Window.ShowAtLocation(_Popup, GravityFlags.Left | GravityFlags.Top, 0, 0);
                _lstDrawer.CloseDrawer((int)GravityFlags.Left);
            }

            else if (_Start_X < _X & _Start_Y < _Y & _End_X > _X & _End_Y > _Y & !_lstDrawer.IsDrawerOpen((int)GravityFlags.Left))
            {
                _lstDrawer.OpenDrawer((int)GravityFlags.Left);
                _Window.ShowAsDropDown(_Popup, _Params.X, _Params.Y);
                _Window.ShowAtLocation(_Popup, GravityFlags.Left | GravityFlags.Top, 0, 0);
            }

            else if (_lstDrawer.IsDrawerOpen((int)GravityFlags.Left))
            {
                _lstDrawer.CloseDrawer((int)GravityFlags.Left);
            }

            break;


        case MotionEventActions.Move:

            _XX = e.GetX() - _X;
            _YY = e.GetY() - _Y;

            //_Main_Background.SetX(_Main_Background.GetX() + _XX);
            //_Main_Background.SetY(_Main_Background.GetY() + _YY);

            _X = e.GetX();
            _Y = e.GetY();


            if (scaleX > 1.0f && scaleY > 1.0f)
            {
                _MoveToX(_XX);
                _MoveToY(_YY);

                Console.WriteLine("_XX : " + _XX.ToString());
                Console.WriteLine("_YY : " + _YY.ToString());
            }

            break;
    }

    return true;
}

private void _MoveToX(float xX)
{
    float futureX = _Main_Background.GetX() + _XX;
    if (futureX > 0 | futureX < 500)
    {
        _Main_Background.SetX(futureX);
    }
}

private void _MoveToY(float yY)
{
    float futureY = _Main_Background.GetY() + _YY;
    if (futureY > 0 | futureY < 500)
    {
        _Main_Background.SetY(futureY);
    }
}



private void _Zoom_In(View v)
{
    if (scaleX < maxZoomLimit && scaleY < maxZoomLimit)
    {
        Animation animation = new ScaleAnimation(scaleX, (scaleX + 0.2f), scaleY, (scaleY + 0.2f), _X, _Y);
        scaleX += 0.2f;
        scaleY += 0.2f;
        animation.Interpolator = new DecelerateInterpolator();
        animation.Duration = duration;
        animation.StartOffset = offset;
        animation.FillAfter = true;
        v.StartAnimation(animation);
    }
}

private void _Zoom_Out(View v)
{
    if (scaleX > minZoomLimit && scaleY > minZoomLimit)
    {
        Animation animation = new ScaleAnimation(scaleX, (scaleX - 0.2f), scaleY, (scaleY - 0.2f), _X, _Y);
        scaleY -= 0.2f;
        scaleX -= 0.2f;
        animation.Interpolator = new DecelerateInterpolator();
        animation.Duration = duration;
        animation.StartOffset = offset;
        animation.FillAfter = true;
        v.StartAnimation(animation);
    }
}

我担心这个问题大约一个星期。

如何避免离开屏幕?

调整比例?我该如何触摸刻度?

  1. 使用按钮放大和缩小 - &GT;分辨率

  2. 使用按钮放大图像 - &GT;分辨率

  3. 如果在放大后移动它,请将图像移动到陌生的地方。 - &GT;分辨率

  4. 布局超出范围 - &GT;分辨率

  5. 仅在布局范围内移动(未解析)

1 个答案:

答案 0 :(得分:0)

修改if方法中的_MoveTo条件。 xy是视图左上角的坐标。计算imageview的宽度和高度,确保xy值小于0且不小于screenwidth的值减去imageview宽度。

例如:

    private void _MoveToX(float xX)
    {
        float futureX = _Main_Background.GetX() + xX;
        if (futureX <=0 && futureX >=YourScreenWidth -(_Main_Background.Width * scaleX))
        {
            _Main_Background.SetX(futureX);
            Console.WriteLine("futureY : " + (_Main_Background.Height * scaleX).ToString());
            Console.WriteLine("futureX : " + futureX.ToString());
        }
    }

    private void _MoveToY(float yY)
    {
        float futureY = _Main_Background.GetY() + yY;
        if (futureY <= 0 && futureY >=YourScreenHeight- (_Main_Background.Height * scaleY))
        {
            _Main_Background.SetY(futureY);
            Console.WriteLine("futureY : " + (_Main_Background.Height * scaleY).ToString());
            Console.WriteLine("futureY : " + futureY.ToString());
        }
    }