I have this code:
LinearGradient backlg = new LinearGradient(0, 0, 10, 10,
new int[]{Color.BLACK, Color.BLACK, Color.BLUE, Color.BLUE},
new float[]{0,0.5f,.55f,1}, Shader.TileMode.REPEAT);
Which creates diagonal bands alternating between blue and black. They go from Bottom left to top right so here is my question, what do I change to make it go from top left to bottom right?
Is this what you mean:
LinearGradient backlg = new LinearGradient(0, 10, 10, 0,
new int[]{Color.BLACK, Color.BLACK, Color.BLUE, Color.BLUE},
new float[]{0,0.5f,.55f,1}, Shader.TileMode.REPEAT);
Checkout the [documentation][1] for that constructor. The first 4 parameters are the x,y coordinates for the start and end of the gradient line.
So if 0,0 is bottom left and 10,10 is top right, try going from 0,10 to 10,0.
[1]: http://developer.android.com/reference/android/graphics/LinearGradient.html#LinearGradient(float, float, float, float, int[], float[], android.graphics.Shader.TileMode)
Related
My goal is to fill shade the outer parabola when its downward but when i used paint that's the output.
My Code
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));
series1Format.setFillPaint(lineFill);
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);
I know I can create with LinearGradient, but I couldn't figure out how to create gradient from inside out.
I want to create something like this, and I want to control the size and the angle of the gradient.
I created this gradient with the code below.
linearGradient = new LinearGradient(0, 0, 0, h,
new int[]{0xffffffff, 0xffffffff, 0x00ffffff, 0x00ffffff, 0xffffffff, 0xffffffff},
new float[]{0, 0.30f, 0.45f, 0.55f, 0.7f, 1.0f}, Shader.TileMode.CLAMP);
How can i do that? Is there an example for that? Can you show me ?
For ex. I have an circle like that
RadialGradient gradient = new RadialGradient(0, 0, 40, new int[]{Color.RED, Color.YELLOW}, new float[] {1,0}, android.graphics.Shader.TileMode.CLAMP);
paint2.setShader(gradient);
canvas.drawCircle(this.getWidth()/2, this.getHeight()/2, 40, paint2);
How this circle can draw a circle?