Forum Xamarin.Android

Xamarin, Moving images with Zoom in using Buttom

sboinchsboinch Member ✭✭
edited June 2018 in Xamarin.Android

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Views;
using Android.Views.Animations;
using Android.Widget;

namespace Project
{
[Activity(Label = "Project", MainLauncher = true)]
public class Main_Activity : Activity, View.IOnTouchListener
{
LinearLayout _Main_Contact_Layout;
FrameLayout _Main_Frame;
ImageView _Main_Background;
private List _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;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        RequestWindowFeature(WindowFeatures.NoTitle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main_Layout);

        _Main_Contact_Layout = (LinearLayout)FindViewById(Resource.Id._Main_Content_Layout);
        _Main_Contact_Layout.SetOnTouchListener(this);

        _Main_Frame = (FrameLayout)FindViewById(Resource.Id._Main_Frame);

        _Main_Background = (ImageView)FindViewById(Resource.Id._Main_Background);



        Button _Contact_Menu_Button = (Button)FindViewById(Resource.Id._Contact_Menu_Button);

        _Contact_Menu_Button.Click += (s, e) =>
        {
            Intent _Sub_Main_Layout = new Intent(this, typeof(Sub_Main_Activity));
            this.StartActivity(_Sub_Main_Layout);
        };



        Button _Contact_Up_Button = (Button)FindViewById(Resource.Id._Contact_Up_Button);
        Button _Contact_Down_Button = (Button)FindViewById(Resource.Id._Contact_Down_Button);

        _Contact_Up_Button.Click += (s, e) =>
        {
            _Zoom_In(_Main_Background);
        };

        _Contact_Down_Button.Click += (s, e) =>
        {
            _Zoom_Out(_Main_Background);
        };


        Button _Contant_Power_Button = (Button)FindViewById(Resource.Id._Contact_Power_Button);

        _Contant_Power_Button.LongClick += (s, e) =>
        {
            LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
            View _Power_Popup = inflater.Inflate(Resource.Layout.Custom_Power_Popup, null);

            PopupWindow _Power = new PopupWindow(_Power_Popup, 500, 300);
            _Power.SetBackgroundDrawable(new BitmapDrawable());
            //_Power.OutsideTouchable = true;

            ImageButton _Custom_Back_Button = (ImageButton)_Power_Popup.FindViewById(Resource.Id._Custom_Back_Button);
            ImageButton _Custom_Exit_Button = (ImageButton)_Power_Popup.FindViewById(Resource.Id._Custom_Exit_Button);

            //_Power.ShowAsDropDown(_Power_Popup, _Params.X, _Params.Y);
            _Power.ShowAtLocation(_Power_Popup, GravityFlags.Center, 0, 0);

            _Custom_Back_Button.Click += (a, b) =>
            {
                _Power.Dismiss();
            };

            _Custom_Exit_Button.Click += (u, n) =>
            {
                Finish();
            };

            _Contant_Power_Button.Click += (c, d) =>
            {
                if (_Power.IsShowing)
                {
                    Finish();
                }
            };
        };


        _Contant_Power_Button.Click += (s, e) =>
        {

        };


        _DrawerList = FindViewById<ListView>(Resource.Id._DrawerList);
        SetDrawerList();
        ArrayAdapter<string> _DrawerAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _DrawerList_Item);
        _DrawerList.Adapter = _DrawerAdapter;

        _DrawerList.ItemClick += _DrawerList_ItemClick;

    }



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

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

                _XX = e.RawX;
                _YY = e.RawY;

                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:

                float w = _Main_Background.Width;
                float h = _Main_Background.Height;

                float mw = _Main_Background.MaxWidth;
                float mh = _Main_Background.MaxHeight;

                float x = _Main_Background.GetX();
                float y = _Main_Background.GetY();

                float s = _Main_Contact_Layout.Width;
                float m = _Main_Contact_Layout.Height;

                float xx = e.GetX() - _XX;
                float yy = e.GetY() - _YY;

                if (scaleX > 1.0f & scaleY > 1.0f & w < mw & h < mh)
                {
                    _Main_Background.SetX(xx);
                    _Main_Background.SetY(yy);
                }

                break;
        }

        return true;
    }


    private void _DrawerList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        if (_DrawerList_Item[e.Position] == "One")
        {
            Toast.MakeText(this, "첫번째 클릭 이벤트", ToastLength.Short).Show();
        }
    }


    private void SetDrawerList()
    {
        _DrawerList_Item = new List<string>();
        _DrawerList_Item.Add("One");
        _DrawerList_Item.Add("Two");
        _DrawerList_Item.Add("Three");
        _DrawerList_Item.Add("Four");
        _DrawerList_Item.Add("Five");
    }


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

}

----> Current code

  1. Zoom in and Zoom out using the Button

  2. Moving images with Zoom in using Button

  3. Move only within layout range

  4. Use MotionEventActions.Move

Status of the current code

  1. If you move it after Zoom in, start with moving the image to a strange place.

  2. Layout out of range

What should I do?

Tagged:
Sign In or Register to comment.