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);
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 need to draw a circle in an Android canvas, based on a gradient list of colors. I managed to draw it without the gradient, as a set of arcs each having one of the colors in the list, as presented by the following image.
How can I draw it with an actual gradient? I tried with the following code to apply a shader to the paint:
Shader shader = new LinearGradient(0, 0, circleWidth, circleHeight, colorList, null, Shader.TileMode.MIRROR);
paint.setShader(shader);
canvas.drawCircle(circleWidth / 2, circleHeight / 2, radius, paint);
but the result is as follows.
I managed to make it using a SweepGradient.
Shader shader = new SweepGradient(circleWidth / 2, circleHeight / 2, colorList, null);
paint.setShader(shader);
canvas.drawCircle(circleWidth / 2, circleHeight / 2, radius, paint);
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);
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().
Is there a way to put a gradient to a bitmap object in android 2.1? The image must look like this:
I need the gradient only on top of the bitmap. DrawableGradient or LinearGradient are only from android 2.2 so these objects doesn't help me at all. Thanks
Do you need this from XML or from code? In code, try this:
/* Create a 200 x 200 bitmap and fill it with black. */
Bitmap b = Bitmap.createBitmap(200, 200, Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawColor(Color.BLACK);
/* Create your gradient. */
LinearGradient grad = new LinearGradient(0, 0, 0, 50, Color.GRAY, Color.BLACK, TileMode.CLAMP);
/* Draw your gradient to the top of your bitmap. */
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setShader(grad);
c.drawRect(0, 0, 200, 50, p);
In XML, just make two separate views in a vertical linear layout. The top view should have a gradient drawable background, the bottom, taller view should have a solid background.