Android studio XML centercrop doesn't center the image - java

I have added a background-image in Android Studio, I used center-crop to center it.
The problem I have is that the image does not center properly. it's slightly off to the right and I want it perfectly centered
Look on the right the stars are not well centered.
Here's what I have tried
Could you help me ?
Thank you.

Thanks for updating your question with the background image (bouclier.jpg) that you're using. The issue you're experiencing is not to do with your layout (XML) code, but rather with the image itself.
As you can see, your background image appears to be offset to the right ever so slightly (I've added a green cross in the above image to illustrate this). As a result, when you use android:scaleType="centerCrop" the image is correctly centred but doesn't look right.
My recommendation would be to choose a different (properly centred) image to use as your background, or alternatively to centre the image you're presently using.
I hope that helps!

Related

How to draw outline around a PNG image using JAVA in Android Studio?

I have a PNG image (with transparent background) and I want to draw an outline around its visible pixels only. (See example GIF attached). I want to get this all done in JAVA.
I've read many Q&A over stackoverflow and around the web but I didn't find anything anyway near to my requirements.
P.S: We don't have to draw the same image twice on canvas and use the bottom one as outline, so please don't propose such solutions.
P.P.S: I would be great if this solution lets me draw an outline around all visible objects/elements (images & textviews etc) inside a canvas or inside a layout.
Try using Open CV and drawing contours. It is the closest available solution

Android - 9 patch

I'm trying to create a new spinner background using a 9 patch picture. I've tried many ways to get a perfect image but I failed :s
Here is my 9 patch:
The content looks to be good when I simulate with Draw-9-patch but the part with the arrow is not displayed, or when it is, this part is totally mal-formed.
I've put the top black line to the whole size and the problem is still the same.
Do you know how to solve it?
Thanks.
Regards.
V.
First of all, you've marked your rounded corners as stretchable, this should be avoided so your corners stay the same, no matter how its stretched.
You've also marked your arrow as being stretchable vertically, which I suppose is not desirable.
See the attached image for a fixed 9-patch (note the differences in 9-patch frame compared to your image), this should scale well in all directions.

How to remove black border around transparent image in PDF created by iText

I have searched over bunch of sites, and I was unable to find solution for my problem.
This is the problem:
I am making PDF's in Java using iText library.
Everything works fine except one thing.
Transparent PNG images have black/gray border around non-transparent area.
I didn't set any borders in code, and actually I have tried to remove them (with no luck).
Can someone help me how to solve this problem?
The closest answer what I have found is: Resizing an image in asp.net without losing the image quality
But I cannot (don't know) interpret this code in Java.
My code is pretty big to copy/paste, but these are steps:
create document
load image from given path
manipulate image (resize, rotate, positioning)
add image to current page
save pdf file
This is what I have tried also:
http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2157267
http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2330200
I have tried more than those 2, but I didn't bookmarked them (none of them worked)
Thanks in advance
UPDATE: I forgot to mention that my original pictures don't have border. Border is created somehow by iText. I initially thought that it was bug, but since iText 5.0.2 this problem remained so now I doubt that is bug (I am currently using 5.1.3).
UPDATE 2 I forgot to add this link: http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2157261
Here is presented VB script that works, but I cannot convert to Java code (it still draws black border), so can someone help me at least with this to convert good?
You could use the java BufferedImage method, getSubImage(x, y, w, h) which allows you to crop a sub image out of an existing image. That way you could cut out the edges.
See here: Class BufferedImage

Canvas vs Image Android

I have a background image that is white and semi-transparent. This is used as a text container so you can differentiate between the background image and text.
Currently I am using an image that is a white rectangle with rounded corners, but of course when using this on different screen sizes the image is distorted, not massively or that noticeably, but would it be better or possible to draw this image with a canvas?
If it is possible would it slow down the app considerably?
UPDATE:
Thank you #jkhouw1 I have looked into 9 patch images and this seems very suitable. I am still interested in knowing the benefit of canvas against images, but if you post something as an answer I will accept it. Thank you.
use a nine patch is a graphic that android can scale appropriately. see here: http://developer.android.com/guide/developing/tools/draw9patch.html
and here http://www.higherpass.com/Android/Tutorials/Android-9-Patch-Scaled-Png-Image-Guide/
Haven't tried this yet, but I think this should be possible by defining a shape drawable. You can define round corners and you can make it semi-transparent by defining the color with alpha-value, for example #80FF0000 should be semi-transparent red.
I assume this would be a good solution both in terms of compatibility and performance.

how do I specify a transparent background colour on a draw2d Figure?

I'm writing an eclipse plugin, and I'm layering some simple figures (i.e. view elements) together to create diagrams.
A simple component looks like this
A simple component with rendering issues http://img115.yfrog.com/img115/4946/componentwithopacity.jpg
The drum icon on the right is part of a tiny widget drawer.
And the incomplete line underneath it is trying to represent multiplicity, but it's being blocked by the widget drawer.
I tried really hard to get a transparent background on the Figure, but the SWT colour class has no way to specify an alpha or transparent colour.
has anyone run into this problem before? How can I make my draw2d Figure background transparent so I can see stuff behind it.
This a small example of various other opacity related issues I've encountered. Solving this would fix a wide range of visual issues.
Any help is greatly appreciated.
CV
So IFigure.setOpaque(false) doesn't do it?
Simply set the alpha from 0 to 255 to your liking. See Graphics#setAlpha(int) during your paint event or see Shape - I think setAlpha(int) exists there too.

Categories

Resources