WebView 判断放大缩小操作

复制代码
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using Android.Runtime;
using Android.Net;
using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Text;
using Android.Webkit;
using Android.Views;
using System.Collections.Generic;

namespace android_filepiker_demo_test
{
    [Activity(Label = "android_filepiker_demo_test", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        public class WebViewTouchParameter
        {
            public double Distance { get; set; }
            public int FontSize { get; set; }
            public int MinFontSize { get; set; }
            public int MaxFontSize { get; set; }
        }

        public delegate void FontSizeChanged(int fontSize);

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            var button1 = FindViewById<Button>(Resource.Id.button1);
            button1.Click += Button1_Click;

            var web1 = FindViewById<WebView>(Resource.Id.web1);
            web1.Touch += Web1_Touch;
            web1.LoadData("<html><body style='font-size:12px;'><h1>abcde</h1></body></html>", "text/html", "utf-8");
        }

        private void Web1_Touch(object sender, Android.Views.View.TouchEventArgs e)
        {
            WebViewTouchHandler(e, new WebViewTouchParameter { MinFontSize = 12, MaxFontSize = 50 }, delegate(int fontSize) 
            {
                var web1 = FindViewById<WebView>(Resource.Id.web1);
                web1.LoadDataWithBaseURL(null, "<html><body style='font-size:" + fontSize + "px;'><h1>" + fontSize + "px,abcde</h1></body></html>", "text/html", "utf-8", null);
            });
        }
private void Button1_Click(object sender, System.EventArgs e)
        {

        }
    }
}
复制代码

 

复制代码
private void WebViewTouchHandler(Android.Views.View.TouchEventArgs e, WebViewTouchParameter p, FontSizeChanged fsc)
        {
            var ev = e.Event;
            var action = ev.Action;

            switch (action)
            {
                case MotionEventActions.Pointer2Down:
                    {
                        System.Diagnostics.Debug.Print("Pointer2Down");

                        p.Distance = Math.Abs(Math.Sqrt(Math.Pow(ev.GetX(0) - ev.GetX(1), 2)
                            + Math.Pow(ev.GetY(0) - ev.GetY(1), 2)));

                        break;
                    }
                case MotionEventActions.Move:
                    {
                        if (e.Event.PointerCount == 2)
                        {
                            var distance = Math.Abs(Math.Sqrt(Math.Pow(ev.GetX(0) - ev.GetX(1), 2)
                                    + Math.Pow(ev.GetY(0) - ev.GetY(1), 2)));

                            p.FontSize += (int)((distance - p.Distance) / 7);

                            if (p.FontSize > p.MaxFontSize)
                                p.FontSize = p.MaxFontSize;

                            if (p.FontSize < p.MinFontSize)
                                p.FontSize = p.MinFontSize;

                            if (fsc != null && Math.Abs(distance - p.Distance) > 7)
                                fsc(p.FontSize);

                            p.Distance = distance;
                        }

                        break;
                    }

                case MotionEventActions.Up:
                    {
                        break;
                    }

                case MotionEventActions.Cancel:
                    {
                        break;
                    }

                case MotionEventActions.Pointer2Up:
                    {
                        break;
                    }
            }
        }
复制代码

此代码经过测试,可以稳定实现放大缩小。代码衔接调用处参数可能需要调试,最新优化调试后,我只更新了最后一段核心代码段。

posted on   空明流光  阅读(295)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2016-06-22 javascript 键盘输入过滤,只能输入数字,小数一位且只能输入5

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示