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

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

问题描述:

我正在处理应从Internet上加载图像,调整大小并将其显示给用户的片段。目标是稍后将图像用于GridView。但我似乎无法完成调整大小的工作。我一直坚持了几个小时尝试不同的方法,没有任何运气。在将图像显示在ImageView中之前调整图像的大小

这里是我的代码:

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

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

即使我没有找到解决方案,为什么它在测试过程中没有调整大小,它可能与使用match_parent的父片段或父代活动有关。

试试这个

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#

+0

不幸的是,这两种解决方案都没有什么区别。 – rasperryPi

+0

什么在你的程序中执行我的代码?如果你一行一行debbug并打开视图位图是最小的或不是? – user7575308

+0

尝试看看“菜单”,如果没有在FitXY上设置scaletype或类似的东西 – user7575308