亲宝软件园·资讯

展开

Android图片双指缩放 Android实现图片双指缩放

Prosper Lee 人气:0
想了解Android实现图片双指缩放的相关内容吗,Prosper Lee在本文为您仔细讲解Android图片双指缩放的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android图片双指缩放,Android图片缩放,Android双指缩放,下面大家一起来学习吧。

展示

源码

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

加载全部内容

相关教程
猜你喜欢
用户评论