Android initialize Animation - android

I couldn't find examples of how to initialize the animation object.
example Animation ticketAnim;
well new Animation(); is not a valid object it seems so I can't just do Animation ticketAnim = new Animation(); but I would like to. I take the suggested initialization route that the IDE offers which is Animation ticketAnim = null;
of course, accessing this will result in a null pointer exception
what is the right way to do this?

When declaring a new animation, you need to use the constructor of an animation type. Here's some sample code for one of the animation controllers I use in my code:
private void addDeleteDropAnimation() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(300);
set.addAnimation(animation);
controllerDel = new LayoutAnimationController(set, 0.5f);
vw_delLinearLayout.setLayoutAnimation(controllerDel);
}
The Animation class itself is just an abstraction. To use an animation, implement one of Animation's direct know subclasses (also specified in the link to the Animation API).
These include:
AlphaAnimation
TranslateAnimation
RotateAnimation
ScaleAnimation
If you want, you can also create your own custom animation by extending the Animation class. A good example of creating a custom animation can be found here.

Related

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.

Shading sub Layout in android

I am stuck in a condition that I can't keep the sub layout faded after using the fade animation
FrameLayout mapLayout = (FrameLayout)findViewById(R.id.mapLayout);
Animation fadeAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mapLayout.startAnimation(fadeAnim);
Please guide how can I achieve this in android. This animation and shaded effect occurs when I click the 'Pause' Button and to reverse the effect I will press the 'Resume' Button(Sorry I made the image with 'Pause' button in shaded view, in actual it is 'Resume')
Try
fadeAnim.fillAfter(true);
fadeAnim.execute();
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
hii makki you can set the below code at your onclick event
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
then fade out animation
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
Both above answers helped me in solving the issue, but both answers weren't the actual solution to my problem. The solution I made is
I defined two animations according to my need
Animation fadeOut = new AlphaAnimation(0f,0.5f);
Animation fadeIn = new AlphaAnimation(0.5f,1f);
fadeOut.setFillAfter(true);
fadeIn.setFillAfter(true);
When I clicked 'Pause' button I executed following line
mapLayout.startAnimation(fadeOut);
Then to resume from paused state, I executed following line
mapLayout.startAnimation(fadeIn);
Thanks Simon and Deepak!

Listview and animation

I'm using listview animation from API DEMOS, example 2. here's the snippet from OnCreate method:
ListView listview = (ListView) findViewById(android.R.id.list);
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(200);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
listview.setLayoutAnimation(controller);
At some point in the future, notifyDataSetInvalidated() is called upon list's adapter, and my list is refreshed. but the items are not shown in animation any more.
Please help.
If you want to reanimate your LayoutController after your data set changes, call the method startLayoutAnimation() of the view.
You should check out this thread about difference between notifyDataSetChanged() and notifyDataSetInvalidated() (Link updated to point to Romain Guy's answer!)
In short the difference is:
notifyDataSetChanged: The items in the data set are changed (added / removed / updated / reordered, whatever).
notifyDataSetInvalidated: The data source of the adapter is no longer available.
Here you can find a working sample with notifyDataSetInvalidated function in use. Probably it will do the trick with your animation problem as well.

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