Quality of rendering shadow in transparent png on Android - android

When using a transparent PNG image which has a fine fading shadow always there is an edge line around the shadow on the Android screen. It does not show this in emualator or Exclipse. See photo.
I wonder if there is way to improve on this. Is this something to do with inability of Android screen to show all 24 bit colours or the fact that is scaling and resampling image?
In this example the image is loaded into an ImageButton view. I tried it as source or background and it is the same quality.

The artefact you are seeing is known as "banding" and it is a consequence of your display being 16bits per pixel.
The best way to resolve this is to add some "noise" to your image asset in Photoshop or Paint.NET.
Alternatively you can set your window to be 32bpp with the following line added to your activity's onCreate(), between super.onCreate() and setContentView(). :
getWindow().setFormat(PixelFormat.RGBA_8888);

Related

Is it possible to disable anti-aliasing on vector images?

On Wear OS in ambient mode, only black or white pixels should be displayed (no grays). Anti-aliasing a black and white image introduces gray pixels. Now, anti-alising can be disabled for a TextView by writing textView.paint.isAntiAlias = false (in Kotlin) but there doesn't seem to be an equivalent for vector images. The one thing I've tried is adding android:antialias="false" to the image XML file, but it seems to have no effect.
I don't think there is a way to do it in XML.
Personally I've only done this when drawing straight to a canvas. Setting setAntiAlias(false) (don't remember if we ended up using setDither(false) and setFilterBitmap(false) too) on the Paint did the trick.
You can try using a DrawableWrapper and make the necessary changes in the draw() method. Unfortunately this means that you have to set all your drawables in code, but at least you'll still be able to rely on an ImageView.

9Patch image not strectching

I am using http://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create a 9Patch image which is attached. . I use this is to set a drawableBottom to the TextView.But this does not stretch to the width the text view. What am i doing wrong ? This is the original image
Here are a few reasons for it to "not stretch"
Guides must only be one pixel wide, so if you want a 48×48 button, your png will actually be 50×50. Anything thicker than one pixel will remain part of your image.
guides must be solid black (#000000). Even a slight difference in color (#000001) or alpha will cause it to fail
MOST IMPORTANT: you should keep in mind that remaining area of the one-pixel outline must be completely transparent. This includes the four corners of the image – those should always be clear. This can be a bigger problem than you realize. For example, if you scale an image in Photoshop it will add anti-aliased pixels which may include almost-invisible pixels which will also cause it to fail*. If you must scale in Photoshop, use the Nearest Neighbor setting in the Resample Image pulldown menu (at the bottom of the Image Size pop-up menu) to keep sharp edges on your guides.
http://radleymarx.com/blog/simple-guide-to-9-patch/
You need to use android sdk draw9patch tool to make a 9-patch image.
you will need to define stretchable patches to image border like shown in below image border.

9-patch background renders with artifacts and cropping

I'm using a 9-patch file for a button background, and while the draw9patch tool validates the file, in Android Studio it renders with artifacts and cropping. I'm not sure if this is a bug in Android Studio or a mistake in my 9-patch. Here is the code for the button, if it helps.
<ImageButton
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/imageButton"
android:layout_weight="1"
android:src="#drawable/tool_panel_share_icon_default"
android:baselineAlignBottom="true"
android:background="#drawable/view_type_button_background_left_default"
android:scaleType="fitCenter"
/>
Here's my 9-patch:
this is what I think it should look like on the button:
but this is what I'm actually getting ( the bottom line is a 26% transparency over a dark background ):
you can see that there is an errant black line on the left, and the black line that should be on the right is apparently cropped.
Update:
Here is the actual 9-patch file, what I posted earlier was a screen shot of the file, for the sake of seeing the fine details.
Update2:
Ok, I figured out how to make it display correctly, but I still don't understand the reason for the initial failure. I realized that if I place the top marker next to the off-black line that is part of the 9-patch content, it fails; however, if i move it 1 pixel to the left it renders correctly.
Works:
Fails:
Here is the new 9-patch and a screenshot of the successful rendering:
Update 3:
I just recreated the 9-patch using the draw9patch tool and the behavior is the same.
I downloaded the 9patch png you provide, the following image is the left black dot of your png, which is not correct. The borders of the 9patch pngs should be only 1 pixel width, and could only use black as its color, which is (0, 0, 0).
Look this page for more information.
I have face the problem similar to your once.
I guess you place the nine patch image in folder with lower resolution than device.
When device try to get the image it need to scale the image up and your top marker is right at the "border" which is the gradient area .
This can result in unpredictable image after scale.
You make it too high.
Try to make it like i post.
Do you use
...\Android\android-sdk\tools\draw9patch.bat
to draw your image? It's really helpful and show preliminary result.

Android PNG Transparency Issue

I'm using PNG images with transparency in my game and no matter what I do the edges of them get all grainy looking.
I have tried setting the following in my activity with no luck:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DITHER, WindowManager.LayoutParams.FLAG_DITHER);
Is there anything I can do in Photoshop or in my game to overcome this. Please see the image here:
It looks like your image is too low a resolution and the edges are not tidy, there is some white left over from when you deleted the background.
Open the image in photoshop, zoom right in and fix your edges. Probably best to redraw and resize again for the screen you are using.

