在 ImageView 中显示之前调整图像大小

Resize image before displaying it in ImageView

我正在开发一个片段,它应该从 Internet 加载图像,调整其大小,然后将其显示给用户。目标是稍后将图像用于 GridView。但我似乎根本无法调整大小。我已经被困了几个小时尝试不同的方法但没有任何运气。

这是我的代码:

VaultFragment.cs:

using Android.OS;
using Android.Views;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.V4.Widget;
using System.Collections.Generic;
using Android.Widget;
using System.Net;
using System;
using System.IO;
using Android.Graphics;

namespace Test_android.Fragments
{
    public class VaultFragment : SupportFragment
    {
        // Global variables
        private SwipeRefreshLayout gSwipeRefreshLayout;
        ImageView imageview;


        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // return our custom view for this fragment
            View view = inflater.Inflate(Resource.Layout.Vault, container, false);

            // Add SwipeRefreshLayout
            gSwipeRefreshLayout = view.FindViewById<SwipeRefreshLayout>(Resource.Id.swipeLayout);

            imageview = new ImageView(view.Context);
            DownloadRemoteImageFile();

            return view;

        }



        private async void DownloadRemoteImageFile()
        {
            WebClient webClient = new WebClient();
            var url = new Uri("https://logo.clearbit.com/cnn.com");
            byte[] bytes = null;
            try
            {
                bytes = await webClient.DownloadDataTaskAsync(url);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());

                return;
            }

            MemoryStream ms = new MemoryStream(bytes);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InJustDecodeBounds = false;
            int sampleSize = options.OutWidth / 100;
            options.OutWidth = 57;
            options.OutHeight = 57;

            Bitmap bitmap = BitmapFactory.DecodeStream(ms, null, options);
            //imageview.LayoutParameters = new LinearLayout.LayoutParams(57,57);

            var f = imageview.LayoutParameters;
            imageview.SetImageBitmap(bitmap);
            gSwipeRefreshLayout.AddView(imageview);


        }

    }
}

Vault.axml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</android.support.v4.widget.SwipeRefreshLayout>

这是我 运行 它填满整个屏幕时的样子:

试试这个

 Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
    bm, 0, 0, width, height, matrix, false);
bm.recycle();
    return resizedBitmap;
}

为我工作 android 工作室 (java) 我不知道我从哪里得到 C#

问题似乎已经自行解决了。 我需要 GridView 中的位图,因此当我对此进行测试时,在将位图扩展到 GridView 之前,调整大小不起作用。但是,当我将它插入网格时,它会自动调整大小,而无需 xml 中设置的任何调整大小属性。

尽管我没有找到为什么它在测试期间没有调整大小的解决方案,但它可能与使用 match_parent 的父片段或父 activity 有关。