Android- setDuration not Working? ObjectAnimator - android

I'm trying to move an ImageView down using ObjectAnimator. The setDuration() method doesn't seem to impact the duration of the animation. I can't set this animation in the XML as I want to play this dynamically.
ObjectAnimator an= ObjectAnimator.ofFloat(img, View.TRANSLATION_Y, 0, 100);
an.setDuration(5000);
an.start();
Sorta off topic here but say I want to play more than one of those ObjectAnimation after each other, Is there a way to do that? I've looked at the Animator set but I wasn't sure if it would move the actual object like an Object Animator, instead of just appearing as so as in the TranslateAnimation.
Thanks in advance!

Maybe you are testing on Samsung device? In this case, under developer options there is the option Animator duration scale that is set to off by default. If you switch to the normal value you can see the animations.
Hope this help.

LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.loading_popup, null,false);
popup = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
popup.setContentView(popupView);
popup.setOutsideTouchable(true);
ImageView loadingPlane=(ImageView)popupView.findViewById(R.id.loading_plane);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// Step 2: Set the Animation properties
anim.setInterpolator(new LinearInterpolator());
//anim.setRepeatCount(Animation.INFINITE);
anim.setRepeatCount(-1);
anim.setDuration(1500);
loadingPlane.startAnimation(anim);
findViewById(R.id.flyinstyle).post(new Runnable() {
public void run() {
popup.showAtLocation(findViewById(R.id.flyinstyle), Gravity.CENTER, 0, 0);
}
});

Related

Use one animation object for multi views

I want set alpha animation to views but with one object without create many object like this fadeIn1 , fadeIn2 , fadeIn3 etc
View1 , View2 , View3
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setStartOffset(1000);
fadeIn.setDuration(1000);
view1.startAnimation(fadeIn); //I want show this after 1 second
fadeIn.setStartOffset(1500);
view2.startAnimation(fadeIn); //I want show this after 1.5 second
fadeIn.setStartOffset(2000);
view3.startAnimation(fadeIn); //I want show this after 2 second
But all views shows after 2 second together , Why ?
Use AnimatiorSet for this
Note: The below code was in kotlin.
First create an function which returns an animator by taking view
private fun getAnimation(view: View): Animator {
return ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
duration = 1000
}
}
Then after user AnimatorSet.playSequentially to play one after other.
AnimatorSet().apply {
playSequentially(listOf(getAnimation(btn_1),getAnimation(btn_2),getAnimation(btn_3)))
interpolator = AccelerateInterpolator()
start()
}
Please free to ask any query in comment section, I'll try to answer as soon as possible :)
You need AnimationSet ex:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
view.startAnimation(animation);

Touch targets do not move with object when using translate animation?

Whenever I use TranslateAnimation to move an object to a new location, for some reason, the touch target of that object remains in the old position.
How do I change this behaviour?
eg.
public static void hideViewUp (View v, int duration) {
AnimationSet animSet = new AnimationSet(true);
animSet.setFillAfter(true);
animSet.setDuration(duration);
AlphaAnimation alp = new AlphaAnimation(1.0f, 0);
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
-(v.getTop() + v.getHeight()));
animSet.addAnimation(translate);
animSet.addAnimation(alp);
v.startAnimation(animSet);
}
There's no need to post any sample code - the issue is indeed pretty well known and general.
TranslateAnimation and all other old types of animation change only the draw transformation matrix. These animations shouldn't be used to move items permamently.
To truly move a view to a new position, use ObjectAnimator.
Here's an example: http://www.android-app-market.com/animations-in-android-translate-animation-alpha-animation-object-animation.html

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.

Popup Window animation changed programatically in Android

I already have a PopupWindow and a defined XML scale animation in res/anim/ that is applied on show and on dismiss of that popup window. But the case is I want to change pivotX and pivotY of the style of animation dynamically so it starts (grows) from different points of the screen.
So either I'm able change pivotX and pivotY dynamically in res/anim/in.xml or create new ScaleAnimation and apply it. I cannot see how to do the first option, and unfortunately the latter doesn't work when I write:
public class MyPopup {
private PopupWindow popupWindow;
private Integer growFromX;
private Integer growFromY;
public MyPopup(Integer growFromX, Integer growFromY) {
if (growFromX != null && growFromY != null) {
this.growFromX = growFromX.intValue();
this.growFromY = growFromY.intValue();
}
}
public void show() {
LayoutInflater layoutInflater = (LayoutInflater) this.activity
.getBaseContext().getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.map_selector_dialog_layout, null);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 0.0f,
1.0f, 1.0f, Animation.RELATIVE_TO_SELF,
(float) this.growFromX, Animation.RELATIVE_TO_SELF,
(float) this.growFromY);
scaleAnimation.setFillAfter(false);
scaleAnimation.setDuration(4000);
popupView.setAnimation(scaleAnimation);
this.popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}
It doesn't have to be WindowPopup. I can use Dialog or whatever so I can I achieve such a behaviour. Could you help?
few things to tweak to make your code work:
set width and height for the 'popupView' inside the 'popupWindow'
set an interpolator for the ScaleAnimation
either use
popupView.startAnimation(scaleAnimation);
instead of
popupView.setAnimation(scaleAnimation);
or set the start time of you animation before calling 'setAnimation' and make sure the view's parent gets invalidated when the animation starts
replace
this.popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
with
this.popupWindow = new PopupWindow(popupView, width, height);
where width and height have to be bigger than the dimensions of the 'popupView' during and after the scaling process. with LayoutParams.WRAP_CONTENT the ScaleAnimation won't work.
hope it helps

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