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.
Related
I'm using SVG files in my Game and i want to add transparency and brightness effects to them. So what i want is that a sprite becomes more and more transparent until it's invisible. This looks much smoother than a just disappearing sprite. This allows also smooth transitions between the levels where a white screen gets less transparent and then again more. I hope you understand what i mean. Besides this a brightness effect would be nice, too. A sprite can get brighter or darker during the game. Nevertheless i could create a workaround for this using the transparency effect, but yeah, i have none :/
I use this library:
https://androidsvg.googlecode.com/hg/doc/reference/packages.html
I render my svgs on this way:
svg.renderToCanvas(pCanvas);
However i can also convert it to pictures:
Picture pic = svg.renderToPicture();
Maybe this information will help you.
Does there exist something what could help me? And if not, have you an idea how can i solve the problem?
EDIT:So iwant my SVGs behave like these ones:
http://scratch.mit.edu/projects/24634091/
You could use an ImageView and setAlpha(), as Frank says. Or it may just be easier to render the sprites to the canvas in your draw() method.
If you are writing a game, then you may find that rendering SVGs in your game loop may not be fast enough. So you might have to pre-render all your SVG sprites to Bitmaps (ie on startup) and draw them to your Canvas.
Paint p = new Paint();
p.setAlpha(alpha);
canvas.drawBitmap(sprite, x,y, p);
I'm trying to put corners im my ImageView and I find these "workarounds":
1 - Take the Bitmap soure and paint then
2 - put a second ImageView on my layout and use shape+corners as its source
It's not possible take the image view and put a corner around then?
In my app i have a List with a lot of ImageView (the Bitmap used in this ImageView's is programmatically) and I want to put corners in every ImageView.
There is any other option?
You can create a selector that contains shape with rounded corners, and then apply it as the ImageView's background using xml.
Look at this answers here
You can also do it via code by using PorterDuffXfermode. PorterDuffXfermode uses alpha compositing that will allow you to create and intersection (things of mathematical sets here) between the Canvas you get from onDraw() and an off screen Canvas you will have to create. You will use one of the PorterDuff.Mode flags to tell the framework you want to only render the pixels that intersect from the two Bitmaps in each Canvas.
More on Alpha Compositing
This is the scenario:
I have one image background set on an activity. On this background, a rectangle has been drawn (in other words, one image depicting a rectangle). I need to display text WITHIN this rectangle.
Right now, I have one solution in mind: since I'm going to optimize the UI for most screens (incl. tablets), I'm going to customize the main .xml layout for each screen size (multiple .xml layouts). Thus, I can manually set the place where the text area goes within the rectangle (+ its size).
I am most certain that this solution is NOT good. I'd like to hear some suggestions from more advanced developers. What would an elegant way of placing text over a background image showing a rectangle, so that the text stays within the rectangle's borders be?
Because I need to set particular positions for other UI elements (centered buttons (vertically/horizontally), I am currently using a Relative Layout.
By default if you have used dp as dimensional measure, your app should in theory work fine for all resoultions. Android by default scales the screen to fit different screens. AndroidDeveloper You just have to make sure that you have different images for resources (Rectangle image). There is another post in SO which you might be intrested to look into link
Cheers
RIchie
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
Preferably, I'd like a somewhat fancy bevel . I'd like the picture to have some depth on it..so it doesn't look so plain and old-fashion :)
You can perhaps do something with the 2D Canvas API, but I don't know enough about that to describe a process. Here's how you can try to do that with ordinary widgets...well, mostly ordinary...:
Step #1: Create a nine-patch PNG that has your "somewhat fancy bevel" and is designed to stretch around images.
Step #2: Set that nine-patch PNG as the background of the ImageView that has your image.
Step #3: Adjust the padding of the ImageView so that your bevel shows -- that'll depend on how wide you made the bevel effect.
It's possible you'll need to put the nine-patch PNG on a LinearLayout or something and have the ImageView in there (then use margins to allow the bevel to peek through), but I think you can do it without the extra widget.
I would say that the easiest thing to do is to set nine-patch PNG as the background of your ImageView. This way the src would load the image, while background attribute would load the background 9.png image.
When creating 9.png for the background take care that you do not mark border lines as streatchable. Look at the image.