如何在Xamarin.Android中使图像可缩放?

Montoolivo

我有以下图像:

在此处输入图片说明

其尺寸为1920 X 1901 pixels在以下链接上是其实际尺寸:

https://i.stack.imgur.com/0rjdP.jpg

我希望它占据屏幕下面的所有空间,toolbar但是当我将图像放在屏幕上并运行应用程序时,它是如此之大,当我尝试进入activity图像时,出现了异常。除了要使图像占据整个空间之外,我还希望在我展开手势和收缩手势时使用工具栏,该图像分别用于放大和缩小。

PS我在5英寸设备上运行该应用程序,但我也计划在更大和较小的设备上运行它。

少年江-MSFT

在Xamarin Android中,您可以自定义ImageView的Touch事件来实现它。

Xml:添加带有示例图像的ImageView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/main_imgZooming"
        android:background="@color/accent_material_dark"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="matrix"
        android:layout_gravity="center"
        android:src="@drawable/imageone"/>

</RelativeLayout>

然后在“活动”中,您可以自定义触摸事件:

public class MainActivity : AppCompatActivity
{
    private ImageView view;

    private static  int NONE = 0;
    private static  int DRAG = 1;
    private static  int ZOOM = 2;

    private int mode = NONE;
    private float oldDist;
    private Matrix matrix = new Matrix();
    private Matrix savedMatrix = new Matrix();
    private PointF start = new PointF();
    private PointF mid = new PointF();

    float minScaleR = 0.1f;  //Minimum scaling
    static  float MAX_SCALE = 4f; //Maximum zoom ratio
    //float dist = 1f;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        /*
        view = FindViewById<ImageView>(Resource.Id.main_imgZooming);
        view.Touch += Img_test_Touch;

        matrix.SetScale(minScaleR, minScaleR); //Start zooming out first

        var metrics = Resources.DisplayMetrics;
        int screenWidth = metrics.WidthPixels;

        matrix.PostTranslate(screenWidth / 4, screenWidth / 2);     //The position of the image is offset by screenWidth/2 pixels from the top left corner of the imageview
        view.ImageMatrix = matrix;
        */
    }

    // calculator the distance of two points
    private float distance(MotionEvent eventA)
    {
        float x = eventA.GetX(0) - eventA.GetX(1);
        float y = eventA.GetY(0) - eventA.GetY(1);
        return (float)(System.Math.Sqrt(x * x + y * y));
    }

    // calculator the middle point of two points
    private PointF middle(MotionEvent eventB)
    {
        float x = eventB.GetX(0) + eventB.GetX(1);
        float y = eventB.GetY(0) + eventB.GetY(1);
        return new PointF(x / 2, y / 2);
    }


    private void Img_test_Touch(object sender, Android.Views.View.TouchEventArgs e)
    {
        ImageView view = sender as ImageView;
        switch (e.Event.Action & e.Event.ActionMasked)
        {
            //single finger
            case MotionEventActions.Down:
                //matrix.Set(view.Matrix);
                savedMatrix.Set(matrix);
                start.Set(e.Event.GetX(), e.Event.GetY());
                mode = DRAG;
                Console.WriteLine(mode +"---"+ e.Event.GetX());
            break;

            //double finger
            case MotionEventActions.PointerDown:
                oldDist = distance(e.Event);
                if (oldDist > 10f)
                {
                    savedMatrix.Set(matrix);
                    middle(e.Event);
                    mode = ZOOM;
                }
                Console.WriteLine(mode+"---" + oldDist);
             break;
            //finger up
            case MotionEventActions.Up:
            case MotionEventActions.PointerUp:
                mode = NONE;
                break;
            // finger move
            case MotionEventActions.Move:
                if (mode == DRAG)
                {
                    //single finger
                    matrix.Set(savedMatrix);
                    matrix.PostTranslate(e.Event.GetX() - start.X, e.Event.GetY() - start.Y);
                } else if (mode == ZOOM) {
                    //double finger
                    float newDist = distance(e.Event);
                    if (newDist > 10f) {
                        matrix.Set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.PostScale(scale, scale, mid.X, mid.Y);
                    }
            }
            break;
        }

        view.ImageMatrix = matrix;
        CheckScale();  //Limit zoom range
        center();
    }

    //Limit maximum and minimum scaling
    protected void CheckScale()
    {
        float[] p = new float[9];
        matrix.GetValues(p);
        if (mode == ZOOM)
        {
            if (p[0] < minScaleR)
            {
                matrix.SetScale(minScaleR, minScaleR);
            }
            if (p[0] > MAX_SCALE)
            {
                matrix.Set(savedMatrix);
            }
        }
    }


    //Automatic centering
    protected void center()
    {
        center(true, true);
    }

    private void center(bool horizontal, bool vertical)
    {
        Matrix m = new Matrix();
        m.Set(matrix);
        Drawable d = GetDrawable(Resource.Drawable.imageone);
        //Get image width and height
        int imgWidth = d.IntrinsicWidth;
        int imgHeight = d.IntrinsicHeight;


        RectF rect = new RectF(0, 0, imgWidth, imgHeight);
        m.MapRect(rect);
        float height = rect.Height();
        float width = rect.Width();
        float deltaX = 0, deltaY = 0;

        var metrics = Resources.DisplayMetrics;

        if (vertical)
        {

            int screenHeight = metrics.HeightPixels;
            //Phone screen resolution height

            if (height < screenHeight)
            {
                deltaY = (screenHeight - height) / 2 - rect.Top;
            }
            else if (rect.Top > 0)
            {
                deltaY = -rect.Top;
            }
            else if (rect.Bottom < screenHeight)
            {
                deltaY = view.Height - rect.Bottom;
            }
        }

        if (horizontal)
        {
            //Phone screen resolution width
            int screenWidth = metrics.WidthPixels;

            if (width < screenWidth)
            {
                deltaX = (screenWidth - width) / 2 - rect.Left;
            }
            else if (rect.Left > 0)
            {
                deltaX = -rect.Left;
            }
            else if (rect.Right < screenWidth)
            {
                deltaX = screenWidth - rect.Right;
            }
        }
        matrix.PostTranslate(deltaX, deltaY);
    }
}

效果如下:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Xamarin Android中翻转图像?

来自分类Dev

如何在Xamarin Android中使用RunOnUIThread()

来自分类Dev

如何在Xamarin Android中使用RunOnUIThread()

来自分类Dev

如何在Xamarin Android中使用NLog

来自分类Dev

如何在php gd中使用插值缩放图像?

来自分类Dev

如何在Android Studio中使用AsyncTask从可绘制对象设置图像

来自分类Dev

如何在Xamarin.Android中使用C#查找到所选图像的文件路径?

来自分类Dev

如何在Xamarin iOS中使用applicationDidBecomeActive?

来自分类Dev

如何在Xamarin中使用SignalR

来自分类Dev

如何在xamarin中使用OnPageScrollStateChanged

来自分类Dev

如何在Xamarin中使用Hash SHA

来自分类Dev

如何在Xamarin.Android中使用SharedPreferences?

来自分类Dev

如何在Xamarin Forms PCL中使用本机Android ImageView?

来自分类Dev

如何在Android上的Xamarin中使用Spatialite

来自分类Dev

如何在Xamarin Android应用程序中使用动画?

来自分类Dev

如何在Xamarin中使用Android的内部存储?

来自分类Dev

如何在Xamarin.Forms中使用Android控件

来自分类Dev

如何在Xamarin.Android中使用Value Animator?

来自分类Dev

如何在Xamarin.Android中使用NFC发送消息?

来自分类Dev

如何在Xamarin.Android中使用MPAndroidChart的ValueFormatter

来自分类Dev

如何在 xamarin Android 中使用 netstandard 2.1?

来自分类Dev

Xamarin Android:如何在 Canvas 上绘图

来自分类Dev

如何在Android中使用图像映射?

来自分类Dev

我如何创建鼠标滚轮可缩放图像?

来自分类Dev

如何使图像可缩放并同时保持其纵横比?

来自分类Dev

如何在C#Windows窗体中的可缩放图像上绘制

来自分类Dev

如何在可缩放的森伯斯特图表中使用CSV数据?

来自分类Dev

Xamarin缩放的圆形图像

来自分类Dev

如何在 Xcode 的 Interface Builder 中使用约束/自动布局来正确缩放图像和字体?

Related 相关文章

  1. 1

    如何在Xamarin Android中翻转图像?

  2. 2

    如何在Xamarin Android中使用RunOnUIThread()

  3. 3

    如何在Xamarin Android中使用RunOnUIThread()

  4. 4

    如何在Xamarin Android中使用NLog

  5. 5

    如何在php gd中使用插值缩放图像?

  6. 6

    如何在Android Studio中使用AsyncTask从可绘制对象设置图像

  7. 7

    如何在Xamarin.Android中使用C#查找到所选图像的文件路径?

  8. 8

    如何在Xamarin iOS中使用applicationDidBecomeActive?

  9. 9

    如何在Xamarin中使用SignalR

  10. 10

    如何在xamarin中使用OnPageScrollStateChanged

  11. 11

    如何在Xamarin中使用Hash SHA

  12. 12

    如何在Xamarin.Android中使用SharedPreferences?

  13. 13

    如何在Xamarin Forms PCL中使用本机Android ImageView?

  14. 14

    如何在Android上的Xamarin中使用Spatialite

  15. 15

    如何在Xamarin Android应用程序中使用动画?

  16. 16

    如何在Xamarin中使用Android的内部存储?

  17. 17

    如何在Xamarin.Forms中使用Android控件

  18. 18

    如何在Xamarin.Android中使用Value Animator?

  19. 19

    如何在Xamarin.Android中使用NFC发送消息?

  20. 20

    如何在Xamarin.Android中使用MPAndroidChart的ValueFormatter

  21. 21

    如何在 xamarin Android 中使用 netstandard 2.1?

  22. 22

    Xamarin Android:如何在 Canvas 上绘图

  23. 23

    如何在Android中使用图像映射?

  24. 24

    我如何创建鼠标滚轮可缩放图像?

  25. 25

    如何使图像可缩放并同时保持其纵横比?

  26. 26

    如何在C#Windows窗体中的可缩放图像上绘制

  27. 27

    如何在可缩放的森伯斯特图表中使用CSV数据?

  28. 28

    Xamarin缩放的圆形图像

  29. 29

    如何在 Xcode 的 Interface Builder 中使用约束/自动布局来正确缩放图像和字体?

热门标签

归档