I've a little difficult in using ArgbEvaluator object.
I want to change the color of view from one color to another color in specific duration.So I write this code,it works well.
ValueAnimator animator = ValueAnimator.ofArgb(Color.RED,Color.BLUE);
animator.setDuration(5000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int color = (int) animation.getAnimatedValue();
view.setBackgroundColor(color);
}
});
animator.start();
But I want to use ArgbEvaluator to animate like that.
public void setColor(fraction){
int color = evaluator.evaluate(fraction,startColor,endColor);
view.setBackgroundColor(color);
}
The above setColor method will be called agian and again for specific duration(eg-5000ms).
Here is my problem.
I don't know how to calculate fraction to change color within specific duration.
I want to know the calculation formula of fraction.Thanks.
Related
I have custom ImageView which should animate its content after loading from solid background color to image with setColorFilter:
private void _animate() {
int bgColor= ContextCompat.getColor(getContext(), R.color.iconPlaceholderBg);
int transparent=ContextCompat.getColor(getContext(),android.R.color.transparent);
ValueAnimator bgAnim= ValueAnimator.ofObject(new ArgbEvaluator(),bgColor,transparent);
bgAnim.setDuration(5000);
//bgAnim.setInterpolator(new DecelerateInterpolator());
bgAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
//Log.d("anim",animation.getAnimatedFraction()+"")
//Image.this.clearColorFilter();
Image.this.setColorFilter((int)animation.getAnimatedValue());
}
});
bgAnim.start();
}
Animation itself runs as expexted (5 seconds long).However content appears immediately in the final position(without color transformation).I've tried different PorterDuff.Mode modes but looks like none of them do things right.Are there any workarounds to get desired effect?
The problem was with my background color which has tranparency.For some reason setColorFilter works not exactly as you expect with trasparent colors.I've managed to get desired behaviour with following workaround:
Image.this.getDrawable().setAlpha((int) (animation.getAnimatedFraction()*255));
I'm trying to change the background color of my view. I wrote some code and i can change color with animation, but first time my view is freezing before color changes. This is my code
private void changeBackgroundColorWithAnimation(int duration, final View view, int startColor, int endColor) {
ValueAnimator anim = new ValueAnimator();
anim.setIntValues(startColor, endColor);
anim.setEvaluator(new ArgbEvaluator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(final ValueAnimator valueAnimator) {
view.setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
}
});
anim.setDuration(duration);
anim.start();
}
I call my function like this:
changeBackgroundColorWithAnimation(300, TransferFragmentNewVersion.rootLayout,
Color.parseColor("#E6000000"), Color.WHITE);
As I said the background color has changed, but first time View is freezing(only first time)l
How can I solve my problem? Thanks everyone.
Why aren't you sending your view's initial color as a starting color?
You can replace Color.parseColor("#E6000000") with TransferFragmentNewVersion.rootLayout.getSolidColor().
Can you post more of your code?
I'm trying to create smooth color animation in my WaveView. Unfortunatelly animation is creating rainbow effect spamming through every color from start to end.
I'll need it to go from Red to Green with just (Red -> Orange - > Yellow - > Greenish -> Green) like on RGB tables.
Here's my ObjectAnimator:
ObjectAnimator waveColor = ObjectAnimator.ofInt(
mWaveView, "waveColor",Color.parseColor("#64ff0000"),Color.parseColor("#6400ff00"));
waveColor.setDuration(7000);
waveColor.setInterpolator(new LinearInterpolator());
animators.add(waveColor);
I have no idea how to make it to just go through that colors.
Check this Link:
Hope I have understood your question properly and it helps
ColorDrawable[] color = {new ColorDrawable(Color.RED), new ColorDrawable(Color.GREEN)};
TransitionDrawable trans = new TransitionDrawable(color);
//This will work also on old devices. The latest API says you have to use setBackground instead.
mWaveView.setBackgroundDrawable(trans);
trans.startTransition(7000);
best way use ValueAnimator and ColorUtils.blendARGB
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
valueAnimator.setDuration(325);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float fractionAnim = (float) valueAnimator.getAnimatedValue();
view.setColorBackground(ColorUtils.blendARGB(Color.parseColor("#FFFFFF")
, Color.parseColor("#000000")
, fractionAnim));
}
});
valueAnimator.start();
I want to change the size of the itemView of RecyclerView in the 'animateMoveImpl()' method for my ItemAnimator, but it did not work.
I have tried the scale animation, it works fine, but not enough.
What I want to achieve is to change the 'width' property, so the image shown in the itemView will adapt to the change. The scale animation just make the image stretched.
Is it possible to animate the width change of itemView in ItemAnimator?
Thanks~
Update:
The code is like follows.
ValueAnimator anim = ValueAnimator.ofFloat(1f, 0.5f);
anim.setDuration(1000);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float tmp = DEFAULT_WIDTH * (float)animation.getAnimatedValue();
animButton.setWidth((int)tmp);
animButton.requestLayout();
}
});
anim.start();
Note that it works for normal view. But it does not work int the 'animateMoveImpl()' for ItemAnimator.
I'd like to animate the backgroundTint value (and ideally the alpha value too) of a FloatingActionButton, so that the FAB background color continuously switches between two colors.
My noob approach would be to use a timer that calls a function that updates this property when it fires. I'm sure there's a better way of doing this?
I got this to work using ObjectAnimator as suggested by #MH. above, but I had to override the onAnimationUpdate() callback:
final ValueAnimator animator = ValueAnimator.ofInt(Color.rgb(0, 121, 107), Color.rgb(226, 143, 34));
animator.setDuration(2000L);
animator.setEvaluator(new ArgbEvaluator());
animator.setInterpolator(new DecelerateInterpolator(2));
animator.addUpdateListener(new ObjectAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
fab.setBackgroundTintList(ColorStateList.valueOf(animatedValue));
}
});
animator.start();