How to add faded edges to a rectangle drawable? - android

I currently have the following drawable which creates a rectangle with rounded edges, a semi-transparent black background and transparent edges (in case I want to show them later):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#android:color/transparent" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#50FFFFFF"/>
<stroke
android:width="5dip"
android:color="#android:color/transparent" />
<corners android:radius="30dp" />
</shape>
</item>
</layer-list>
My question is how could I modify this so that the edges of the rectangle's background fade, to give a softer appearance?

I did something like this for work once.
Just make a layer-list with multiple rectangle each with increasing opacity and padding
code below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#03101010" />
<corners android:radius="24dp" />
</shape>
</item>
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="rectangle">
<solid android:color="#06101010" />
<corners android:radius="20dp" />
</shape>
</item>
<item
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp">
<shape android:shape="rectangle">
<solid android:color="#09101010" />
<corners android:radius="16dp" />
</shape>
</item>
<item
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp">
<shape android:shape="rectangle">
<solid android:color="#12101010" />
<corners android:radius="12dp" />
</shape>
</item>
</layer-list>
The number of layers may vary depending on the color of your primary rectangle and the background.

Related

Android: Custom background - rectangle with a circle cutting in the middle

I'm trying to implement this view
As you can see it had a circle cutting to the left and right of the rectangle
I tried to use this code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp" />
<size
android:width="300dp"
android:height="200dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<item
android:left="-10dp"
android:right="290dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#00000000" />
</shape>
</item>
<item
android:left="290dp"
android:right="-10dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#00000000" />
</shape>
</item>
<item
android:left="10dp"
android:right="10dp"
android:top="50dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#EEEEEE"/>
</shape>
</item>
</layer-list>
but the result was not showing the cutting circle to the left and right
Is there any way to implement these cutting without loosing the shadow effect of the cart

Android drawable xml reverse corners

I'm trying to replicate the following shape in Android studio:
I can't figure out how to do it in android drawable xml.
At the moment I used a layer-list where:
As the first item I placed a transparent (or white) rectangle, to which I gave a padding on the left side and vertically.
As the second item I placed a rectangle containing the gradient and the corners on the left.
Now, I don't know how I can make the two "reverse angles" on the right, I've tried everything but online there aren't many examples to refer.
Here's my current code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/transparent" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="0dp"
android:top="8dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#color/green"
android:startColor="#color/blue"
android:type="linear" />
<corners
android:bottomLeftRadius="22dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="22dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
How can I change the code to obtain this shape?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="600dp"
android:height="400dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:bottomRightRadius="100dp" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp"
android:top="300dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:topRightRadius="100dp" />
</shape>
</item>
<item android:width="200dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
<item android:width="600dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
<corners android:radius="100dp" />
</shape>
</item>
</layer-list>
Here is a preview image
try this

How to draw 3 circles on top of each other that are each transparent differently?

I'm trying to create a layer of 3 circles that are all inner. And inside I will have a image of flowers.
The background can change...
If it sat on a blue background it will be blue shades. So I need to somehow adjust the transparrencies to make this work.
The center MUST ALWAYS be white completely though. I then want to add my image in the center.
I created a layer list to achieve this but I can't get the circles to be transparent and I can only get two circles to appear?
How would I get the bitmap to appear in the center?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Larger circle in back -->
<item>
<shape android:shape="oval">
<solid android:color="#726"/>
<size
android:width="35dp"
android:height="35dp"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#00f"/>
<size
android:width="25dp"
android:height="25dp"/>
</shape>
</item>
<!-- Smaller circle in front -->
<item>
<shape android:shape="oval">
<!-- transparent stroke = larger_circle_size - smaller_circle_size -->
<stroke android:color="#android:color/transparent"
android:width="5dp"/>
<solid android:color="#f00"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>
</item>
</layer-list>
Try this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#b5ed9259" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#cff27527" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#fbda5807" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#FFFFFF" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:drawable="#drawable/ic_fav"
android:gravity="center"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
</layer-list>
OUTPUT

Is it possible to have multiple borders on a shape?

I was wondering if it is possible to have multiple borders/"stroke" elements on a shape, or if I need to use an image (or a bunch of shapes covering each other). My code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:endColor="#color/editTextBG"
android:startColor="#color/editTextBG"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/editTextEdgeInner" />
<stroke
android:width="1dp"
android:color="#color/editTextEdgeCenter" />
<stroke
android:width="1dp"
android:color="#color/editTextEdgeOuter" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
I've tried just using the code, which didn't work, and giving the earlier strokes a bigger width (so that the later, if drawn over, would only color part). However, it seems the last stroke overrides the others?
A workaround for borders is to overlay elements:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/colorAccent"/>
</item>
<item android:top="8dp">
<color android:color="#color/colorPrimaryDark"/>
</item>
<item android:top="16dp">
<color android:color="#color/colorPrimary"/>
</item>
<item android:top="32dp" android:right="8dp">
<color android:color="#android:color/holo_red_dark"/>
</item>
</layer-list>
This is the result:
The first element in the layer-list from top to bottom is the background and every other is mounted on top.
<item>
<shape android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#color/colorWhite" />
<stroke
android:width="1dp"
android:color="#979797" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="#ffd400" />
</shape>
</item>

How to create border around whole layer-list?

I'm trying to create a border around the whole layer-list and not on the individual shapes in the layer list. I'm following the code from here:
http://gisinc.com/talk/creating-reusable-custom-toggle-button-android-applications/
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="#dimen/settings_toggle_circle_diameter">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
<item android:left="#dimen/settings_toggle_circle_radius" android:right="#dimen/settings_toggle_circle_radius">
<shape android:shape="rectangle">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
<item android:left="#dimen/settings_toggle_circle_diameter">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
</layer-list>
Basically I don't want a border around the individual ovals and rectangle but one border around the whole thing. Is there any way to do this?
I would like to suggest a different approach that I find easier...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<padding android:left="15dp" android:right="15dp" android:top="15dp" android:bottom="15dp"/>
<solid android:color="#color/primary"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#color/secondary"/>
</shape>
</item>
<item android:gravity="center">
<bitmap android:src="#drawable/logo_splash"
android:gravity="center" />
</item>
</layer-list>
Three possible options
Using stroke attribute of Shape and some adjustment in logic
User Inner/Outer(border) shapes
Use Nine patch
I modified your code to show border
<?xml version="1.0" encoding="utf-8"?>
<!-- I have assumed 1 dp padding around all the shapes-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First Draw Left Outer circle(white) -->
<item android:right="22dp" android:left="0dp" android:top="0dp" android:bottom="0dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
<size
android:height="22dp"
android:width="22dp" />
</shape>
</item>
<!-- Draw Left Inner circle(black) -->
<item android:left="1dp" android:top="1dp" android:bottom="1dp" android:right="23dp">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Right Outer circle(white) -->
<item android:left="22dp" android:right="0dp" android:top="0dp" android:bottom="0dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
<size
android:height="22dp"
android:width="22dp" />
</shape>
</item>
<!-- Draw Right Inner circle(black) -->
<item android:left="23dp" android:right="1dp" android:top="1dp" android:bottom="1dp">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Rectangle Center (black) -->
<item android:left="11dp" android:right="11dp">
<shape android:shape="rectangle">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Rectangle Top edge(white) -->
<item android:left="11dp" android:right="11dp" android:bottom="21dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
<!-- Draw Rectangle Bottom edge(white) -->
<item android:left="11dp" android:right="11dp" android:top="21dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>

Categories

Resources