-
Notifications
You must be signed in to change notification settings - Fork 1.2k
CENTER_CROP is not precisely centered if border_width > 0 #8
Comments
mind opening this in a pull request? |
I'm still seeing this bug, or a slightly different bug. When I use the following XML, the white border causes the image itself to not be centred within the border circle: it's moved down and to the right by the looks of it. In our case this makes the border feature unusable, which is a shame.
|
A little experimentation shows that this would work, where we simply add /2 after each mBorderWidth, to ensure the image is centred within the border:
An alternative approach would be to scale the image down a bit (by border width presumably), so the border doesn't overlap its edges at all. |
I actually think borderWidth should not be part of the postTransate at all in this case since mBorderRect is already inset by the necessary amount. I think I will just remove that. There will still needs to be overlap, which is needed for a clean line between the border and the image. Otherwise you see small bits of white between the border and the image due to antialiasing. |
Hmmm - for me this fix doesn't work correctly, but my suggestion with mBorderWidth/2 does. Maybe there's something screwy with my images, but I don't believe so. I'm using avatar images, that by default are a centred, symmetrical graphic (e.g. the Twitter egg in this example) but I can see that the image does not end up centred within the border with the current fix. |
Hm, that looks to be off more than borderwidth/2... Any chance you can provide some sample code to reproduce? |
If I add in + mBorderWidth/2 to the translation it appears to centre perfectly. The XML in question is as follows:
A whole canned example would take a bit of effort as the images are fetched from the network etc. |
Do you have a copy of the image I could play with? |
Here's an example of one of the default Twitter avatars, where the egg is perfectly centred within the image: https://abs.twimg.com/sticky/default_profile_images/default_profile_2_normal.png |
Is there a workaround for this issue? |
For now I have my own versions of RoundedImageView and RoundedDrawable which I use in preference to the ones in the library, so that I can apply the fix (at least the fix that works for me, as detailed above). The fixed code is in my RoundedDrawable, but you need RoundedImageView too, so that it uses the modified RoundedDrawable throughout. |
I have implemented your solution but the image is not centred when scale type is centre. Link to modified file Edited:
|
news on this? |
After taking a second look, looks like @alwaysthecritic had the right fix the whole time. Not sure why that didn't click the first time. |
It should be translate along with border_width to be drawn correctly.
RoundedDrawable.java
private void setMatrix() {
...
...
CENTER_CROP:
...
...
mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth); // Translate by border_width to both x, y.
The text was updated successfully, but these errors were encountered: