Android实现图片双指缩放
时间:2022-08-03 12:20:51|栏目: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); } } }