欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android实现图片双指缩放

程序员文章站 2022-04-14 11:37:43
本文实例为大家分享了android实现图片双指缩放的具体代码,供大家参考,具体内容如下展示源码using android.app;using android.os;using android.util...

本文实例为大家分享了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);
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。