Пример #1
0
        /// <summary>
        /// Loads a non-scaled bitmap from the Assets folder into memory.
        /// </summary>
        /// <returns>The bitmap.</returns>
        /// <param name="maxPoolSize">The maximum limit of images to pool.</param>
        /// <param name="context">A context to get an AssetManager instance.</param>
        /// <param name="fileName">The image file path.</param>
        /// <param name="assetImageDir">The parent directory for <paramref name="fileName"/> path parameter.</param>
        public Bitmap LoadBitmap(int maxPoolSize, Context context, string fileName,
                                 string assetImageDir = null)
        {
            Bitmap bitmap = DecodeBitmap(maxPoolSize, context, fileName, assetImageDir);

            if (Env.Debug)
            {
                Log.Debug(TAG, "Sync. Decoding ({0}) done. Bytes:{1}", bitmap.ToString(), bitmap.ByteCount.ToString());
                TimeProfiler.LogMemInfo();
                Log.Debug(TAG, "========================================");
            }
            return(bitmap);
        }
Пример #2
0
 // Once complete, see if ImageView is still around and set bitmap.
 protected override void OnPostExecute(Bitmap bitmap)
 {
     if (imageViewReference != null && bitmap != null)
     {
         ImageView imageView;
         if (imageViewReference.TryGetTarget(out imageView))
         {
             if (Env.Debug)
             {
                 Log.Debug(TAG, "Async. Decoding ({0}) done. Bytes:{1}", bitmap.ToString(), bitmap.ByteCount.ToString());
                 TimeProfiler.LogMemInfo();
             }
             imageView.SetImageBitmap(bitmap);
         }
     }
 }
Пример #3
0
        Bitmap DecodeBitmap(int maxPoolSize, Context context, string fileName, string assetImageDir)
        {
            string filePath = System.IO.Path.Combine(assetImageDir ?? DataHolder.Current.Common.AssetImageDir, fileName);

            BitmapFactory.Options options = null;
            if (bitmapPool.TryGetOptions(maxPoolSize, filePath, out options))
            {
                return(options.InBitmap);
            }

            long started = TimeProfiler.Start();

            if (options.InBitmap == null)
            {
                // First decode with inJustDecodeBounds=true to check dimensions
                options.InJustDecodeBounds = true;
                using (var stream = AssetHelper.Instance.Open(filePath))
                {
                    BitmapFactory.DecodeStream(stream, null, options);
                }

                // Due to a bug in SDKs below Kitkat a scaled down version of a bitmap cannot be decoded into an existing bitmap
                var ratio = (Env.KitkatSupport) ? 2 : 1;

                options.OutWidth  = options.OutWidth / ratio;
                options.OutHeight = options.OutHeight / ratio;
                Bitmap bitmap = Bitmap.CreateBitmap(options.OutWidth, options.OutHeight, options.InPreferredConfig);
                options.InJustDecodeBounds = false;
                options.InBitmap           = bitmap;
                options.InSampleSize       = ratio;
            }

            // Decode bitmap with inSampleSize set
            using (var stream = AssetHelper.Instance.Open(filePath))
            {
                var bitmap = BitmapFactory.DecodeStream(stream, null, options);
                TimeProfiler.StopAndLog(TAG, "Decode Stream", started);
                return(bitmap);
            }
        }