Android ValueAnimator interpolating crazy colors - android

I'm trying to use a very simple code to fade the background color change in a textview
the idea is to chose a new color, than have it fade from transparent to the desired color, here is the code:
ValueAnimator anim = new ValueAnimator();
anim.setEvaluator(ArgbEvaluator.getInstance());
anim.setDuration(1500);
anim.setIntValues(hightlightColor & 0x00FFFFFF, hightlightColor);
// txtView.setBackgroundColor(hightlightColor);
anim.addUpdateListener(animation -> txtView.setBackgroundColor((int) animation.getAnimatedValue()));
anim.start();
highlight color is just a green i'm using for example, txtview is a valid textview and the current background is null
but instead of creating a fade in shades of green the code results in a crazy rainbow like interpolation, that ends exactly in the desired green, but all intermediate values are shit
I thought it might be the alpha channel so i tested starting from white to green but the intermediate colors were all crazy also
MY APP MINSDK IS 16 SO I CANT USE ValueAnimator.ofArgb()
here is how it looks

Related

Chat bubble animation

I want to achieve animation when I send my message it goes from bottom to top like flying a bird (though I have done it :P) but while flying, that chat bubble view's radius is also changing(This is the thing I am not able to achieve).
Please help me to achieve this kind of animation or give me some sort of hint.
Thank you
I have used below code to set radius and then translate that view using xml file but not able to change radius on the go.
GradientDrawable gd = new GradientDrawable();
// Specify the shape of drawable
gd.setShape(GradientDrawable.RECTANGLE);
// Set the fill color of drawable
gd.setColor(Color.TRANSPARENT); // make the background transparent
// Create a 2 pixels width red colored border for drawable
gd.setStroke(2, Color.RED); // border width and color
// Make the border rounded
gd.setCornerRadius(15.0f);
((RelativeLayout) findViewById(R.id.rlvView)).setBackground(gd);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.item_animation_from_bottom);
((RelativeLayout) findViewById(R.id.rlvView)).startAnimation(animation);
You need to use ObjectAnimator (derivative class of ValueAnimator) to achieve animate any value (visible/invisible) of a view. Code below animate cornerRadius for one time from 15.0f to 0. Please try, I hope it helps.
// Make the border rounded
gd.setCornerRadius(15.0f);
((RelativeLayout) findViewById(R.id.rlvView)).setBackground(gd);
ObjectAnimator animator = ObjectAnimator.ofFloat(gd, "cornerRadius", 0);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(800);
animator.start();

apply gradient and shadow simultaneously with different colors on TextView Android

I am trying to apply gradient and shadow simultaneously with different colors on TextView. Whenever I apply gradient(shader) it also redraws the shadow with the same color as gradient has, but I need both shadow and gradient with different colors.
Here is some of my code for gradient and shadow:
txtHello.setShadowLayer(shadowRadius, tempShadowX,tempShadowY, getShadowColorCode());
radialGradient = new RadialGradient(txtHello.getWidth()/2,txtHello.getHeight()/2,10,color,position,tile_mode);
txtHello.getPaint().setShader(radialGradient)
Whenever shader applied on textView you cant change the color of shadow.its necessary to set shader to null first and then apply shadow layer and text color with different colors
Problem solved:
you can apply change the shadow of a textured or gradient applied text that is
First save the shader if you have already applied on text.
Second make shader null so that shader effect disappear so a little bit of time
Then apply shadow Layer on text
Save the shadow Layer
Clear Shadow layer
Apply shader
then save the shader
Now you have a text having shader(gradient/textured) applied with custom color.
Shader shader = getPaint().getShader();
getPaint().setShader(null);
getPaint().setShadowLayer(20, 10, 10, Color.BLACK);
super.onDraw(canvas);
getPaint().clearShadowLayer();
getPaint().setShader(shader);
super.onDraw(canvas);

Android find if color is too close to black or white

I have a int representation of a color. What I want to do is if this color is too black or too white, I want to set background to a default color. I want to find if the color is too close to black or white using RGB or HSV representation is fine. By too black or too white, I mean to say I can set some threshold depending on the scale. Say there is a scale from 0 to 1 where 0 is black and 1 is white, I want to reject all the colors below 0.2 and above 0.8. I tried the below code but it actually does not work. This does not work since I see some other colors also get rejected which are not shades of black or white. Please help.
public static boolean isWhitish(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
return hsv[2] > 0.8;
}
First you will want to get the HEX CODE of your color, then you can use this solution to extract the brightness of that color.
You can then use your (brightness < .2 || brightness > .8) to decide what to do next.
Hope this helps, good luck :-)

alpha value always reset after color change of paint

I'm drawing several shapes in different colors, using a single instance of Paint. I would like the alpha value (< 255) of each painted region to be the same. I set the desired alpha value when initializing the Paint. However, whenever I change the color of my Paint using setColor, the alpha value of my paint is reset to 255 (fully opaque).
The following code demonstrates this issue:
myPaint.setAlpha(100);
// myPaint.getAlpha() returns 100, as expected;
myPaint.setColor(Color.DKGRAY);
// myPaint.getAlpha() now returns 255;
Why does this happen? Will there be any impact on performance if I am forced to call paint.setAlpha(...) every time I change the paint color?
This is because the color you are using is of format argb. See the Paint and Color javadoc. You need to specify a color with just RGB or set the color alpha to the value you want to use. And it shouldn't be much of performance hit if you set the alpha each time, if you want to go that route.
As an added example you could do
paint.setColor(new Color(100, Color.red(Color.DKGRAY), Color.green(Color.DKGRAY), Color.blue(Color.DKGRAY)));
If you were going to always set the color to something like dark gray I would say you are better off creating a color resource with the values you want and using it.

How do I set an Android View's transparency with animation?

I need to set the initial transparency for a TextView, but I don't know how to do it. On iPhone/iOS, it can be done easily with the alpha property. On Android, I've tried to set the alpha using AlphaAnimation but it's no good -- it doesn't work. It just returns to 100% alpha when the animation ends.
AlphaAnimation anim = new AlphaAnimation(1, 0.2f);
anim.setDuration(5000);
textView.startAnimation(anim);
Any ideas guys?
You can set the alpha channel directly in a color value that you assign to the TextView. See Available Resource Types.

Categories

Resources