ObjectAnimator in android - android

I want to show a objectanimator in android for a imageview.This imageview should move from top of the screen to about 30 pixels down from top of the screen.Once it(imageview) reaches there and onclick of the imageview the image view should disappear.I have read that i cannot use translate animation as the onclick event wont get fired and hence i will have to use Objectanimator.
Can somebody please help me out in this ...stuck up for a long time now.
Following is my code i have used however it does not seem to work also i do not understand what parameters(for that points) i need to pass.
ObjectAnimator scaleXOut = ObjectAnimator.ofFloat(tv, "translationX", 1f, 9f);
AnimatorSet set = new AnimatorSet();
set.play(scaleXIn);
set.setDuration(1000);
set.start();

you can use translateAnimation and add an animationlistener so when animation ends its onAnimationEnd() method will fired up, so inside it you can put the codes that was in onclick method, or you can put an if statement inside your onclicked method to check whether animation ends, for more about animations you can check out these resources
http://www.youtube.com/playlist?list=PLxLa2tjhVPOZ9KMqanm12_xPDBjKPZWgM
and
http://www.vogella.com/articles/AndroidAnimation/article.html

Related

Animation of View in onCreate() method

I want to animate TextView when activity has been launched, in onCreate() method. However, when I try to get view's height or width it's always 0. In my case it's important to know view's height or width because I want to set the translation for Y or X. For example, consider this code:
TextView text=(TextView)(findViewById(R.id.text));
text.setText(R.string.some_text);
text.setTranslationY(-text.getHeight());
ObjectAnimator moveTextAnimator=ObjectAnimator.ofFloat(text,"translationY",-text.getHeight(),0);
moveTextAnimator.setDuration(1000);
ObjectAnimator fadeTextAnimator=ObjectAnimator.ofFloat(text,"alpha",0,1);
fadeTextAnimator.setDuration(2000);
AnimatorSet textAnimatorSet=new AnimatorSet();
textAnimatorSet.playSequentially(moveTextAnimator,fadeTextAnimator);
textAnimatorSet.start();
I want to run this code in onCreate() method. Do you know how to solve this problem? I believe that one of the solution is just simply to wait until the view will be drawn. However, what if I would like to run the same animations for every single view in my layout? Do I need to wait for every view? And how can I do it?
simply use text.post(new Runnable(){});
call text.getHeight() in the runnable will get correct result;

Android Animation : View directly change to end position

I get an assignment to create a game like candy crush saga and flow for android platform. I've try some tutorial about event listener and animation, it work well but I got problem when I animate ImageView to change its position.
I want to make change position between two ImageView when I swipe on of ImageView (candy crush gameplay), it was changed position perfectly but the problem is the animation movement is not working. When I swipe one of the ImageView (Right swipe for example), that ImageView changed position with ImageView which located beside it but I don't see movement animation like when I play candy crush saga.
I use 2 ObjectAnimator to animate that ImageView and play it together when Swipe Event was trigger on that ImageView. this is my animation code.
final String name = "x";
ImageView target = (ImageView) findViewById(id[x-1][y-1]);
ImageView next = (ImageView) findViewById(id[x-1][y]);
ObjectAnimator aTarget = ObjectAnimator.ofFloat(target, name, target.getX(), (target.getX()+target.getWidth()));
aTarget.setDuration(1000);
aTarget.setInterpolator(new LinearInterpolator());
ObjectAnimator aNext = ObjectAnimator.ofFloat(next, name, next.getX(), (next.getX()-next.getWidth()));
aNext.setDuration(1000);
aNext.setInterpolator(new LinearInterpolator());
AnimatorSet moveset = new AnimatorSet();
moveset.playTogether(aTarget, aNext);
moveset.start();
I use ID array to save my ImageView's ID and I using 'for' to compare View's ID which trigger swipe event with list ID inside ID array so I can find where ImageView which choosen to be moved (target) and other ImageView which will changed position with choosen one.
I don't understand what wrong with that code, I get that code from animation tutorial source code from
http://www.101apps.co.za/articles/a-property-animation-tutorial.html
It works perfectly when I run the application but when I try to implement it to my application I got this problem.
Hope somebody can help me, Thanks so much before.

Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). I have another animation working as intended to show/hide a seperate layout. When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. Then when i show the other view, it plays all 3 animations (not intended). I can't figure out why this is happening? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3.
My code for initiating the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
layoutMsgs.startAnimation(anim);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
tvMsgs.startAnimation(tvAnim);
My code for hiding the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
layoutMsgs.startAnimation(animLayout);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);
tvMsgs.startAnimation(animMsgs);
Then this is the other animation that is working fine, it's only one animation, no textView, just a layout
LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
pokeLayout.startAnimation(anim);
So how can i fix this? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information.
solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension
wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. Would still like to know if theres a better solution

Android animation moves the Layout, but not the buttons

Say I have a "main screen" Layout which has to move up, to reveal a "history bar".
if ( HistoryBar == false )
{
HistoryBar = true;
TranslateAnimation MainScreenPullUp = new TranslateAnimation(0, 0, 0, -200 );
MainScreenPullUp.setDuration(350);
MainScreenPullUp.setFillAfter(true);
LinLayMain.startAnimation(MainScreenPullUp);
}
Right now I am doing it like that. But the problem is, that while the main screen layout itself moves up, all the button places keep the same, i.e. the button picture moves, but the click-spot remains in place. Any ideas how to fix that?
I tried useing an animationListener, instead setFillAfter(true) , which would TanslateY the main screen layout, once the animation stops, but it makes the layout jump and flicker. Are there any other ways?
edit:
Thanks, Vineet.
Got it working with:
ObjectAnimator pullUp = ObjectAnimator.ofFloat(LinLayMain, "translationY", 0f, -200f);
pullUp.setDuration(350);
pullUp.start();
I faced the same problem. From 3.0 android has property animation. Below 3.0 I tackled this issue with the help of view's method offsetLeftAndRight (int offset) and offsetTopAndBottom (int offset). You can refer this link for more details.
For Property animation refer this link.

Apply Two Animations to the Same View?

I applied an entry animation to one of my ImageButton (fade_in.xml which is in the project anim/ folder). Now, after a button click, I want to apply an exit animation (fade_out.xml which is in the same folder)
When I do that, the entry animation happens. However, the exit one do NOT !! It seems that each View will accept ONLY one animation.
Is this true? How can I work around this problem?
-
-
UPDATE:
This is in the onCreate() method for setting the entry animation:
Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fade.setStartOffset(600);
img.startAnimation(fade);
img.setvisibility(View.VISIBLE);
And this is in the onClick() method for some button b1:
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_out);
fade.setStartOffset(500);
img.startAnimation(fade2);
img.setvisibility(View.INVISIBLE);
You can use ViewFlipper with getInAnimation and getOutAnimation methods.
Other solution is setting animation in your code(as far I understand you set animation in xml file).

Categories

Resources