How is it possible to create gradient for dynamic number in Android app?
E.g. TextView or Button with text is used.
(simple solution is a favorite)
Try this
TextView Textview1 = new TextView(this);
Shader textShader=new LinearGradient(0, 0, 0, 20,
new int[]{Color.GREEN,Color.BLUE},
new float[]{0, 1}, TileMode.CLAMP);
Textview1.getPaint().setShader(textShader);
It's possible, just create a drawable file, with a gradient:
<shape>
<gradient android:startColor=""
android:endColor=""
android:angle=""/>
</shape>
and point it in the
<TextView ...
android:textColor="#drawable/yourdrawablename"/>
or programmatically like this :
tv.setTextColor(R.drawable.yourdrawablename);
Use Shader and LinearGradient in android
TextView textview = new TextView(this);
Shader shader= new LinearGradient(0, 0, 0,20,new int[]{Color.WHITE,Color.GRAY},new float[]{0, 1}, TileMode.MIRROR);
textview.getPaint().setShader(shader);
Related
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);
I am new at Canvas and drawing in Android and I just took the code from this website I managed to tweak it however, I am finding it difficult to change the border style and the border color.
I want the border to just be a simple black square border, no fancy waves and color spectrums.
This is the code in the onCreate function, I already managed to minimize the wavyness of the border but there's still tiny bumps and turns.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
RectF inset = new RectF(6, 6, 6, 6);
float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
mDrawables = new ShapeDrawable[2];
mDrawables[0] = new ShapeDrawable(new RoundRectShape(outerR, inset, null));
//mDrawables[0].getPaint().setShader(makeSweep());
mDrawables[1] = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR));
//mDrawables[1].getPaint().setShader(makeLinear());
PathEffect mPathEffect1 = new DiscretePathEffect(1, 1);
PathEffect mPathEffect2 = new CornerPathEffect(1);
mDrawables[0].getPaint().setPathEffect(new ComposePathEffect(mPathEffect2, mPathEffect1));
RelativeLayout layout = (RelativeLayout) findViewById(R.id.myDrawing);
layout.setBackgroundDrawable(mDrawables[0]);
activity = this;
//mView = new DrawingView(this);
mView = new SignatureView(activity, null);
layout.addView(mView, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
init();
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
Sorry I know this may turn out to be a simple task but I'm completely stumped.
EDIT - Okay I managed to make it plain black, I just removed the mDrawables[0/1].getPaint() lines and it turned to black, my only problem right now is to make it perfectly straight.
Figured it out.
For the color of the border, comment out the following lines:
mDrawables[0].getPaint().setShader(makeSweep()); and mDrawables[1].getPaint().setShader(makeLinear());
After that, the problem is the DiscretePathEffect. Change that line of code to:
PathEffect mPathEffect1 = new DiscretePathEffect(1, 0);
Apparently, the two arguments for the DiscretePathEffect determine how "squiggly" the line gets.
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.
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);