Can you help me with a solution for custom color shadow with gradient effect?
I need this effect for ImageView shadow
For example, I try to use gradient like:
<gradient
android:type="radial"
android:startColor="#000000"
android:endColor="#43bababa"
android:gradientRadius="100dp"
android:angle="270"/>
But this gradient has ring effect, not rounded corners.
There's problem in every possible solution:
About shadows:
You can use CardView. It has rounded corners and shadows out of the box. But as I know compat realization doesn't cut corners of inner layouts. It just fits image in it (as result tiny white strokes around image example)
You can use 9 patch image as background. Not so easy to place layout layers correctly
SOLUTION YOU WILL LIKE:
Use CardView with RoundedImageView
Related
I'm currently drawing a simple rounded rectangle using an XML drawable (black BG for contrast):
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white"/>
<corners android:radius="15dp" />
</shape>
What I would like to achieve is adding an inside outline/glow that is a gradient so that it looks like this:
I know it will most likely involve several XML gradient elements in a layer list, but where I get stuck creating this is figuring out how to:
Have the gradient follow the curves in the corners since they are rounded (always face towards the center of the rectangle, or as close as possible)
Avoid the effect having double intensity in the corners like it is in the example. This will happen if 4 gradients are simply overlapped or a radial gradient stretched to a ellipse is used. The density of the outer color needs to be consistent all the way around the rectangle, or as close as possible
It is highly preferable if feasible that the implementation is modifiable so that:
I can control how far into the rectangle the gradient reaches
I can control how quickly the gradient fades at any given distance (control its density/intensity)
I can remove any side from the gradient so that I can have only 1 or 2 edges and their corners glow if needed
This is a perfect example of what I need except that it is obviously much simpler to do with a circle and a radial gradient. I need this consistent pattern but with the rounded rectangle:
I am trying to achieve this animation here:
I want to do something similar with a RelativeLayout having a rounded corner background:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="5dp" />
... solid color etc code here ...
</shape>
I am trying to think of ways to animate it. What I have thought of is probably using 4 translation animator to animate in the 4 directions, finding the 4 corners of the RelativeLayout position. The shape of the animation got me thinking a little too. Initially I thought of achieving it with a 1dp height rectangle shape, but it will not have the sort of sharp edges like the animation above. Can i achieve that kind of shape without using an image?
Any help pointing in the right directions as how to achieve this kind of animation will be greatly appreciated!
I have a TextView, to which I want to add a solid color background. I want this background only have the borders fading to transparency. Like this picture here, but instead of a photo, it's only a solid color. http://i.imgur.com/zAoazUy.png (Sorry I cannot post images yet)
This TextView is dynamically generated so its size can vary. The gradient has to end to transparent.
Is this possible? I fiddled a lot with XML Drawable ressources but got to nothing remotely close to what I want.
Thanks in advance
PS: (Picture stolen from Draw transparent gradient with alpha transparency from 0 to 1)
You have to define gradient in drawable folder ,startColor can be any color of your choice but for endColor use argb value to get transparent effect at corner
grad.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:gradientRadius="250"
android:startColor="#0000ff"
android:endColor="#64ffffff"/>
</shape>
and then simply use in textView
textView.setBackground(getApplicationContext().getDrawable(R.drawable.grad));
If you create an outset border in CSS the browser varies the border colour for each edge to make the shape appear to protrude from its parent.
Is there an easy way to do this in an android layout or do I need to set each line colour manually?
Update - added example below:
Example http://www.witzelsucht.co.uk/googleplusheader.png
Set the background of an ImageView to a 9 patched drawable resource with the desired shadow/bevel around the edge. Lets say it takes 5 pixels to create the effect you want. Then set the padding of the ImageView to 5 pixels. Then set the bitmap to any image.
ImageView.setBackgroundDrawable
ImageView.setPadding
ImageView.setImageBitmap
Even easier, use a shape with a stroked border and set it as the background of the view:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000" />
<stroke android:width="1px" android:color="#ffffff" /></shape>
I'm not aware of any native support for Views to have borders. Your question reminded me of this question that I was looking at recently:
Is there an easy way to add a border to the top and bottom of an Android View?
I have a specific drawable that I use as background in my app. It is not a solid color.
Now I want to add rounded corners to this drawable.
I only found the rounded corner available in a shape with a gradient or a solid color as a background but not another drawable.
Is there another easy way of adding rounded corners to a drawable?
Use AQuery to make Drawable or Downloaded images rounded corners.
http://code.google.com/p/android-query/wiki/ImageLoading
Note : This is very effective in doing asynchronous task with images and provides lots of featres.
Download API from here : http://code.google.com/p/android-query/downloads/list
Code to do rounded Corners of an image :
AQuery aq = new AQuery(this);
// Image URL to download
String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";
ImageOptions options = new ImageOptions();
options.round = 15;
aq.id(R.id.image).image(url, options);
Could you accomplish what you want using a "layer-list" drawable? It allows you to combine a shape and a graphic drawable like so:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_home_button_img"/>
<item android:drawable="#drawable/icon_home_shape_overlay"/>
</layer-list>
In this case, shape_overlay is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#60000000"/>
<stroke android:width="3dp" color="#ff000000"/>
<corners android:radius="10dp" />
</shape>
I'm using this to get a "selected" effect on an image. It works for me, though, because the image in question is an icon and the background is the same color as the containing view's background (both are white).
The other option I see is to modify the drawable so it has rounded corners.
For the case where I needed it I got it working with a shape that was filled with a gradient and had round corners. This looks a bit like the effect I wanted to achieve.
If you have a Texture or some other complex drawable that you can't build with shapes it seems you need to add a drawable with the rounded corners to your project. Or build the round corners yourself like it is explained in this question.
You could create a Nine-patch file with your drawable. With the center matching your current background and the edges and corners rounded, as needed. The Android documentation shows how to create Nine-patch files and you may be able to use the Draw nine-patch tool to build it.
This is how the SDK makes rounded-edge, gradient background styles for popup boxes and such.