android tint animation doesn't work - android

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));

Related

Android ArgbEvaluator calculate fraction

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.

Can not change background in view

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?

Fill Imageview background animation

How to fill a image view background such that its transparent area will be filled by some color.
I want to fill below image -
and filled image will be like -
i want to fill black area of image only with animation from bottom to top.
Please suggest some animation by which i can do desire animation.
Thanks in advance
U can try adding it in code
ImageView backgroundImg = (ImageView) findViewById(R.id.backgroundImg);
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));
This will also solve your problem. Just add this in your ImageView tag.
android:background="#android:color/red"
Check this Im not sure. But check this snippet
You can simply use ArgbEvaluator which is available since API 11 (Honeycomb):
ValueAnimator anim = new ValueAnimator();
anim.setIntValues(color1, color2);
//anim..setIntValues(Color.parseColor("#FFFFFF"), Color.parseColor("#000000"));
anim.setEvaluator(new ArgbEvaluator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
backgroundImg.setBackgroundColor((Integer)valueAnimator.getAnimatedValue());
}
});
anim.setDuration(300);
anim.start();
Even better, beginning with API 21 (Lollipop 5.0) you can replace the first 3 lines in the code above with one:
ValueAnimator anim = ValueAnimator.ofArgb(color1, color2)

Animating "fillColor" property in Animated Vector Drawable

In my app I'm using an arrow as a drawable, for which I'd like to animate the fillColor property. I came across AnimatedVectorDrawable examples which modify properties such as pathData, transateY/X, rotation etc., How do I animate the fillColor property, so that my arrow changes color periodically?
The function animate() haven't got a method to change fillColor directly, but you can do a litle trick. In the next code I show you how use the function animate() to simulate the change of fillColor using the function animate twice:
drawableView.animate()
.alpha(0.0f)
.setDuration(100)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
/*
In this point you can change de color of drawable,
the color or property that you want to change (title, color, size...)
*/
drawableView.animate()
.alpha(1.0f)
.setDuration(100)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
});
Using code above, you can change some property of drawable inside the animation, I mean, the proccess will be the next:
1 - Animation do that the drawable disappear (alpha 0)
2 - You change the property you want
3 - Animation do that the drawable appear again (alpha 1)
If you use this code inside Timer, you can do that the drawable change the color periodically.

FloatingActionButton icon animation (Android FAB src drawable animation)

I was trying to find documentation about how to create icon animation of FAB's (of the Design support library), after searching a while i couldn't find any information about it and the AnimationDrawable reference in android developers doesn't work for FAB's even if the class is a child of ImageView.
but manage to get a workaround that works just fine.
The technic I used is similar to the one exposed on the DrawableAnimation documentation, and using the Property Animation API doc.
First I use the ValueAnimator class, and an int array containing the ids of the drawables that you're going to use in your animation.
final int[] ids = {R.drawable.main_button_1,R.drawable.main_button_2,R.drawable.main_button_3,R.drawable.main_button_4,R.drawable.main_button_5,R.drawable.main_button_6, R.drawable.main_button_7};
fab = (FloatingActionButton) findViewById(R.id.yourFabID);
valueAnimator = ValueAnimator.ofInt(0, ids.length - 1).setDuration(yourAnimationTime);
valueAnimator.setInterpolator( new LinearInterpolator() /*your TimeInterpolator*/ );
Then set up an AnimationUpdateListener, and define the change in icon behavior with the method FloatinActionButton.setImageDrawable(Drawable yourDrawable).
But the ValueAnimator updates by default every available frame (depending on the load in hardware), but we don't need to redraw the icon if it has already been drawn, so that is why I use the variable "i"; so each icon is drawn only once. (the timing depends on the interpolator you define)
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
int i = -1;
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
if(i!=animatedValue) {
fab.setImageDrawable(getResources().getDrawable(ids[animatedValue]));
i = animatedValue;
}
}
});
This implementation allows you to play the animation backward with the method ValueAnimator.reverse();
I know is not a pro solution but it's the only one I've figured out to work on all API's supporting the PropertyAnimation. Please, if you know a better solution post it here, if not I hope this post is helpful
You can achieve FAB's src drawable animation quite similarly to other views that use background for AnimationDrawable.
For general views' background drawable animation, see Use AnimationDrawable.
However, you cannot call getBackground() to get FAB's src.
Instead, use getDrawable().
Example:
FloatingActionButton loginButton = findViewById(R.id.loginButton);
loginAnimation = (AnimationDrawable) loginButton.getDrawable();
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loginAnimation.start();
}
});

Categories

Resources