Android ImageView radial gradient similar to a spotlight? - android

I'm trying to make my imageview have a radial gradient background that goes from solid white to a transparent white on the outside. Here's my SHAPE definition that I use as the ImageView background. The problem is that I end up with a solid white circle for my background, not the semi transparent edges like I want:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffffff00"
android:endColor="#ffffffff"
android:centerX="50%"
android:centerY="50%"
android:gradientRadius="50%"
android:type="radial"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>

Ok, I figured it out. I had two errors: the first being that percentages are not supported in gradientRadius and the second being that the first two places in startColor and endColor are the transparency values. It's AARRGGBB not RRGGBBAA. Here is the fixed Shape definition:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffffffff"
android:endColor="#00ffffff"
android:gradientRadius="36"
android:type="radial"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>

Related

How to design color boxes in android as shown in the image?

How can we display the colors as shown in the image where we can select more than one color ? The colored boxes must be checkboxes according to me but I am not able customize them
You need to define an xml resource in the drawable folder and use it in an imageView for instance for your question above it may look like this. Where by you use a shape called rectangle, giving it a solid colour as below and then also making the corner radius circular with 8dp. Then to display it you use an imageView and set it as a src. I have also added a stroke. Remove it if you dont need it!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#DFDFE0" />
<size
android:height="8dp"
android:width="16dp" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<stroke
android:width="3dp"
android:color="#2E3135" />
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp"/>
<gradient
android:startColor="#2E3135"
android:centerColor="#000000"
android:endColor="#ffffff"
android:angle="180"/>
</shape>
Change the colour as your need.

Drawable with 2 solid colours

I'm trying to create a drawable to act as the background for one of my layouts, and can't get it to display two solid colours divided in the middle.
Currently I'm attempting to use a layer-list with the background being solid, and an overlay of a semi transparent white rectangle to tint the original solid background colour along the top half of the rectangle. It doesn't need to be done like this though.
Gradient will not work unless you can find a way to make the transition immediate.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height"></size>
<solid android:color="#color/brb_orange"
android:gravity="bottom"></solid>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height_halved"></size>
<solid android:color="#color/md_white_1000"
android:alpha="127"
android:gravity="top"></solid>
</shape>
</item>
So, my understanding of this is that there should be two shapes, one half the size of the other. The first shape covers the whole drawable in orange and the other is white, and ~50% transparent (alpha = 127), takes up half the drawable and gravitates toward the top.
The preview of the drawable is fully white, without a hint of orange. What am I doing wrong?
Edit:
Right, to make this even simpler, I've added the tinted colour to my #color so that I don't even need to bother with transparency. The XML is as shown below:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="#dimen/basic_runners_height_halved">
<shape android:shape="rectangle" >
<size android:height="#dimen/basic_runners_height_halved" />
<solid android:color="#color/brb_orange_tint" />
</shape>
</item>
<item android:top="#dimen/basic_runners_height">
<shape android:shape="rectangle" >
<size android:height="#dimen/basic_runners_height" />
<solid android:color="#color/brb_orange" />
</shape>
</item>
But this doesn't even do the trick... runners_height_halved is half the dp of runners_height but this doesn't split it down the middle. Maybe 10% of the drawable is the tinted colour, and for some reason they are reversed so that the tinted colour is at the bottom. This should be the most simple thing...
Try to use padding(top,left, right, bottom) in the <item> tag. Hope it helps
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height"></size>
<solid android:color="#color/brb_orange"
android:gravity="bottom"></solid>
</shape>
</item>
<item android:top="10dp"
android:left="10dp"
android:right="10dp"
android:bottom="10dp">
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height_halved"></size>
<solid android:color="#color/md_white_1000"
android:alpha="127"
android:gravity="top"></solid>
</shape>
</item>

XML Circle with gradient fill Android

I am trying to make an XML Button, circle shape, which is filled with a gradient color. This color needs to be changed programatically.
Here is an example of what I am trying to achieve.
My attempts to build this have ended up with a gradient background and a solid colored circle. Instead of the gradient circle and transparent background.
I am hoping to be able to use more then 2 colors or a radial gradient for the circle as well. Any help is greatly appreciated.
You can Try with making Drawable XML. Example
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="circle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
</shape>
Arrange angle based on your requirement.
If you wants to change Color progarmatically then read this:-
How do I programmatically set the background color gradient on a Custom Title Bar?
Belated but can be helpful for someone else
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="0" android:endColor="#fff" android:gradientRadius="#dimen/_30sdp" android:startColor="#color/colorPrimaryDark" android:type="radial" />
<size android:width="#dimen/_25sdp" android:height="#dimen/_25sdp" />
</shape>
</item>
Output
Late but someone might get helped. Thanks to #Faizan
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="270" android:endColor="#D10719" android:gradientRadius="120dp" android:startColor="#0A5A1B" android:type="linear" />
<size android:width="100dp" android:height="100dp" />
</shape>
</item>
</selector>
Output:

Android: How to make Circle through xml which looks like a halo

I am trying to make a circle using xml with a halo. The halo would be like it starts with say green color in the center, the greenish color keeps fading and finally at the edges it becomes transparent.
How to go about it?
I am trying to use following:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient android:centerColor="#drawable/light_green" android:gradientRadius="250" android:type="radial"
android:endColor="#android:color/transparent"
android:angle="270"/>
</shape>
Still not able to get the desired effect. Any lead would be helpful.
Try this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="270"
android:centerColor="#88FF0000"
android:centerX="50%p"
android:centerY="50%p"
android:endColor="#00FF0000"
android:gradientRadius="50%p"
android:startColor="#FFFF0000"
android:type="radial" />
<size
android:height="80dp"
android:width="80dp" />
</shape>
Change color code and change centerColor's alpha code as per your requirement

How to create Rectangle Shape with inside round corners

I want to create Drawable like the White Shape shown in above Image.
I tries it using following code
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="40dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="3dp"
android:color="#android:color/white" />
<size
android:height="150dp"
android:width="150dp" /></shape>
But I'm getting Output like this..
I also tried layer-list with multiple Shapes but not able to get the desired output..
Also my ImageView's height and width are calculated in code..so I also don't want to pass size in drawable. Is it possible??
You can draw the shape with the following xml;
<?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="#fff" ></solid>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#000" ></solid>
<size
android:width="90dp"
android:height="90dp"/>
</shape>
</item>
</layer-list>
We draw two items upon each other. A white square on first layer and put another black square with 10dp radius and 5dp padding inside white square.
Also you can control width and height of the imageView which your drawable assigned, it scales accordingly.

Categories

Resources