Android XML Drawable: Rounded Rectangle With Inner Edge Gradient - android

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:

Related

Rounded Gradient Shadow

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

Android ClashRoyale-like border animation

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!

Draw rectangle with empty partial fill

Here's what I want to do: Click
I have a progressBar and I want to overlay this onto it so that I can achieve rounded corners. Problem is, if I use a 9patch image, it doesn't scale down, and scaling up pixelates the corners. Also, the corners when I increase the size of the progressBar, don't look sharp enough.
So I thought maybe drawing such a rectangle on top would make it 100% precise with crisp quality. Unfortunately, I've never used that before and there's no tutorial similar to what I want to achieve.
Thanks for all the help.
If you apply a shape drawable, you should be able to achieve what you want. You reference it just like any other drawable (android:src/android:background).
Let's name this file rounded_rect.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#80ffffff" />
</shape>
The radius can be more or less than 20dp, it will actually scale down if the container that it's drawn in requires less for the sides to be perfectly rounded; the color is white with a bit of transparency (#80).
Save it to your drawable folder and refer to it as you usually would with any png/jpg/etc drawable:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/rounded_rect" />

Round ring button in Android

I want to create a button background (or button itself) that look exactly like the below one.
I'm doing it currently with an image. I tried to create a similar one using the following XML, but it doesn't look as expected.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#f0600000"/>
<stroke
android:width="10dp"
android:color="#FFFF6666"/>
</shape>
Actually I want a round button with 3 pixel shadow and 1/4 width stroke in red color around the white circle. I have not succeeded with the shadow part at all. Thanks for any sort of help.
Well, the solid fills the middle and stroke paints the border. Since you want 3 colors you will have to use 2 drawables, painting white circle over the red circle with grey border. You can then use LayerDrawable to keep these as one unit

How do I create the equivalent of a css outset border in Android?

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?

Categories

Resources