Android: Lossless quality image scaling - android

Is it possible in Android to use the Vector images (for example, contours made in Adobe Illustrator) to be drawn on Canvas?
I looked at Vector graphics in Android but I don't want to use any additional third-party libraries.
So, is there any another way to make this idea?
One thought, that came to mind, was to convert to the 9patch images. But still I'm not sure whether it sounds good.
All of fuss just over the aim to make the complex countour from which I want to create android.graphics.Path using public void addPath (Path src, Matrix matrix) function.

You cannot use vector images in Android, at least with the built in SDK.
It may exist libraries to use vector images, but this is not the Android best practice.
What you need to do in order of not pixelating/blurring your images is to use the different drawable folders existing in android.
You should provide different images depending on the screen density of the display, and android will pick the correct one in runtime.
http://developer.android.com/guide/practices/screens_support.html
9 patches are good only if the image is meant to stretch to fit its content, while its borders should repeat following a pattern. The typical use of this are buttons. For images that dont work as the background of a View, 9 patches are not a good option.

Related

Which kind of images is appropriate for VectorDrawable?

VectorDrawable is a new feature for Android after API Level 21, Which add support for vectorgraph. But I have a question for VectorDrawable, is it suitable for me to replace most of images in my project to VectorDrawable. In the android offical dev site I saw a few words :
A vector drawable is appropriate for simple icons. The material icons provide good examples of the types of images that work well as vector drawables in an app. In contrast, many app launch icons do have many details, so they work better as raster images.
Is that means VectorDrawable is only appropriate for simple icons like offical material icons, images have many details aren't appropriate to use VectorDrawable.
Sorry for my poor english skills, hope you guys can understand me! 😬
I plan to use VectorDrawable for all my project image resource if it is ok.
You're basically right.
Vector images describe shapes and geometry, and need to be rendered into bitmaps (a grid of pixels). This requires some math, calculating the pixels that represent the lines and curves defined by the icon.
The more complicated an icon gets, means the more shapes that are required, and the more calculations that need to be done.
On the other side, if you already have the icon rendered to a specific scale, like with png images, all of the pixel values have already been calculated. Now it just needs to be converted to a bitmap and scaled to the size it gets displayed at.
So depending on the situation, an icon may be able to decode and scale from a png file faster than rendering from a vector drawable, or vice versa, it all depends on the icon.
It's hard to say exactly how simple an icon needs to be, so try it out and make sure it runs well on a range of devices.

How can I manipulate and display SVG graphics on Android?

I would like to draw technical illustrations in my Android app.
I have images with shapes, and I have to change the size of those shapes based on input data. Thought that I could save those images as SVG, then I could manipulate the size of the shapes in it. For example, I would like to take a with a specified ID, and change its width/height.
There're great SVG display libraries for Android, but I haven't found one which can also manipulate the graphics.
Do you know any library which can do this?
If not, how else should I draw my illustrations?

Android background bitmap crop

I have a background png in my Android application. I would like to support lots of displays, but I have this one obstacle - many displays have many resolutions and many ratios. I would like to make sure my background is displayed properly and make it more ellegant, than just creating 10+ cropped png files in Photoshop.
My idea would be - a fairly large picture imported in the project. The app would find out screen dimensions and simply say starting points(x,y) and ending points, that would "crop" the picture and display it without any deformations.
Is there a way of doing it?
I think bitmap.createBitmap() is the method your looking after. The method simply lets you cut out a defined portion of the bitmap.

Android OutOfMemory exception - Is there another approach for my needs?

