internal static async Task <Drawable> GetFormsDrawableAsync(this Context context, ImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (imageSource == null || imageSource.IsEmpty)
            {
                return(null);
            }

            // try take a shortcut for files
            if (imageSource is FileImageSource fileImageSource)
            {
                var file = fileImageSource.File;
                var id   = IdFromTitle(file, DrawableClass);

                // try the drawables via id
                if (id != 0)
                {
                    var drawable = AndroidAppCompat.GetDrawable(context, id);
                    if (drawable != null)
                    {
                        return(drawable);
                    }
                }

                // try a direct file on the file system
                if (File.Exists(file))
                {
                    using (var bitmap = await BitmapFactory.DecodeFileAsync(file).ConfigureAwait(false))
                    {
                        if (bitmap != null)
                        {
                            return(new BitmapDrawable(context.Resources, bitmap));
                        }
                    }
                }

                // try the bitmap resources via id
                if (id != 0)
                {
                    using (var bitmap = await BitmapFactory.DecodeResourceAsync(context.Resources, id).ConfigureAwait(false))
                    {
                        if (bitmap != null)
                        {
                            return(new BitmapDrawable(context.Resources, bitmap));
                        }
                    }
                }
            }

            // fall back to the handler
            using (var bitmap = await context.GetFormsBitmapAsync(imageSource, cancellationToken).ConfigureAwait(false))
            {
                if (bitmap != null)
                {
                    return(new BitmapDrawable(context.Resources, bitmap));
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public static Drawable GetDrawable(this Context context, string name)
        {
            int id = IdFromTitle(name, DrawableClass);

            if (id == 0)
            {
                Log.Warning("Could not load image named: {0}", name);
                return(null);
            }

            return(AndroidAppCompat.GetDrawable(context, id));
        }
Exemplo n.º 3
0
        public static Drawable GetDrawable(this Resources resource, string name)
        {
            int id = IdFromTitle(name, DrawableClass, _drawableDefType, resource);

            if (id == 0)
            {
                Log.Warning("Could not load image named: {0}", name);
                return(null);
            }

            return(AndroidAppCompat.GetDrawable(Forms.Context, id));
        }