Why is this a bad PNG for 9-Patching? - android

It seems every attempt I've made at creating a 9-Patch file has created a situation where the 'Content' in the Draw 9-Patch is always 'Bad'. Here is my latest (and very simple) PNG that is shown as all bad pixels:
Could I please be enlightened as to why this is 'bad'? And how am I suppose to know what is good or bad if Android does not explicitly state any criteria in the SDK docs?

This PNG is fine. What you're supposed to do with the draw 9-patch tool is to select the areas which are stretchable. Don't select the two pixel area where your two colors meet. Here's the 9-patch I created from your PNG:

It looks like your image does not have a 1-pixel border all around defining the stretchable area and (optionally) the content area. This border is a requirement. The criteria for 9-patches is discussed in the document Canvas and Drawables. There's also the Draw 9-patch tool to help you draw 9-patches.

The colormap of you bitmap is wrong:
correct.9.png: PNG image data, w x h, 8-bit/color RGBA, non-interlaced
buttonbg.9.png: PNG image data, 6 x 21, 1-bit colormap, non-interlaced

Related

Delete padding or Force fitting or Delete empty space from square Vector asset to Fit a Rectangular button?

I'm trying to adjust the all_inclusive svg image to my rectangular button. The shape itself is rectangular as well but the vector asset is square (24x24) with white spaces above and under the shape. These spaces force the shape itself to be very small. How to make the all inclusive svg rectangular by deleting that padding on top and on bottom?
In this picture the image is set to fit the guidelines on the left, top and right side:
<ImageView
android:id="#+id/imgInfinity"
app:srcCompat="#drawable/ic_infinity"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="0.75"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="0.75"
app:layout_constraintEnd_toEndOf="0.25" />
Things that did not work:
pivot vector asset with a group -> I just cant figure out the dimensions without messing up the original shape. Same storty with scaleX/Y or translateX/Y. I got it to work on my other buttons with simpler shapes though.
adjusting android:viewportheight or android:height -> it deshapes the picture to a weird form
crop svg online --> as Googles original SVG pathData is already 580 characters long, cropping tools only make it to large for android to deal with (above 1000 charactes)
crop svg picture with word and extract from zip file-> it doesnt compress svg images so it stays rectangular with the white spaces above and under.
Set a seperate horizontal guideline for the top of the picture. It does the trick but one or multiple guidelines for each image gets very messy. There must be a better way, right?..
ACCEPTED SOLUTION (edit with InkShape):
Install InkShape
Open SVG
Click on picture once to select it
Go to File-> Document Properties and click 'Resize pager to drawing or selection' (this button is hidden on the first tab, click +Resize page to content to show the option);
Save
extract pathData and (viewport)width/heights from saved file.
The viewportHeight attribute defines the size of the "canvas" that the path is drawn on (i.e., it defines what the coordinates in the path data actually "mean").
The height attribute defines the intrinsic size of the drawable.
The original vector has 6.5 units (in the viewport) of white space at the top and the bottom. That means that you can look for any pathData command that uses a capital letter, and subtract 6.5 from the y coordinate. That leaves you with this:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="13dp"
android:viewportWidth="24.0"
android:viewportHeight="13.0">
<path
android:fillColor="#FF000000"
android:pathData="M18.6,0.12c-1.44,0 -2.8,0.56 -3.77,1.53L12,4.16 10.48,5.5h0.01L7.8,7.89c-0.64,0.64 -1.49,0.99 -2.4,0.99 -1.87,0 -3.39,-1.51 -3.39,-3.38S3.53,2.12 5.4,2.12c0.91,0 1.76,0.35 2.44,1.03l1.13,1 1.51,-1.34L9.22,1.7C8.2,0.68 6.84,0.12 5.4,0.12 2.42,0.12 0,2.54 0,5.5s2.42,5.38 5.4,5.38c1.44,0 2.8,-0.56 3.77,-1.53l2.83,-2.5 0.01,0.01L13.52,5.5h-0.01l2.69,-2.39c0.64,-0.64 1.49,-0.99 2.4,-0.99 1.87,0 3.39,1.51 3.39,3.38s-1.52,3.38 -3.39,3.38c-0.9,0 -1.76,-0.35 -2.44,-1.03l-1.14,-1.01 -1.51,1.34 1.27,1.12c1.02,1.01 2.37,1.57 3.82,1.57 2.98,0 5.4,-2.41 5.4,-5.38s-2.42,-5.37 -5.4,-5.37z"/>
</vector>
Then, once the whole shape has been moved "up" by 6.5 units, you can subtract 11 (6.5 * 2) from both the viewport and the intrinsic height.
The end result is a 24x13dp shape, which should scale much better in wide views.
Update using Inkscape version 1.1:
Unfortunately, the latest version of Inkscape (1.1) no longer will import a vector drawable file directly, so the original answer is not 100% correct. That answer will probably work with other editors that can handle vector drawable files.
Here is an update to that answer that works with later versions of Inkscape to remove all padding from a vector drawable.
Convert vector drawable to scaled vector graphic (SVG):
Open Alex Lockwood's Shape Shifter site
Drag the vector drawable file from Android Studio to Shape Shifter.
Export the image as an SVG to a local file
Now that we have an SVG file, we can edit it with Inkscape:
Install Inkscape if not already installed.
Open SVG file in Inkscape.
Click on the image to select it.
Resize the image to the selection (Shift+Ctrl+R or Edit->Resize Page to Selection). You can also specify an alternate size if you desire some padding.
Save the image as an SVG file.
The image is now cropped in an SVG file. We need to convert it back to a vector drawable.
In Android Studio import the SVG file as a vector drawable. (File->New->Vector Asset) Asset Type = "Local file (SVG, PSD).
Once imported, the vector drawable no longer has any padding.
Use an image editor that can handle SVG files to crop the image. I used InkScape but there are others. Once the image is cropped, you can import it into Android Studio as an XML file.
Here's the new update on this topic:
https://code.google.com/p/android/issues/detail?id=202019
It looks like using android:scaleType="fitXY" will make it scale correctly on Lollipop.
From a Google engineer:
Hi, Let me know if scaleType='fitXY' can be a workaround for you , in
order to get the image look sharp.
The marshmallow Vs Lollipop is due to a special scaling treatment
added into marshmallow.
Also, for your comments: " Correct behavior: The vector drawable
should scale without quality loss. So if we want to use the same asset
in 3 different sizes in our application, we don't have to duplicate
vector_drawable.xml 3 times with different hardcoded sizes. "
Even though I totally agree this should be the case, in reality, the
Android platform has performance concern such that we have not reach
the ideal world yet. So it is actually recommended to use 3 different
vector_drawable.xml for better performance if you are sure you want to
draw 3 different size on the screen at the same time.
The technical detail is basically we are using a bitmap under the hook
to cache the complex path rendering, such that we can get the best
redrawing performance, on a par with redrawing a bitmap drawable.