My app is loading a large image (a house floorplan), then drawing touch-reactive objects (furniture, lamps etc.) on the image. I have a base image file included with my app but the objects come from coords in my database.
I've successfully deployed the app in multiple iterations, but now I need to use larger base images and BitmapFactory is causing an OutOfMemory exception on many devices (both old and new devices; anything with < 32MB heap seems to crash). I've read the 157 OOM questions on SO, but I'm afraid the link they all seem to point to won't help me since resolution / zooming is critical to the app's function.
I've tried to test the device's available memory before loading, but the results are spotty at best (some devices like the galaxy S3 report plenty of heap but still crash). I've also tried decreasing resolution but the image became unusable when reduced to a safe size based on the above test.
Is there another way to implement this design without ever using bitmaps?
I need to:
Load large base image
Create clickable shapes on top of the base image, maintaining their position / scale relative to the base image
BONUS: in the iOS version of my app, I can do SVG-style text scaling so a long label on a small object will stay inside the object
instead of running across the map(and will be invisible until the
image is zoomed). Replicating this in android would make me a happy
code monkey.
I can post code if needed, but you've all seen it before (almost all of it came from SO).
Thanks in advance for reading, and for any help you can give.
you have a few options:
break your large image into tiles, load these tiles into an array, and move a camera object around and only load tiles that need to be drawn, as the comments suggest.
make your image small and scale it up using 'android:scaletype`
Draw lines and curves on a Canvas object at runtime.
Use OpenGL
The appropriate solution really depends on how you want it to look. Tiling will take more dev effort but will look better, just be careful that you are properly cleaning up any tiles that aren't being drawn...
dynamically scaling will be easier, but you cannot guarantee the image won't be blurry.
Drawing on a Canvas object at runtime could work well-- just use Lines of different width and circles and Rects etc.
Using OpenGL will have the steepest learning curve, and might be overkill. This depends on your purpose.
You might like to look into using a "largeHeap"
http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap
Here are some options:
1) Use tiles. Use tiles and dynamically load your data. Honestly, this is the best solution. With this solution you can load arbitrarily large images.
I've successfully used this approach for an endless paint canvas and it works quite well. You really only need to draw what is directly visible to the user. Tiles is a way to cast away pieces you don't need. A pyramid of tiles (where you pre-downsample your images and create more tiles), allows you to do this in a clean and fast way.
2) Use native code. The memory restrictions on native code are not the same as Java code. You can get away with allocating more memory.
3) Use OpenGL. Once again, the memory restriction for OpenGL are not the same as Java code.
4) Convert your original plan to an SVG and use an SVG library like this one.
5) Use "largeHeap". I strongly discourage this, as I think a largeHeap is rarely the solution, there are generally cleaner ways to approach the problem.
if the image is static , you might wish to use this nice library:
https://github.com/ManuelPeinado/ImageLayout
if the library doesn't support auto-downsampling of the image, you should do it by yourself, in order to use the best image for the current device (so that you won't get OOM).
for auto-sizing text , you might have some luch with the next post:
Auto-fit TextView for Android

Using vector images to scale for app platforms

Images in apps come in many shapes or sizes, but to save space and editing time is there a way to use scaleable vector images?
Ideally I would have one vector image at middle resolution, I could then detect the screen size and scale the vector how I need and add the background using some custom gradients.
I'm using titanium for this.
Titanium doesn't yet support vector graphics, though it is available in native Android code via Shape Drawables. There is a third-party SVG library available for Android SDK.
For Titanium, branch the code based on the device screen size (Titanium.Platform.DisplayCaps), and find an image that works with decent performance on the device.
You can use PNGs with transparency and apply a background color to your view object.
I've found away round making different sized drawable:
Basically just one have folder called drawable within the res folder.
Make your artwork in what ever you use but make it large (at least 1080p for future devices).
Save the image's as PNG within the drawable folder but save them large. (IE at least 1000x1000)
Write a function that loads in the PNG but scales it (according to screen size & percentage of what size you want the drawable to be. So 20% of 800px width is 120px). I've managed to do this bit with 30ish lines of code, can't paste my code since I'm not on my working machine.
For me this has worked across all my apps for all devices, I've not had a single crash yet (1000's of installs, including Live Wallpapers).

Categories

Resources