Search code examples
c#androidxamarinmobile-application

Resize image before displaying it in ImageView


I'm working on a fragment that should load an image from the internet, resize it and then display it to the user. The goal is to use the image for a GridView later on. But I can't seem to get the resizing working at all. I've been stuck for a couple of hours trying different methods without any luck.

Here's my code:

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>

This is what it looks like when I run it filling up the whole screen: enter image description here


Solution

  • The problems seems to have solved itself. I needed the bitmap in a GridView, so while I was testing this, before inflating the bitmap to the GridView, the resizing didn't work. However, when I inserted it into the grid it automatically resized without any of the resizing properties set in the xml.

    Even though I didn't find the solution for why it wasn't resizing during testing, it might have something to do with the parent fragment or parent activity using match_parent.