In my project i need to create the frames, lines and all the shapes required from the drawable folder. I could draw a rectangle and frame and all from drawable. But the problem comes when i need to create arrows from the drawable folder.
Please help me to draw the up arrow and down arrow as shown in the images in the drawable folder.
You cannot draw complex shapes currently with the builtin tools android has. ShapeDrawable lists all the possibilities you have for now.
Why don't you just create different sizes of the arrow in png (ldpi, mdpi, hdpi), and use those? That is the recommended way.
If you want to do custom things, you either should look into SVG, or draw your own.
Related
I'm trying to create a shape for Button that would have one side round and other right side slop , the actual image and a shadow.
I'm tried alot but i'm not understand how to draw it and how to draw shadow for same.
Thanks in advance.
Design the shape whatever you want in any designing tool, export it as SVG. Then, in android studio right click on "drawable" folder -> new vector asset, then choose the exported SVG file from your local disc.
Now you can able to set the created drawable as button background.
Here is the information about custom buttons. In a different approach you can use canvas and onClickListener.
I would like to include a bottom right triangle on the background of each item in a listview.
I can achieve this using nine-patch images:
But, there can be different possible colors, so I would like to do this with XML drawables, so that I can easily add more colors.
My questions are 2:
1) How I can draw triangles as XML drawables? (I have realized that the triangle is not one of the possible drawing shapes in Android)
2) In case I manage to achieve a triangle drawable, how can I force that drawable to say at the bottom right background? (with nine-patch images is pretty easy, but I don't know how to do the same with drawables)
1) If there is no need for scaling then you could simply take your ninepatch drawable and recolor it. For example if your base triangle had transparent background and the triangle was white you could do something like this to color it into red:
NinePatch ninePatch;
NinePatchDrawable drawable = new NinePatchDrawable(ninePatch);
drawable.setColorFilter(Color.RED, Mode.MULTIPLY);
2) Solved since you know how to do it with ninepatches
I am new to Java and Android programming and I am having trouble with my app. I have various ImageViews with their pictures set as PNGs from the res folder (using .setImageResource), I have another PNG, which is essentially a transparent square with a red border, which I would like to place on top of the ImageViews giving a highlight effect. I can't work out how to combine the two PNGs to give the desired highlighted look.
I have tried creating a new bitmap and drawing the highlighted image over the top of the ImageView's background but I couldn't work out how to convert the drawables to bitmaps.
Any help would be appreciated, thanks.
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?
after I found out (sadly) that there is no easy way to create a simple border around a View, I was forced to use the 9-patch-image approach to get a border around my View.
So I read the short thing there is on the 9-patch and its easy enough to understand. I started the "Draw 9-patch" application that came with the Android SDK, opened up a very simple image I created in Paint and then... I got stuck.
According to the page (http://developer.android.com/guide/developing/tools/draw9patch.html) I should be able to define those areas where the image can be expanded and so on, just by clicking on the edges ("Click within the 1-pixel perimeter to draw the lines that define the stretchable patches...").
So I try to do just that and absolutely nothing happens. I try to hold down SHIFT when clicking and also do the rigth-mouse-click, and nothing happens. When I move the mouse away from the 1-pixel perimiter I get a grey-and-red "marquee" (stripes).
Bottom line is: nothing happens no matter what I do.
The tool definitely works, but make sure the image you create first does NOT have the .9.png extension. If it does, the draw9patch tool will assume the 1 pixel perimeter of your image is already setup as 9-patch. If you open an image with just a .png extension, the tool will add the 1 pixel perimeter and you will be able to manipulate it as described, and save it as 9-patch when you're done.
The 9-patch tool is a bit awkward to use, but it does work. I believe you need to left click and drag around the edge to paint the outside border.
Alternatively, you can use any image editor and create a one pixel empty border around it, and draw in the following:
Top/Left = black pixels for the area of the image that can be grown or shrunk as needed. For a typical border, it is everything except for the curved corners
Bottom/Right = black pixels for the small snippet that should be repeated if the image needs to be grown in that direction.
You must use a non-8bit png file to create a .9.png file, or it can not be edit.
In my case, I can convert a 8-bit png to 9-patch file with Android Studio successfully, but I can't edit it anyway.
When I export the origin psd to normal png file, and convert it to 9-patch file, the 9-patch borders ban be edit now!