Rotate GIF images in java - java

I am trying to rotate a GIF image in java.
I read these interresting two trails about rotating images in java : trail 1 , trail 2.
All works fine, except that when I rotate my GIF image, there is no more animation of the GIF image. Only the first image of the GIF animation is displayed.
So, is there any way to keep the GIF animation after rotating my GIF image, without using any third part library but only standard J2SE?
Or will I be obliged to separate my GIF image into single images, rotate them one by one, and then display them in a loop?
I don't give any piece of code I wrote because it is not relevant in my humble opinion.

I think animated gifs work by storing multiple frames, all in the gif format.
So you just need to apply you rotation method to every single image, instead of the whole gif.
Wikipedia has a quite nice description of the format

Related

Stop a looping GIF icon

I am using an animated GIF image as icon to button.
But this GIF loops without stopping.
How can I play this GIF icon animation just once?
When creating an animated GIF, there are options to have it only loop a single time. That is where you need to look for a solution to this problem. Swing itself provides no methodology to change the behavior.

Displaying a part of an image in libGDX - looks stretched

I'm trying to build an progress bar in libGDX, for that I have one full horizontal image and in two lines I trying to display 2 different widths of that image:
imageFull:
imageFull.draw(batch,10,80,600,50);
imageFull.draw(batch,10,20,100,50);
the result is:
Its looks like when the width is 'small' its stretched and looks bad.
Why I can't display only part of the image without destroying the left side of the image?
Any ideas how to fix it?
That is normal behavior. If you stretch a image without keeping it's aspect ratio it will deform, it does not know the stretchable part by itself.
9-patch will help you here but you cannot simply draw the sprite as you are doing now (maybe with SpriteDrawable though?).
If I where you I would use the Scene2D Actor named ProgressBar. Feed that a 9-patch image, then it should stretch correctly. Or just use a Image if you want to control it yourself, and feed this image a ninepatch.
A quick way to create a ninepatch is to specify it's stretching regions yourself by hardcoding it.
texture = new NinePatch(yourTexture, A, B, C, D)
Where ABCD corresponds to the following image:
Now create a Scene2D Image with that ninepatch and it should stretch properly.
If you have a this texture already in a Atlas you can also supply the line split:a,b,c,d to the image data in the .atlas file and Scene2D will automatically pick it up as a ninepatch.
If you don't want to use Scene2D and/or ninepatch (but I recommend you to use it) you have to code the behavior yourself. Or cut the texture up yourself and stick the caps on the left and right side of your rectangle. But Scene2D is invented for this and a ton more GUI functionality.

ImageView doesn't stretch image properly

So I have no clue why this is happening. I am using Universal Image Loader to load these images. It seems like the last line of pixels is being streched for some weird reason. I want the image to just stretch out evenly. (I don't care that it will look weird. The images below are for demo purposes.)
Also, don't mind the first and last image. I purposely blurred that out because it had someone's face on it.
This is how I set up my Universal Image Loader:
//setup Image Loader for loading cruise line logos
displayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)//show this image when image is loading
.showImageForEmptyUri(R.drawable.ic_launcher)//show this image incase image doesn't exist
.showImageOnFail(R.drawable.ic_launcher)//show this image if fetching image from URL didn't work
.cacheInMemory(true)//cache image in RAM
.cacheOnDisc(true)//cache image in device for later use
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(5))//super subtle rounded corners on images
.build();
This is caused by the way RoundedBitmapDisplayer draws the bitmap.
If you look at the source, you'll see that it uses a RoundedDrawable, which uses canvas.drawRoundRect() to draw a rounded rectangle of the desired size of the Drawable, using the downloaded image as the texture via a BitmapShader. BitmapShader does not support scaling (only clamping and tile modes). Try using a SimpleBitmapDisplayer instead which uses the normal ImageView.setImageBitmap() way of displaying the image.
If you need rounded corners, you'll have to find a different way to implement that, for example by scaling the Bitmap to the desired size first. Another option is to call Canvas.saveLayer() before delegating to BitmapDrawable for the scaling, and then applying the rounded corner masking effect using PorterDuff.Mode.DST_IN. Either way you'll end up writing a bit more low-level code, but you should be able to encapsulate everything nicely in a custom BitmapDisplayer.

How do I make my sprites have transparent backgrounds in Slick2D?

I've used GIMP to create a sprite using a transparent background. Once I put it in the Image Packer the transparent background disappears and the whole image is messed up. After a bit of googling without finding a single useful thing I decided to use a pink background, but I can't find support for setting a pixel to alpha (I can read the pixel's color though!)
How do I fix this and have a transparent background in my sprite (org.newdawn.slick.Image to be precise)?
Useful links: None as far as I can remember. Took me 45 minutes to jump to this site for help, so there's either pretty much nothing or literally no support.
Are you sure the image is saved in a format that supports the transparency? For example JPG does NOT support transparency. PNG does.
Also are you sure that when saving the picture the background is indeed transparent and not white? To double check the background is transparent the majority of image editors will display a grey and white checked grids to show transparency.

How can I cut an image using a color pattern?

I am developing a small program which cuts images by the color.
That's will be easiest to explain using this example image:
And I want to create a new image just with the purple form, without the black frame.
Does anyone have any ideas? I am using Java 2D so I think that I need to create an Object "Shape" with the purple area of the first image.
If the image is literally like the one you show, you could:
load the image into a BufferedImage (with ImageIO.read())
create a new BufferedImage of the same size, ensuring it has an alpha layer (e.g. set its type to BufferedImage.TYPE_4BYTE_ABGR)
"manually" go through each pixel in turn in the loaded BufferedImage, getting the pixel colour with getRGB() and checking if it's black
if the colour is black, set the corresponding pixel to transparent in the new image, else to the original colour from the first image (see setRGB() method)
save the new image (with ImageIO.write())
There are fancier ways, but this simple method is nice and understandable and will work fine for images of the type you showed.
You need to use some flood-fill algorithm that finds the boundries of the purple area:
Wikipedia has a page on it with excellent pseudo code and animations.
http://en.wikipedia.org/wiki/Flood_fill

Categories