Mach Banding images in Android

I used a linear gradient image for a background and in Photoshop (and everything else) it looks nice and smooth but when I displayed it in the emulator is was banded! What's worse, it's banded on my actual phone - a Droid Incredible. I'm running 2.2 both in emulation and on the phone.
Here's a sample - original on the left, Android'ed version on the right: http://pnart.com/temp/AndroidMach.jpg
This has the appearance of Android imposing some bit-depth limitation. What's going on and how do I fix it?
Thanks in advance!
I had the same problem and did some searching on Google. One of the sites I found suggested I put the gradient image in res/raw/ and load the image when needed. I tried this and it worked.
From what little I understand, any image you place under "drawable" will be processed by AAPT and it isn't guaranteed the final images will be the same as the ones you are putting into it. In this case, it decided to shrink the PNG gradient image to a smaller palette to shrink the size of the final APK. If someone else has a better (or more correct) explanation, I'd love to hear.
-Dan
just try this: get down the width and height to the screen resolution of the device (eg: for moto droid: w:320px and h:480px and in photoshop keep the resolution to 200 dpi or above)
Regards,
Mistry Hardik
If there isn't any special reason for using an image to get a gradient I highly recommend looking up the Shape Drawable as it supports gradients which should (I haven't tried it but I assume) allow lossless scaling.
First of all, the color depth of phone's screen is much worse than that of your PC. On your PC you have 8-8-8 bits depth for R-G-B but for phones it's typical to have 5-6-5 depth. It means that any fine gradients/shades that look good on PC will be distorted when displayed on the device because its color depth is just not enough, physically.
Therefore, designer's rule №1: avoid fine shades and fine gradients in your designs.
But if you have to then, of course, you may try the following, but you've been warned!
1) For runtime-generated gradients: use dithering, like this:
setContentView(R.layout.screen_dashboard);
findViewById(R.id.layout).getBackground().setDither(true);
2) For graphics assets: always apply a 5-6-5 filter before saving png. Here is a good article with examples. Applying 5-6-5 filter ensures that color depth of your png is within device capabilities, and decreases png size too.
Seems like it might be because it's scaling the image to fit the screen. When non-vector gradients are scaled they often get that "banded" look. Try making a gradient that's the same size as the screen you're targeting, or try changing the ImageView (or whatever) that is displaying the image to make sure it's not stretching or scaling the image.
EDIT
In my opinion the best solution would be to create your gradient for your background at runtime have a look at GradientDrawable

Categories

Resources