How to animate ImageAlpha of ImageView - android

Since API16 ImageView.setImageAlpha(int) should be used instead of View.setAlpha(float) for better performance.
But how can I animate that value? I tried ValueAnimator without success.
ObjectAnimator.ofInt(imageView, "imageAlpha", 0).start();
I mean what Chet Haase is talking about here http://youtu.be/vQZFaec9NpA?t=33m

It should be fine. Maybe you forgot to specify the animation duration, or your alpha values are wrong (negative, too small, too close to notice)?
You can always animate using custom listener. It's longer, but easier to debug:
ValueAnimator animator = ValueAnimator.ofInt(0,255);
animator.setDuration(300);
animator.setInterpolator(new DecelerateInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
imageView.setImageAlpha(valueAnimator.getAnimatedValue());
}
});
animator.start();

Related

How to start multiple ObjectAnimator on view simultaneously?

Here i am trying to move a view on a path with ObjectAnimator and also need to set one more scale animation on same view.
ObjectAnimator objectAnimator = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
{
objectAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
}
if (objectAnimator != null) {
objectAnimator.setDuration(2500);
objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
objectAnimator.start();
view.startAnimation(scaleRection);// this is not working because changing of x y position
need to start another Animation when objectAnimator.start();
also tried with listener
objectAnimator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
view.startAnimation(scaleRection);
}
#Override
public void onAnimationEnd(Animator animation)
{
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
You can also use an AnimatorSet play together https://developer.android.com/reference/android/animation/AnimatorSet.html#playTogether and it's builder function https://developer.android.com/reference/android/animation/AnimatorSet.Builder
To play Two ObjectAnimator together
e.g.
AnimatorSet animationSet = new AnimatorSet();
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY", 1f, 0f);
scaleY.setDuration(5000);
scaleY.setInterpolator(new AccelerateDecelerateInterpolator());
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX", 1f, 0f);
scaleX.setDuration(5000);
scaleX.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.playTogether(scaleX, scaleY);
animationSet.start();
I would suggest using ViewPropertyAnimator.
From the docs :
This class enables automatic and optimized animation of select
properties on View objects. If only one or two properties on a View
object are being animated, then using an ObjectAnimator is fine; the
property setters called by ObjectAnimator are well equipped to do the
right thing to set the property and invalidate the view appropriately.
But if several properties are animated simultaneously, or if you just
want a more convenient syntax to animate a specific property, then
ViewPropertyAnimator might be more well-suited to the task.
This class may provide better performance for several simultaneous
animations, because it will optimize invalidate calls to take place
only once for several properties instead of each animated property
independently causing its own invalidation. Also, the syntax of using
this class could be easier to use because the caller need only tell
the View object which property to animate, and the value to animate
either to or by, and this class handles the details of configuring the
underlying Animator class and starting it.
You can chain as many animations as you like at once in one line of code :
view.animate().translationX(...).translationY(...).scaleX(...).scaleY(...).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(2500);
if you need different values for your duration or similar, you can do it with two lines :
view.animate().translationX().setDuration(...) ...
view.animate().scaleX().setDuration(...) ...
There are also methods translationXBy() and scaleXBy() which might be more suitable for your case, and you can also set a listener etc. Check the docs for all available methods

ValueAnimator behaviour changes when AdView shows in

I've made a custom view for visualizes the progress. It has a simple intro animation like the below :
PropertyValuesHolder c=PropertyValuesHolder.ofInt("c",0,100);
ValueAnimator anim=new ValueAnimator();
anim.setValues(c);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
position = (int) animation.getAnimatedValue("c");
postInvalidate();
}
});
anim.setDuration(1000);
anim.start();
It works smoothly with about 50 keyframes, but after the AdView that placed on the parent view, turns visible the same animation runs with lags (performance reduced to about 20 keyframes). I check the ValueAnimator.FrameDelay, it doesn't change.
What causes this problem, And what is the solution?
As #MartinMarconcini said, the problem related to the WebView that embedded inside the AdView. The performance improved by enabling Hardware Acceleration on the AdView.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
adView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
else adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

RecyclerView ItemAnimator cannot animate width change

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.

FloatingActionButton backgroundTint animation

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

ValueAnimator Helper Class

I am a beginner in Android development.
Coming from iOS background I tend to use animations on Views
to animate a View in Android. I have found out to do it in a "normal" way
For example to animate alpha value of a view
valueAnimator = ValueAnimator.ofFloat(0, 100);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
viewToAnimate.alpha(value);
}
});
valueAnimator.setDuration(10000);
valueAnimator.start();
I am wondering if there is an easy way. Perhaps create a "Helper" class to animate an objects alpha value. Where we can pass in the View to animate and have it animated.
Something like AnimationHelper.animateAlpha(viewToAnimate, OF_VALUE, TO_VALUE, DURATION);
I am sorry to ask this question. Since my Java knowledge is rusty, I am wondering if there is any kind folks out there who is willing to help out.
Thank you
Try this:
viewToAnimate.animate().alpha(TO_VALUE).setDuration(DURATION);
or this:
ObjectAnimator anim = ObjectAnimator.ofFloat(viewToAnimate, "alpha", FROM_VALUE, TO_VALUE);
anim.setDuration(DURATION);
anim.start();

Categories

Resources