I have a ShapeDrawable Object in my class in the form of an arc.
ShapeDrawable progressArc = new ShapeDrawable(new ArcShape(120, 12));
Now I would like to use a radial gradient effect on the color and also display just a ring instead of a filled arc. Does the Android SDK provide such a feature?
Currently using the below code, I get a gradient on the circle.
progressArc.setIntrinsicHeight(500);
progressArc.setIntrinsicWidth(500);
Shader shader1 = new LinearGradient(5, -5, 35, 0, new int[] { 0xFF33B5E5,0xFFFFFFFF }, null, Shader.TileMode.MIRROR);
progressArc.getPaint().setShader(shader1);
progressArc.getPaint().setStrokeWidth(1);
progressArc.getPaint().setColor(Color.RED);
progressArc.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
You can use a SweepGradient to get a radial gradient:
Shader s = new SweepGradient(0, 0, new int[] { 0xFF33B5E5,0xFFFFFFFF }, null);
//or if you only use two colors:
Shader s = new SweepGradient(0, 0, 0xFF33B5E5, 0xFFFFFFFF);
To avoid filling, just use the Paint.Style.STROKE style, with appropriate strokeWidth.
Related
I need to apply dynamically a gradient color to a bitmap (it looks like a scratch with some transparent parts) that will be draw over another bitmap: this is the result i need.
This is my code:
Bitmap bitmapbackground = bitmaporiginal.copy(bitmaporiginal.getConfig(), true);
Bitmap bitmaptocolor = BitmapFactory.decodeResource(activity.getResources(), R.drawable.scratch);
LinearGradient gradient = new LinearGradient(0, 0, 0, bitmaptocolor.getHeight(), Color.parseColor("#D81B60"), Color.parseColor("#F48FB1"), Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(gradient);
Canvas canvas = new Canvas(bitmapbackground);
canvas.drawBitmap(bitmaptocolor, 0, 0, paint);
But in this way it does not apply the gradient color to the scratch (it remains always black). What am i doing wrong ?
Make sure bitmaptocolor.getHeight() is actually returning height.
Use hexadecimal equivalent for color.
LinearGradient gradient = new LinearGradient(0, 0, 0, bitmaptocolor.getHeight(), 0xD81B60, 0xD81B60, Shader.TileMode.CLAMP);
I want to set a custom drawable with a linear gradient as the background drawable of the bar in my SeekBar. I am creating the LinearGradient in the second statement in the following snippet, and doing it like so:
// Creating the drawable:
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
Shader linearGradientShader = new LinearGradient(0, 0, 300, 20, new int[] { Color.RED, Color.BLUE },
new float[] { 0, 1 }, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(linearGradientShader);
shapeDrawable.setBounds(10, 10, 300, 30);
seekBar.setProgressDrawable(shapeDrawable);
The problem here is that, according to the specification, the 6th parameter is defined as
May be null. The relative positions [0..1] of each corresponding color in the colors array. If this is null, the the colors are distributed evenly along the gradient line.
I wanted both the red and blue colors to be distributed evenly, i.e. half the shape should appear redish and half should appear bluish (like the following image).
So I tried null, new float[] {0, 0.5f}, and new float[] {0, 1} as values of the 6th argument. I got the following three results respectively.
For null:
For new float[] {0, 0.5f}:
For new float[] {0, 1}:
Show where am I going wrong? How should I fix this?
use
ShapeDrawable#setShaderFactory(ShapeDrawable.ShaderFactory factory)
the factory has a method Shader resize(int width, int height) which is called every time your drawable bounds change and this is a place where you should return your LinearGradient shader based on width / height parameters
as you will see you can now just pass null positions and colors will be distributed evenly
TextView secondTextView = new TextView(this);
Shader textShader=new LinearGradient(0, 0, 0, 20,
new int[]{Color.GREEN,Color.BLUE},
new float[]{0, 1}, TileMode.CLAMP);
secondTextView.getPaint().setShader(textShader);
The above particular code giving me green shade at the top and blue as text color but my requirement is absolutely in reverse order all i need is green color shade from the bottom and blue as text color. can someone please help me out to achieve this.
Hope this will work for you.
Shader myShader = new LinearGradient(
0, 0, 0, 100,
Color.WHITE, Color.BLACK,
Shader.TileMode.CLAMP);
secondTextView.getPaint().setShader(myShader);
Currently I am using this code to add color:
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(color);
Now I need to apply some gradient colors to it along with stroke(like border with different color). I am setting this as background to button.
Here is what I am expecting, I need to do it programmatically.
Add a RadialGradient to your drawable like this:
Shader shader = new RadialGradient(x, y, radius, color0, color1, Shader.TileMode.REPEAT);
drawable.getPaint().setShader(shader);
Obviously, you can interchange LinearGradient, SweepGradient, and any of the parameters.
Here is how to add the stroke:
drawable.getPaint().setStrokeWidth(3);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
Hmmm, I think I have to defer to #StinePike with the GradientDrawable:
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
gd.setShape(GradientDrawable.OVAL);
use GradientDrawable to create gradient
or
you can see this answer
Hi
I want to create a shape drawable and fill it with gradient color with white stroke
here is my code
ShapeDrawable greenShape = new ShapeDrawable(new RectShape());
Shader shader1 = new LinearGradient(0, 0, 0, 50, new int[] {
0xFFBAF706, 0xFF4CD52F }, null, Shader.TileMode.CLAMP);
greenShape.getPaint().setShader(shader1);
greenShape.getPaint().setStrokeWidth(3);
greenShape.getPaint().setColor(Color.WHITE);
greenShape.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);`
The problem is the rectangle appears with gradient fill but without stroke
The ShapeDrawable does not allow you to easily draw a stroke around it.
If you really want then this would be a nice place to look at.
OR
You could use a GradientDrawable
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
(PS: This is given as a comment in the same page!)
It looks like this person struggled with the same issue and the only way they found was to subclass the ShapeDrawable:
Trying to draw a button: how to set a stroke color and how to "align" a gradient to the bottom without knowing the height?
<stroke
android:width="2dp"
android:color="#808080" />
try it, it will work definitely
Change the style to greenShape.getPaint().setStyle(Paint.Style.STROKE)
and
greenShape.setShaderFactory(new ShapeDrawable.ShaderFactory() {
#Override
public Shader resize(int width, int height) {
return new LinearGradient(0, 0, 0, 0, Color.WHITE, Color.WHITE, Shader.TileMode.REPEAT);