欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)圖片雙指縮放

 更新時間:2021年11月03日 15:24:16   作者:Prosper Lee  
這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片雙指縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)圖片雙指縮放的具體代碼,供大家參考,具體內(nèi)容如下

展示

源碼

using Android.App;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace android_by_csharp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.main);

            var avatar = FindViewById<ImageView>(Resource.Id.avatar);
            var distance = 0;
            if (avatar != null)
                avatar.Touch += (sender, args) =>
                {
                    if (args.Event == null) return;
                    // 雙指按下
                    if ((args.Event.Action & MotionEventActions.Pointer2Down) == MotionEventActions.Pointer2Down)
                        distance = (int)Distance(args.Event);
                    // 雙指抬起
                    else if ((args.Event.Action & MotionEventActions.Pointer2Up) == MotionEventActions.Pointer2Up)
                        Toast.MakeText(this, "雙指抬起", ToastLength.Short)?.Show();

                    // 移動,雙指
                    if ((args.Event.Action & MotionEventActions.Move) == 0 || args.Event.PointerCount != 2) return;
                    var moveDistance = Distance(args.Event);
                    avatar.ScaleY = avatar.ScaleX *= moveDistance / distance;

                    if (moveDistance / distance < 1)
                        Toast.MakeText(this, "縮小", ToastLength.Short)?.Show();
                    else if (moveDistance / distance > 1)
                        Toast.MakeText(this, "放大", ToastLength.Short)?.Show();
                };
        }

        // 計算兩個觸摸點之間的距離
        private static float Distance(MotionEvent motionEvent)
        {
            var x = motionEvent.GetX(0) - motionEvent.GetX(1);
            var y = motionEvent.GetY(0) - motionEvent.GetY(1);
            return FloatMath.Sqrt(x * x + y * y);
        }
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論