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);
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 have multiple lines draw in different positions.
For example:
canvas.drawLine(startXLine1 ,stopXLine1, startYLine1, stopYLine1, paint)
canvas.drawLine(startXLine2 ,stopXLine2, startYLine2, stopYLine2, paint)
I want a gradient like this for each line:
When I try this, I don't have this effect, but the gradient with this direction blue(left) ---> white(right)
Like this: http://media.24ways.org/2011/verou/1.png
Shader shader = new LinearGradient(startXLine1, startYLine1, stopXLine1, stopYLine1, res.getColor(R.color.blue), res.getColor(R.color.white), Shader.TileMode.CLAMP);
paint.setShader(shader);
Someone can help me with this?
To fill the background like in your image:
Shader shader = new LinearGradient(0, 0, 0, h /*canvas height*/, res.getColor(R.color.blue), res.getColor(R.color.white), Shader.TileMode.MIRROR /*or REPEAT*/);
paint.setShader(shader);
For days I tried to implement a simple Imagebutton in Android, using a dynamic LinearGradient as background. I want the base design from android.R.drawable.btn_default, which is colored starting from the left edge of the button to a value given in percent from v = 0-100%(right edge). The edge between the left and right part of the button should be sharp (like an equalizer). For any suggestions without NullException errors I would be very thankful!
EDIT 29.07.14
Here is the code snippet, working without errors:
Drawable dr = getResources().getDrawable(android.R.drawable.btn_default);
Bitmap bitmap = Bitmap.createBitmap(dr.getIntrinsicWidth(), dr.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
dr.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
dr.draw(canvas);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
paint.setShader(new LinearGradient(0, 0, canvas.getWidth()*7*vol, 0, new int[] { Color.GREEN, Color.TRANSPARENT }, new float[] { 0, 1 }, Shader.TileMode.CLAMP));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
mButtonSpeech.setBackground(new BitmapDrawable(getResources(), bitmap));
But even without the middle part of my code, adding the gradient to the canvas, the button background does not look like I expect it (it is to small and in the center not colored like the other buttons, where I set the backgrund by:
mButtonDraw.setBackgroundResource(android.R.drawable.btn_default);
If I also draw the Paint into Canvas, I would expect the default button background, colored in the left part, like I could do it with setColorFilter().
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?
This must be an easy one but I'm really at a loss... The following code draws a rectangle with a linear gradient going from left to right, from white to black,
int x1 = 0, y1 = 0, x2 = 100, y2 = 40;
Shader shader = new LinearGradient(x1, y1, x2, y2, Color.WHITE, Color.BLACK, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
canvas.drawRect(new RectF(x1, y1, x2, y2), paint);
Ok, fine. Now what I'd like to do is to change this gradient into a horizontal one, so that the color goes from white to black, from top to bottom. What I tried to do is to add:
Matrix trans = new Matrix();
trans.setRotate(90);
shader.setLocalMatrix(trans);
but instead the gradient goes at a funny angel, or there is just a single color... I also tried to play with the coordinates of the gradient in all sorts of way (thinking that maybe they should be transformed) to no avail. What am I missing?
I haven't done much android coding, but one approach worth trying is:
int x1 = 0, y1 = 0, x2 = 0, y2 = 40;
The x never changes in the gradient only the y does.
So what this might look like is:
Shader shader = new LinearGradient(0, 0, 0, 40, Color.WHITE, Color.BLACK, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
canvas.drawRect(new RectF(0, 0, 100, 40), paint);
you can adjust the gradient orienation
Top to Bottom
LinearGradient(0, 0, 0, height, gradientStartColor, gradientEndColor, Shader.TileMode.CLAMP)
Bottom to Top
LinearGradient(0, height, 0, 0, gradientStartColor, gradientEndColor, Shader.TileMode.CLAMP)
Left to Right
LinearGradient(0, 0, width, 0, gradientStartColor, gradientEndColor, Shader.TileMode.CLAMP)
Right to Left
LinearGradient(width, 0, 0, 0, gradientStartColor, gradientEndColor, Shader.TileMode.CLAMP)