在程序集引用Xamarin

时间:2016-05-31 14:08:53

标签: xamarin xamarin.android

嘿,我是xamarin的新手,我正在研究一个需要使用本教程http://sharpmobilecode.com/android-listviews-reinvented/进行成像大小调整的样本。但是当我添加ImageManager时。错误说它无法找到程序集引用。它不在Android.Drawing的System.Drawing上。我尝试在nuget中安装,但它会产生错误。提前谢谢,祝你有愉快的一天。

1 个答案:

答案 0 :(得分:1)

ImageManager也是教程实现的类:

https://github.com/SharpMobileCode/ListViewsReinvented/blob/master/ListViewsReinvented.Droid/ImageManager.cs

因此,创建一个类并将其命名为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
    }
}