Is it better to have separate image in each drawable folder (hdpi, ldpi, ...) or use 9patch drawable?
I want to use 9patch drawable for different screen size but I am not sure which is better. I know that 9patch drawable is better because it we can use only one image instead of several image in each drawable folder.
It seams like you're misunderstanding the intent of Nine-Patch Drawables:
A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds.
There's no correlation to the screen density. If you're not using very simple graphics, you might need to add the nine-patch files to each drawable folder anyway.
Related
Reference the image attached. How to set such an image as background?
Does using 9-patch makes it possible?
Yes, Please Use 9-Patch image and Please check this on how to draw 9-patch image and also this Video Tutorial
Refer to your ldpi, mdpi, hdpi, xhdpi, and xxhdpi folders there you will have to format your image to fit the particular sizes of each screen. Another way of doing it would be android:setbackground="#drawable:example"; in your xml and testing your app out in the emulator on all the different screen sizes to see if it really matters that much due to the simplicity of your image.
It would be much simpler to create a high res .png file in your case. and might end up working better (just a thought)
If attached image is the only case, then you could try and create your own drawable by extending Drawable class.
It's just a rectangle with circles cutout on each side. This can be easily drawn using drawRect and drawCircle. This way you'll get perfect quality on every screen.
Is it possible to keep the colors of the bitmap in a stretched imageview sharp if this bitmap contains only the colors black and white?
Currently what i get is the bitmap with blurred edges.
I think it would make sense to keep the sharpness of the colors without resizing the bitmap
I know this question is old, but :)
You should provide screenshots with your question so we can see what you're talking about. Anyway, drawables in Android aren't scaled very well so it is normal if you get some blurry effet on a resized bitmap.
You should use the different drawables (drawable-hdpi, -mdpi, xhdpi, ...) folders of your project to provide different versions of your images to the system. The right resource will always be chosen depending on the device screen.
I would like to create Drawable that contains nine-patch and a Bitmap.
I put them together in Layer List Drawable, but it turns out that Bitmap is extending nine-patch beyond the edge.
Is there any way to "cut" Bitmap so it won't be overlapping nine-patch?
EDIT: Actually it seems that I would like to shrink background to wrap content and ingore big bitmap in background.
a nine patch by definition doesn't have a size.
From the sounds of things you haven't configured your nine patch correctly.
Please attach an image of what you're seeing and your source files.
I use several nine patch images in my layout, for buttons, text fields and form backgrounds. All of the xxx.9.png files are in the drawable-hdpi folder, just like the regular images, yet they still look pixelated.
The ImageView at the top has a regular bitmap and the lower ImageView uses a Nine Patch bitmap. You can see that the Nine Patch bitmap is scaled to a lower density. How do I fix that?
You should put 9 patch images in the nodpi folder, unless you want the non-stretchable area to be scaled/stretched also.
Ah, it was messed up because I was calling canvas.setDensity( 480 ) somewhere in a custom view. That caused this problem.
I have a widget with an ImageView on it. I set this ImageView to a Bitmap created from a 9-patch PNG resource. The image is set correctly but is not stretched correctly - i.e. the whole image is stretched instead of just part like defined in my 9-patch PNG. If I just set the 9-patch image as a resource, it works. How can I fix this?
// Does not work (9-patch does not display correctly)
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.someNinePatch);
remoteViews.setImageViewBitmap(R.id.someImageView, bitmap);
// Works (9-patch displays correctly)
remoteViews.setImageViewResource(R.id.someImageView, R.drawable.someNinePatch);
Android knows a PNG is a nine-patch by virtue of the .9.png file extension on the resource. I do not know of any way to use nine-patch images outside of resources, because Android will not have the file extension and will not know the image is a nine-patch.