Reputation: 2980
I have animated transparent gif image and need to display it in android. I tried Component GifImageView, everything is ok but background is black not transparent.
Thanks
Stream input = Assets.Open("oie_2103618iUK1c2Pr.gif");
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
bytes = ms.ToArray();
}
_gifImageView = new GifImageView(this);
_gifImageView.SetBytes(bytes);
_gifImageView.Background = new ColorDrawable(Color.Transparent);
LinearLayout giflayout = FindViewById<LinearLayout>(Resource.Id.gif);
giflayout.AddView(_gifImageView);
Upvotes: 1
Views: 3032
Reputation: 11
The current version of GifImageView doesn't have transparency changes that were just made a few days ago. I was able to get it working by having my Activity inherit from GifImageView.IOnFrameAvailableListener and setting my gifImageView.OnFrameAvailableListener to the Activity(as seen here). The last thing was to implement OnFrameAvailable in the Activity itself.
public Bitmap OnFrameAvailable(Bitmap bitmap)
{
if (bitmap != null) {
var newBitmap = bitmap.Copy (Bitmap.Config.Argb8888, true);
newBitmap.HasAlpha = true;
var pixelArray = new int[bitmap.Width * bitmap.Height];
bitmap.GetPixels (pixelArray, 0, bitmap.Width, 0, 0, bitmap.Width, bitmap.Height);
for (int ctr = 0; ctr <pixelArray.Count(); ctr++)
{
pixelArray[ctr] = pixelArray[ctr] == -16777216 ? 0 : pixelArray[ctr];
}
newBitmap.SetPixels (pixelArray, 0, bitmap.Width, 0, 0, bitmap.Width, bitmap.Height);
return newBitmap;
}
return bitmap;
}
This worked for me, hopefully it works for you too. You may need to change -16777216 to -1 if you see white backgrounds. Hopefully GifImageView gets updated soon, as this post-processing is not ideal.
Upvotes: 0
Reputation: 43
try to add:
GifImageView.setBackgroung();
or read this: Transparent GIF in Android ImageView
You can use another library, like Android Gif Drawable (GifImageView does not support transparent gifs), or set backround color (not transpatent) in gif, with gif editor.
Upvotes: 2
Reputation: 724
you cant set transparent background for gif image. Just remove it with online gif editor. There are a lot of them
Upvotes: 0