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.
Related
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.
I have one big image named panel_bck.jpg to use in different resolutions to decrease apk size. The image format is JPG and it's size is 1.9MB. I put it to default drawable folder only. But when I set the background in code, the BitmapFactory can't find the image at
drawable/panel_bck.jpg
and the result is all black background. I tested this case with other images(PNG files, JPG files) and there is no problem with them. Only question is that a limitation exist for the drawable size or what is the problem? How can I solve this?
If your image very big resoulation you will set drawable-xxhdpi directory only.
I had a problem like yours trying to maka a big image fit into a too small ImageView and I fixed my problem by using this line in the layout xml file:
android:adjustViewBounds="true"
I don't know if it can ba applied to an activity background though.
You should use 9patch image. You can create 9patch image by using the tool called draw9patch located in sdk/tools. First create a large resolution image and give to draw9patch it will create a 9patch image with extension .9.png add that image in drawable it will solve you problem.
See following links for detail
http://developer.android.com/tools/help/draw9patch.html
http://codesignature.com/how-to-design-9-patch-buttons-for-android-using-adobe-photoshop-for-all-pixel-densities-and-states/
I use the following statement to copy an image from the assets to the gallery app, which works fine:
MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, myTitle , myDescription);
The images are png-files with a transparent background. They are displayed correctly when I load them from the assets to, for example an ImageView.
The problem is, that the formerly transparent background of the imported images became solid black in the gallery.
The png is a png24 created by gimp. I also tried a transparent gif and a png with transparency added by Apple's preview application with the same result.
Why it happens?
I know this is an old question but I just had the same problem. The problem is that MediaStore.Images.Media.insertImage stores the file with a MIME_TYPE of "image/jpeg", and jpeg doesn't support transparency.
One solution is to make your own content provider that uses another image format. The Picasso image library might be worth a look too.
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'm displaying an external image in an image view by fist downloading it like this:
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
Then setting this bitmap to the ImageView like this:
imageView.setImageBitmap(bitmap);
This works all good except that one of the images is a PNG and I lose the transparent background when using the BitmapFactory.
Can anyone tell me how I can keep the transparent background?
not sure if this will help, but try following this advice and adding Options to make sure that your image is pulled in as ARGB_8888
http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String, android.graphics.BitmapFactory.Options)