Android: How to make horizontal progress bar using Interpolator? - android

I have a progress bar view like this:
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"/>
It lasts 3 second, so how to use interpolator to make update smoothly?
ObjectAnimator animation = ObjectAnimator.ofInt(what_is_in_here?);
animation.setDuration(3000); // second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
I really appreciate your help. Thank you very much in advance.

I found out the solution:
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
animation.setDuration(3500); // 3.5 second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
Here is a detailed explanation:
create an animation object:
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
progressBar: reference to the ProgressBar in the layout;
"progress": the name of the property to be animated;
100: starting point of the animation;
0: ending point of the animation.
and set an interpolation:
animation.setInterpolator(new DecelerateInterpolator());
It is possible to use different interpolators for our animation, like for example:
LinearInterpolator: where the rate of change is constant.
DecelerateInterpolator: where the rate of change starts out quickly and and then decelerates.
AccelerateInterpolator: where the rate of change starts out slowly and and then accelerates.
OvershootInterpolator: where the change flings forward and overshoots the last value then comes back.
For other interpolators check the interface android.view.animation.Interpolator.

Related

Android studio non-stop moving image from both sides

I have an image, which is a bee. I want to add an animation that make the bee look like it is randomly flying up and down within a specified area. The bee should only fly within paddingTop="200dp" to paddingTop="370dp". When it reaches the left of the screen, it should fly back to the right of the screen. And it should be non-stop moving from both sides. However, I can only make it fly or move horizontally with a specified time period.
<ImageView
android:id="#+id/bee"
android:paddingTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/beeFaceRight" />
The code in Main.java
ObjectAnimator animation;
ImageView bee;
protected void onCreate(Bundle savedInstanceState) {
bee = (ImageView) findViewById(R.id.bee);
animation = ObjectAnimator.ofFloat(bee, "translationX", 100f);
animation.setDuration(2000);
animation.start();
}
I have tried this, but it does not work.
ObjectAnimator objectX;
ObjectAnimator objectY;
AnimatorSet animatorXY;
objectX = ObjectAnimator.offFloat(bee, "translationX", 0);
objectY = ObjectAnimator.offFloat(bee, "translationY", 200);
animatorXY.playTogether(objectX, objectY);
animatorXY.setDuration(500);
animatorXY.start();

Bounce animation with depth

how i can add bounce animation to view which will go down and come up but it should do this only for one time .
if i set bounce interpolator with y its bouncing for given duration
but i want it for one time only but it should go say 5dp down and up to current view
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 50, 0);
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
Use OvershootInterpolator.
Doc:
An interpolator where the change flings forward and overshoots the
last value then comes back.
You can adjust the tension of the overshoot using the constructor with tension parameter:
OvershootInterpolator(float tension)
tension: Amount of overshoot. When tension equals 0.0f, there is no
overshoot and the interpolator becomes a simple deceleration
interpolator.
Default value of tension is 2.0f.
Example using ViewPropertyAnimator:
targetView.animate()
.translationY(50)
.setInterpolator(new OvershootInterpolator())
.setDuration(200);
using ObjectAnimator:
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 50); // pass only start and end values.
animator.setInterpolator(new OvershootInterpolator());
animator.setDuration(200);
animator.start();

How to change animation speed when is playing?

I have Animation and i need to change duration or speed in run.
How to do this?
Animation fadeIn = new AlphaAnimation(1, 0);
fadeIn.setInterpolator(new AccelerateInterpolator()); //add this
fadeIn.setDuration(800);
fadeIn.setFillEnabled(true);
fadeIn.setFillAfter(true);
bt1.startAnimation(fadeIn);
You can use Interpolators to have a variable speed during animation. Example can be found here.
I think you need two animations with different speeds. After the first has finished, the second gets played.
Animation anim1 = new AlphaAnimation (1, 0.5F);
anim1.setDuration (600); //The slow part.
//set other things for anim1
Animation anim2 = new AlphaAnimation (0.5F, 0);
anim2.setDuration (200); //The fast part.
//set other things for anim2

How to apply animations to entire Android Listview

I am trying to have a Listview fade out/in under certain circumstances. I'm running into issues when running the animation, the Listview just becomes invisible and no animation is performed.
I've tried running the fade_out animation:
Animation out = AnimationUtils.loadAnimation(mainActivity, R.anim.fade_out);
out.setInterpolator(new LinearInterpolator());
myListView.startAnimation(out);
As well as an AlphaAnimation
AlphaAnimation animation1 = new AlphaAnimation(1f, .5f);
animation1.setDuration(2000);
animation1.setFillAfter(false);
myListView.startAnimation(animation1)
and finally simply setting the alpha
myListView.setAlpha(.5f)
The best I can tell is that alpha transparencies are not supported with a list view and only values of 0 and 1 are available as alpha. I can't find any documentation confirming or denying either way.
Is there some kind of known issue with ListViews? Am I doing something wrong? Or is this some kind of limited functionality on ListViews?
Just try it's.If you want to do visible :
public void viseb_show(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.VISIBLE);
}
or if non visible
public void viseb_gone(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.GONE);
}
anim it's you object who mast be visible.
I figured out my issue. emigrantdd sent me in almost the right direction, how ever there were a few problems I needed to overcome.
First I had to alter emigrantdd's code to:
public void viseb_gone()
{
AnimationSet set = new AnimationSet(false);
Animation fadeout= new AlphaAnimation(1.0f, 0.0f);
fadeOut.setInterpolator(new LinearInterpolator());
fadeOut.setDuration(3500);
set.addAnimation(animation);
myListView.startAnimation(fadeOut);
myListView.setVisibility(View.GONE);
}
As there was some problem with the interpolator.
The other issue I had was that the ListView I was testing on was still in a ScrollView as a part of an old design, once it was taken out it worked as was intended with the changes above.

set the animation to the activity without xml file

Hi I want to set the animation to an activity without xml file.Please give me some suggestions on this topic.Thanks in advance
AnimationSet myAnimation = new AnimationSet(true);
// Create a translate animation
/* TranslateAnimation animation=new TranslateAnimation(0,0,237,0);
animation.setDuration(250);
// Add each animation to the set
myAnimation.addAnimation(animation);*/
ScaleAnimation scale = new ScaleAnimation((float)0.5, (float)1, (float)0.5, (float)1);
scale.setFillAfter(true);
scale.setDuration(500);
<activity name>.startAnimation(scale);
private Animation createAnimation() {
final Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(1000);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
return animation;
}
Simple alpha animation, the way to create others is pretty much the same.
Use view.startAnimation(yourAnimation) to start animation, and yourAnimation.reset() before reusing the same animation again.
P.S. Maybe I misunderstood you, and you want an animation between activity changes?

Categories

Resources