Search code examples
xamarinxamarin.androidffimageloading

FFImageLoading : load into an ImageButton


In Xamarin.Android, to load an image using FFImageLoading, a ImageViewAsync must be used instead of a standard ImageView.

I couldn't find what I can use if I wanna load my image into an ImageButton. Didn't find an "ImageButtonAsync".


Solution

  • AFAIK there is no particular ImageButton to use with FFImageLoading but you can use directly an ImageViewAsync set it as android:clickable="true" and add the click event.

    And as stated here an ImageButton is just an ImageView that has a non-null background by default and also, ImageButton.onSetAlpha() method always returns false, scaleType is set to center and it's always inflated as focusable. So if you need that behaviour you can add it.

    Another way would be you to create your custom ImageButton that can handle FFImageLoading image loading. For that you can inherit from ImageViewAsync and add the behaviours explained in the paragraph before. So that you can use the FFImageLoading API directly as this custom controls inherits ImageViewAsync.

    Instead of that you can also add your custom loading logic as explained here inheriting from ImageLoaderTask and call it like ImageService.Instance.LoadImage(customImageTask)

    Finally another way (but hackish and non-performant) would be to create an ImageViewAsync just to hold the result of the FFImageLoading and on the Success callback set the Drawable in the ImageButton:

    var myImageButton = myView.FindViewById<ImageButton>(Resource.Id.my_image_button);
    var myImageView = new ImageViewAsync(this); // this is the context
    ImageService.Instance.LoadUrl(this.Source[position].LogoImagePath)
                    .Success(() => myImageButton.SetImageDrawable(myImageView.Drawable))
                    .Into(myImageView);
    

    HIH and if you need any help let me know