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

时间:2017-03-31 17:10:55

标签: c# android xamarin mobile-application

我正在制作一个片段,该片段应该从互联网上加载图片,调整大小然后再显示给用户。目标是稍后将图像用于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>

当我运行它填满整个屏幕时,这就是它的样子: enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个

 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 studio(java)我不知道我在哪里得到C#

答案 1 :(得分:0)

这些问题似乎已经解决了。 我需要GridView中的位图,所以当我测试它时,在将位图充气到GridView之前,调整大小不起作用。但是,当我将其插入网格时,它会自动调整大小,而不会在xml中设置任何调整大小属性。

即使我没有找到测试期间没有调整大小的解决方案,但它可能与使用match_parent的父片段或父活动有关。