Android实现图片双指缩放
作者:Prosper Lee
这篇文章主要为大家详细介绍了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);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