How to create drawable in xml, with inverse rounding?

I need to create in xml next drawable:
How can I achieve this?
I only know how to make right-top rounding - but what about bottom-right inverse rounding?
You should use a nine patch image to do this.
https://developer.android.com/studio/write/draw9patch.html
Example tool:
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html#&sourceDensity=320&name=example
A nine patch is a png image with black pixels in the borders to delimitate the area that has to be extended. The format is example.9.png, .9.png is the extension.
A little tutorial, you can find others:
https://www.youtube.com/watch?v=wLimJf-EIl8

Android UI Images Sharp Edge

I've been having problems with large images being resized for UI use in Android.
Look at this image, it's an ImageView:
The original image (That arc is a progressbar) is around 10 times bigger than what you see here. In UWP (Windows Platform) we had no problem using a very large image, but here in Android, I beleive it's the Nearest Neighbour method used for fitting images into UI elements, which as you see, causes sharp edges.
Is there any way to switch it into another method? Like Bicubic? It happens in all Android versions I've tested (4.1, 5.0, 6.0).
Just to mention, I'm using Xamarin 4, which I don't beleive as a contributing factor here.
No luck searching through the internet, I'm afraid I'm the only one having this problem.
Thanks.
As mentioned above, you should prefer to use vector image instead of pixel image.
But if you have to use pixel image, maybe you could use BitmapRegionDecoder to decode lines of image and write your own resample algorithm(like Bilinear Interpolation, it's much better than the Near Neighbor) to resize the image, typically in JNI side.
Another possible way is to use "filter" parameter while calling Bitmap.createBitmap method as your original image would not cause OOM issue, just set it to true, it works to reduce the artifacts.
You should use Vector Images instead of Bitmap Images.
Bitmap x Vector
A bitmap represents an image by a series of colored pixels. Whereas a vector image is represented by geometric shapes (lines, curves) using colors.
The main utility of a vector image is allowing to scale without losing definition.

Keep sharpness of bitmap colors in stretched imageview

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.

Layer List Drawable - clip bitmap by nine-patch?

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.

Categories

Resources