I searched a lot for how to make simple TimerAnimator like Value animator
can anyone tell me how to do it very simply
this is the one for value animator
ValueAnimator timeAnimator=ValueAnimator.ofInt(1,10);
timeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int currentvalue= (int) animation.getAnimatedValue();
tttt.setText(String.valueOf(currentvalue));
}
});
timeAnimator.setDuration(20000);
timeAnimator.start();
Related
I have a TextView showing double value. Double value is transferred from API response, and I want to add animation. I want to if, for example, the double value is 2.3, I want text View to increase shown number by 0.0 until 2.3, so it would be 0.1-0.2-0.3... etc. How can I do this? I have seen examples with Value Animator but they only work with integers. Ps. my code it's in Kotlin.
public void animateTextView(float initialValue, float finalValue, final TextView textview) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(initialValue, finalValue);
valueAnimator.setDuration(1500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
textview.setText(valueAnimator.getAnimatedValue().toString());
}
});
valueAnimator.start();
}
I'm developing an m3u livestreaming app, and I want to show all of the channels in a listview. Now I don't know how to make a listview appear from the right as shown in the picture with a slide animation when a button is clicked and make it disappear (slide out animation) when I click the button again.
Can somebody help me out ?picture of what I want to achieve
Edit: I tried this, but it didn't work unfortunately.
//slide in imgbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgbuttonisclicked = true;
ValueAnimator widthAnimator = ValueAnimator.ofInt(0, 255);
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
list.getLayoutParams().width = animatedValue;
list.requestLayout();
}
});
//slide out if (imgbuttonisclicked){
widthAnimator = ValueAnimator.ofInt(255, 0);
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
list.getLayoutParams().width = animatedValue;
list.requestLayout();
}
});
}
}
});
You could perhaps play on the width property of the list. Set it to 0 at first then on the button click event animate it to a bigger width. You can learn more about property animation in this article.
Edit: This answer provides a better solution.
In my app I'm using ValueAnimator - it fades one image into another. It works perfectly but only after launching app. When I want to animate images again animator is not working. I tried do use animation.end() in onAnimationEnd listener but app crashes. I tried everything. Can you please help me ? Here's my method to start animation
private void animate() {
if(animator != null)
animator.end();
animator = ValueAnimator.ofFloat(0f, 1f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mPackOneSecond.setAlpha((Float) animation.getAnimatedValue());
mPackTwoSecond.setAlpha((Float) animation.getAnimatedValue());
mPackThreeSecond.setAlpha((Float) animation.getAnimatedValue());
}
});
animator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
startSorting(option);
}
});
animator.setDuration(2500);
animator.start();
}
Basically animate() in onCreate() works perfectly but when I want to restart it - it doesnt. Why ?
Call animator.cancel() before restart.
I want to do a translate animation using this following
public static void move(TextView view){
ValueAnimator va = ValueAnimator.ofFloat(0f, 3f);
int mDuration = 3000; //in millis
va.setDuration(mDuration);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
}
});
va.setRepeatCount(5);
va.start();
}
But I don't know how to use onAnimationUpdate method.
Can anyone help please?
If you really, really, really want to use ValueAnimator for animating translation of the View you can do it this way (finishing your example, assuming you meant translationX.
Bare in mind that you're animating translation from 0px to 3px, so you probably won't see much difference.
fun move(view: TextView) {
val va = ValueAnimator.ofFloat(0f, 3f)
va.duration = 3000 //in millis
va.addUpdateListener { animation -> view.translationX = animation.animatedValue as Float }
va.repeatCount = 5
va.start()
}
ValueAnimator is a great tool for making animations. Usually we have three steps:
Step 1- Create your ValueAnimator class by
ValueAnimator animator = ValueAnimator.ofFloat(start value, end value);
Step 2- Adding one update listener and overriding at least onAnimationUpdate() function
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) animator.getAnimatedValue();
//this value should be used to update properties of views.
//just don't forget to run invalidate function of your views
// to redraw them.
}
});
Step 3-
animator.start();
I have a RelativeLayout with 4 Button side by side. On certain event I wish to change position / Change margin of all four Button with animation.
I am currently doing it with following code
final RelativeLayout.MarginLayoutParams params1 = (RelativeLayout.MarginLayoutParams) button1.getLayoutParams();
final RelativeLayout.MarginLayoutParams params2 = (RelativeLayout.MarginLayoutParams) button2.getLayoutParams();
final RelativeLayout.MarginLayoutParams params3 = (RelativeLayout.MarginLayoutParams) button3.getLayoutParams();
final RelativeLayout.MarginLayoutParams params4 = (RelativeLayout.MarginLayoutParams) button4.getLayoutParams();
final RelativeLayout.MarginLayoutParams params5 = (RelativeLayout.MarginLayoutParams) button5.getLayoutParams();
ValueAnimator animator1 = ValueAnimator.ofInt(params1.rightMargin, (deviceWidth - availableWithForTabs + spaceForTabs));
ValueAnimator animator2 = ValueAnimator.ofInt(params2.rightMargin, dpToPx(78));
ValueAnimator animator3 = ValueAnimator.ofInt(params3.rightMargin, dpToPx(52));
ValueAnimator animator4 = ValueAnimator.ofInt(params4.rightMargin, dpToPx(26));
ValueAnimator animator5 = ValueAnimator.ofInt(params5.rightMargin, dpToPx(0));
animator5.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
params5.rightMargin = (Integer) valueAnimator.getAnimatedValue();
}
});
animator5.setDuration(150);
animator5.start();
animator4.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
params4.rightMargin = (Integer) valueAnimator.getAnimatedValue();
}
);
animator4.setDuration(150);
animator4.start();
animator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
params3.rightMargin = (Integer) valueAnimator.getAnimatedValue();
}
});
animator3.setDuration(150);
animator3.start();
animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
params2.rightMargin = (Integer) valueAnimator.getAnimatedValue();
}
});
animator2.setDuration(150);
animator2.start();
animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
params1.rightMargin = (Integer) valueAnimator.getAnimatedValue();
button1.requestLayout();
}
});
animator1.setDuration(150);
animator1.start();
I feel above code is too much to animate margin change for 5 button. Can any one tell me a better way of doing this? or what other options i have? I have to support sdk 16 +
You might want to rethink your design, using ViewPropertyAnimator could save you you a lot lines of code and is very readable/maintainable.
Using ViewPropertyAnimator is pretty simple :
Button button1 = (Button) findViewById(R.id.button1);
button1.animate()
.translationX(toX)
.translationY(toY)
.setDuration(milliseconds); // add more if you
// like (alpha, startDelay)
// check the docs for available methods
The animation always starts at the current position of the View.
For the sake of cleaner code why dont you try something like this :
Write your own Animation Method that takes a View(in your case a Button), and all the values that you need as parameters (minimal implementation of moving a view on the x and y axis) :
private void animateView(View view, float toX, float toY, int duration) {
view.animate()
.translationX(toX)
.translationY(toY)
.setDuration(duration);
}
Then you can simply call this method on your buttons to animate them individually f.e. :
animateView(button1, 5.0f, 2.5f, 150);
animateView(button2, 2.5f, 1.0f, 150);
of course 5.0f, 2.5f etc are only fictional numbers, you'd have to fill in where you want to move the Views yourself.
Extra Suggestion : It is always nice to use Interpolators to make your animation more life like f. e. :
view.animate()
.translationX(...)
.translationY(...)
.setInterpolator(new AccelerateDecelerateInterpolator())
.setDuration(...);
If this was not thorough enough or anything is unclear let me know.
UPDATE :
If you want to set a listener you can either implement the Animator.AnimatorListener Interface in your class like this :
public class yourClass extends AppCompatActivity implements Animator.AnimatorListener {...
Then you are forced to implement the following methods :
#Override
public void onAnimationEnd(Animator animation) {
// here you can call stuff that should happen when the animation ends,
// f.e. start the next animation
// the method names explain themselves
}
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
Then all you have to do is add another line to your animateView method :
view.animate()
...
...
.setListener(this);
Or you can do it in an anonymous inner class like this :
view.animate()
...
...
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
}
// same methods as above ...
...
)};
Why don't you just animate the whole relative layout containing the 4 buttons.
Also I suggest to use View.animate method
public ViewPropertyAnimator animate ()
Added in API level 12
This method returns a ViewPropertyAnimator object, which can be used to animate specific properties on this View.
Returns
ViewPropertyAnimator The ViewPropertyAnimator associated with this View.