在程序集引用 Xamarin 中找不到 ImageManager
ImageManager could not be found in assembly reference Xamarin
嘿,我是 xamarin 的新手,我正在使用本教程处理一个需要调整图像大小的示例 http://sharpmobilecode.com/android-listviews-reinvented/。但是当我添加 ImageManager.错误说它找不到程序集引用。它不在 System.Drawing 上,也在 Android.Drawing 上。我尝试在 nuget 中安装,但它会产生错误。提前致谢,祝你有美好的一天。
ImageManager 是本教程也实现的 class:
因此创建一个 class 并将其命名为 ImageManager,添加下面的代码并更改命名空间以匹配您的命名空间:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Content.Res;
using Android.Graphics;
namespace ListViewsReinvented.Droid
{
public class ImageManager : IDisposable
{
private readonly Dictionary<int, Bitmap> _imageCache = new Dictionary<int, Bitmap>();
private Resources _resources;
public ImageManager(Resources resources)
{
_resources = resources;
}
private Task<BitmapFactory.Options> GetBitmapOptionsOfImageAsync(int resourceId)
{
return Task.Run(() => GetBitmapOptionsOfImage(resourceId));
}
private BitmapFactory.Options GetBitmapOptionsOfImage(int resourceId)
{
var options = new BitmapFactory.Options
{
InJustDecodeBounds = true
};
var result = BitmapFactory.DecodeResource(_resources, resourceId, options);
return options;
}
private int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
float height = options.OutHeight;
float width = options.OutWidth;
double inSampleSize = 1D;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = (int)(height / 2);
int halfWidth = (int)(width / 2);
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return (int)inSampleSize;
}
private Task<Bitmap> LoadScaledDownBitmapForDisplayAsync(BitmapFactory.Options options, int resourceId, int reqWidth, int reqHeight)
{
return Task.Run(() => LoadScaledDownBitmapForDisplay(options, resourceId, reqWidth, reqHeight));
}
private Bitmap LoadScaledDownBitmapForDisplay(BitmapFactory.Options options, int resourceId, int reqWidth, int reqHeight)
{
options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
options.InJustDecodeBounds = false;
var bitmap = BitmapFactory.DecodeResource(_resources, resourceId, options);
return bitmap;
}
public Task<Bitmap> GetScaledDownBitmapFromResourceAsync(int resourceId, int requiredWidth, int requiredHeight)
{
return Task.Run(() => GetScaledDownBitmapFromResource(resourceId, requiredWidth, requiredHeight));
}
public Bitmap GetScaledDownBitmapFromResource(int resourceId, int requiredWidth, int requiredHeight)
{
Bitmap bitmap;
if(_imageCache.TryGetValue(resourceId, out bitmap))
{
return bitmap;
}
var options = GetBitmapOptionsOfImage(resourceId);
bitmap = LoadScaledDownBitmapForDisplay(options, resourceId, requiredWidth, requiredHeight);
_imageCache.Add(resourceId, bitmap);
return bitmap;
}
#region IDisposable implementation
public void Dispose()
{
if(_imageCache == null)
return;
foreach(var key in _imageCache.Keys)
{
Bitmap bitmap;
if(_imageCache.TryGetValue(key, out bitmap))
{
Console.WriteLine(String.Format("Recycling bitmap {0} . . .", key));
bitmap.Recycle();
}
}
_imageCache.Clear();
}
#endregion
}
}
嘿,我是 xamarin 的新手,我正在使用本教程处理一个需要调整图像大小的示例 http://sharpmobilecode.com/android-listviews-reinvented/。但是当我添加 ImageManager.错误说它找不到程序集引用。它不在 System.Drawing 上,也在 Android.Drawing 上。我尝试在 nuget 中安装,但它会产生错误。提前致谢,祝你有美好的一天。
ImageManager 是本教程也实现的 class:
因此创建一个 class 并将其命名为 ImageManager,添加下面的代码并更改命名空间以匹配您的命名空间:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Content.Res;
using Android.Graphics;
namespace ListViewsReinvented.Droid
{
public class ImageManager : IDisposable
{
private readonly Dictionary<int, Bitmap> _imageCache = new Dictionary<int, Bitmap>();
private Resources _resources;
public ImageManager(Resources resources)
{
_resources = resources;
}
private Task<BitmapFactory.Options> GetBitmapOptionsOfImageAsync(int resourceId)
{
return Task.Run(() => GetBitmapOptionsOfImage(resourceId));
}
private BitmapFactory.Options GetBitmapOptionsOfImage(int resourceId)
{
var options = new BitmapFactory.Options
{
InJustDecodeBounds = true
};
var result = BitmapFactory.DecodeResource(_resources, resourceId, options);
return options;
}
private int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
float height = options.OutHeight;
float width = options.OutWidth;
double inSampleSize = 1D;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = (int)(height / 2);
int halfWidth = (int)(width / 2);
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return (int)inSampleSize;
}
private Task<Bitmap> LoadScaledDownBitmapForDisplayAsync(BitmapFactory.Options options, int resourceId, int reqWidth, int reqHeight)
{
return Task.Run(() => LoadScaledDownBitmapForDisplay(options, resourceId, reqWidth, reqHeight));
}
private Bitmap LoadScaledDownBitmapForDisplay(BitmapFactory.Options options, int resourceId, int reqWidth, int reqHeight)
{
options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
options.InJustDecodeBounds = false;
var bitmap = BitmapFactory.DecodeResource(_resources, resourceId, options);
return bitmap;
}
public Task<Bitmap> GetScaledDownBitmapFromResourceAsync(int resourceId, int requiredWidth, int requiredHeight)
{
return Task.Run(() => GetScaledDownBitmapFromResource(resourceId, requiredWidth, requiredHeight));
}
public Bitmap GetScaledDownBitmapFromResource(int resourceId, int requiredWidth, int requiredHeight)
{
Bitmap bitmap;
if(_imageCache.TryGetValue(resourceId, out bitmap))
{
return bitmap;
}
var options = GetBitmapOptionsOfImage(resourceId);
bitmap = LoadScaledDownBitmapForDisplay(options, resourceId, requiredWidth, requiredHeight);
_imageCache.Add(resourceId, bitmap);
return bitmap;
}
#region IDisposable implementation
public void Dispose()
{
if(_imageCache == null)
return;
foreach(var key in _imageCache.Keys)
{
Bitmap bitmap;
if(_imageCache.TryGetValue(key, out bitmap))
{
Console.WriteLine(String.Format("Recycling bitmap {0} . . .", key));
bitmap.Recycle();
}
}
_imageCache.Clear();
}
#endregion
}
}