I have a detailed background that has a hole in it (transparent area in the middle). The background is detailed png image.
Now I want this hole to be shown on all devices equally. So I thought of 9patch, but then the details get screwed of the image. If I don't use 9patch, the hole appears as an egg, which is not the point.
What is the way to fix this?
update
I used 2 different methods (depending on what is better in the excact case)
-different resources for different screens.
-Disadvantage: will increase you app size significantly
-Advantage: This is quit convenient for you to use
-Extend Drawable and give your Background and a alpha map (Alphamap in your case would be a ninepatch with 0xffffffff ouside and 0x00ffffff as color for your circle) over with the constructor.
Override draw(). Create
ComposeShader(
drawable1,
drawable2,
PorterDuff.Mode.MULTIPLY);
Set the shader to a paint and draw it on the canvas.
-Disadvantage: Not so convenient
-keeps you app size small :)
Related
I have an image in my assets folder on which I am drawing stuff using an external program and then using them in my app. The problem is that the bitmaps are blank (transparent) with black and white objects in them. Note that the objects are created with Anti-Aliasing on to look better. I know this was asked before but I couldn't find what I want. I need to replace all the black and white pixels in the image (even the transparent anti-aliased ones!) to the colors given by the user. Below are some images to show what I want to do:
Please note that this is just an example and I have even some very complicated shapes and the final colors aren't known (as inputed by the user in RGB style).
Any help is appreciated. Thanks! :)
Usually you can tint the images you load apliying them a color at runtime.
The problem is that color applies to the whole image and it only matchs exactly the same color in white pixels, with or without alpha.
So you could separate all the areas of the image with the same color, save them as white and then tint them at runtime while overlapping one over another.
It depends on the framework you are using.
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.
So I have this image set as the background of a seekbar. On different screens, the image gets skewed so much that it is quite horrible. I tried doing a 9 patch, but obviously there are several regions I would like to scale so 9 patch wasnt the best option. What I would like to acheive is, have the background stretched and leave the numbers proportional. The numbers tend to eith stretch too much or shrink too much.
Any help is appreciated.
Thanks
The simplest option here is probably to create a GradientDrawable to set as the background of your view. You can see from the docs that GradientDrawable can take an array of colors in its constructor, and it will draw all those colors evenly spaced across whatever size the enclosing view end up to be. This is the same element used to create <shape> drawables in XML, but if you instantiate it in Java code you can add more than just three colors to the gradient scale.
How do i remove white borders around an image when used on different backgrounds?
http://groups.google.com/group/android-developers/browse_thread/thread/f4ef24e03431bbe4/ce7f19488fd0388a?lnk=gst&q=Crop+Image+View+that+fits+to+the+image#ce7f19488fd0388a
if your trying to use it as the main screen background then set its layout weight and height to "fill_parent" then the background can be any image you want it to be ref just like the SRC call you made. That would let the image fill the screen only and give you access to write on the screen with the SCR command. If you give us more of what your doing we can help more.
OK, I have seen the image and you want the ImageView to be of the same shape as your image because you don't want to see the white border around your image.
Well,in short, I don't think that is possible AFAIK. ImageView is a rectangular view which asks for height,width.
However, rather than try to do the impossible, I would suggest you to use alpha-masking( subset of alpha blending, google them to know more).
A little theory: depending on the alpha bits for each pixel in your Bitmap (the translucency bits), the extent of blending of that pixel with over-written pixel is determined. Considering extremes, if alpha is 255, overwriting pixel is used instead of the over-written one (fully opaque, in regular terms); if alpha is 0, overwriting pixel is just ignored (transparent). For other alphas in between: there is blending.
In your case you would have to make the alpha of the border zero, to achieve complete blend. Outlining the steps:.
The drawable to be used can't be a JPEG( JPEG doesn't store alpha values per pixel). Go for PNGs
2.You will need to create and keep your bitmap drawable in that way beforehand(use google for alpha blending PNGs) such that the borders have zero alpha value ( use softwares like Paint.NET for ex).
If images are being created dynamically, you would need to use a blending equation ( for details read materials by Porter and Duff).
For ex, this is PNG image with transparent pixels having alpha 0 other than the alphabet itself
.
Above is the alpha blended PNG on colored background of an Activity. I hope this is what you really want to achieve, and my answer is actually useful .
Which is better to use in Android and why?
Load a .png file that is 1x1 and has, obviously, one pixel of one color (ie #000)
Use an .xml drawable that contains a solid rectangle of the same solid color (#000)
Which is better to use in Android and why?
Use an .xml drawable that contains a solid rectangle of the same solid color (#000).
Why? Because that's the point of the XML layouts, is to enable developers to quickly make changes to visuals (in this case colors) without having to open image-editing software.
If you were to really only load a 1x1 PNG then some could argue the PNG loading is faster, but in reality, this is never going to happen. The point here is that it will be stretched or manipulated in some way, or set to tile as a background and this will eventually be slower than defining a background color or shape in Android XML Layout.
For a single color, use a color resource. http://developer.android.com/guide/topics/resources/more-resources.html#Color This will be more performant than a 1x1 stretched image or an xml drawable.
As general performance guidelines, solid colors are the cheapest, followed by unstretched bitmaps, followed by 9-patches and stretched bitmaps. Shape drawables are usually the most expensive at runtime.
i think that png is better, because is native and take less time to load.xml is needed parser, so is a little low than png. i think, but i never tried it.
UPDATE
i make a files with these specifications, and the results are those:
png 1x1 black dot [119 bytes]
xml rectangle blac [261 bytes]
UPDATE 2
Performance wise, what is typically better, using an image or a xml created shape as a drawable?