Animation of View in onCreate() method - android

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;

Related

Is there any way to set rotation to the layout parameter

I want to set rotation to the view through Layout Parameter by calling setLayoutParams
Here I can set margins
RelativeLayout.LayoutParams paramsForSpinnerTextView=(RelativeLayout.LayoutParams)spinnerTextView.getLayoutParams();
paramsForSpinnerTextView.setMargins(30,30,30,30);
spinnerTextView.setLayoutParams(paramsForSpinnerTextView);
Same way, Is there any way to set rotation to the layout parameter.
EDIT:
Yes, I can simply rotate the view using spinnerTextView.setRotation(-90) but this is instantaneous and does not show any animation. My parent layout is Relativelayout with a attribute android:animateLayoutChanges="true" until unless I change the child views attribute using setlayoutParams animation attribute of parent will not do anything.
And I also know that I can use rotate animation but that will not move the touch area of the view.
As per I know there is no way to set rotation to the layout parameter. There are other ways to achieve what you want.
Apply rotation animation
Listen to the animation end event
There, just call setRotation(required_angle) on your view

ObjectAnimator in 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

What is the difference between addView and addViewInLayout

I have seen some widgets using addView and sometimes addViewInLayout.
What is the difference between them?
What will happen if I replace one with the other?
Should I keep a flag during layout and use "addViewInLayout" or "addView" accordingly?
Thanks.
BR,
Henry
ps. add more tags: removeview, removeviewinlayout
Its generally a bad idea to call addView inside onLayout because addView internally triggers a requestLayout which eventually will call onLayout. So you end up triggering a layout while you are in the middle of a layout.
addViewInLayout is a "safer" version of the addView in the case you really have to add a new view in onLayout. It basically doesn't trigger a layout pass (doesn't call requestLayout internally).
See this video (by android engineer) for more detail: http://www.youtube.com/watch?v=HbAeTGoKG6k
addViewInLayout
Adds a view during layout. This is useful if in your onLayout() method, you need to add more views (as does the list view for example). If index is negative, it means put it at the end of the list.
addView
Assign the passed LayoutParams to the passed View and add the view to the window.
*Note that addView is implemented by ViewManager, an Interface to let you add and remove child views to an Activity, so you will be able to add views to ViewGroup at run time (DYNAMICALLY). Also note that addViewInLayout is a protected method of ViewGroup so if you are doing to create a custom view group you can call addViewInLayout() in onLayout() method.
For more see this
for example: we have a Layout (mLayout) and you want to add 2 views (view1, view2) into this layout.so there are 2 ways (the same)
case1: simply you use following command
mLayout.addView(view1); //onLayout() will be called first time
mLayout.addView(view2); //onLayout() will be called second time after the first time.
in this case, you don't care function onLayout(). it is simple source code.
case2: complicate but better performance
//do something to <global variable>
bCheck = true; //check it in fuction onLayout()
requestLayout(); //use this function to call onLayout() function for only one time
//in onLayout() function of mLayout, you use addViewInLayout()
//addViewInLayout() dont call onLayout() function, so you can add 2 views with only one time to call onLayout()
//onLayout() is abstract function, so mLayout is a instant of subclass of ViewGroup (ex: RelativeLayout, LinearLayout....)
#Override
onLayout(boolean changed, int l, int t, int r, int b)
if(bCheck == true){
v.addViewInLayout(view1); //add view1 to mLayout
v.addViewInLayout(view1); //add view2 to mLayout
bCheck = false;
}
}
});
I have no time to test it. anyone can help me make it more clear.

How to bring Animation to front: bringToFront and zAdjustment/Z-ordering

I have a LinearLayout with two views in it, next to each other. When a view is tapped upon, it animates to full screen. This goes fine for the view on the right, but it fails for the left view:
I am unable to animate the left view on top of the right view
Without corrupting my layout with bringToFront(). Adjusting the Z-order of my animation does not seem to work.
Not a solution: The problem is gone when I use "brintToFront()" on the left view, but this causes my layout to be completely corrupted afterwards, and there is no brintToBack() function or whatsoever. => brintToFront = not a good solution?
Adjusting the Z-order of my animation does not seem to work (does not change anything).
scaleAnimation.setZAdjustment(Animation.ZORDER_TOP);
translateAnimation.setZAdjustment(Animation.ZORDER_TOP);
AnimationSet set = new AnimationSet(true);
set.addAnimation(scaleAnimation);
set.addAnimation(translateAnimation);
set.setZAdjustment(AnimationSet.ZORDER_TOP);
myFrameLayout.startAnimation(set);
Why does Z-ordering not work as expected?
This should be possible if you extend LinearLayout and override the following method:
getChildDrawingOrder (int childCount, int i)
To make sure layout uses your function you need to call setChildrenDrawingOrderEnabled(true)
See ViewGroup javadoc
For your z reordering will apply you gonna need to request a new layout on the view, but try doing it before starting your animation:
AnimationSet set = new AnimationSet(true);
// init animation ...
scaledView.bringToFront();
scaledView.requestLayout();
myFrameLayout.startAnimation(set);
I guess there is no good answer to this.
I am now animating everything else on the screen too, so if view 1 has to grow larger on top of view 2 than I animate view 2 also, in such a way that it looks like view 1 is growing on top of view 2, while actually view 2 is getting smaller while view 1 is getting larger.
Quite a lot of work and a bad solution, but I guess the only solution.

layout function doesn't change View dimension.

I am trying to create a view that slides up from the bottom of the screen. I tried setting the initial position of the view (which should be offscreen) in xml, but instead of placing the imageview where I specified, it truncated it. My second thought was to set the position of the view programatically inside the onWindowFocusChanged method. Here's my code
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus) {
slide_dock.layout(slide_dock.getLeft(), phone_height - 70, slide_dock.getRight(), phone_height + 230);
}
}
The problem is that this only works SOME of the time. I've been debugging it, and I believe the issue is that the layout values of slide_dock get altered after my onWindowFocusChanged function completes, I'm just not sure where. Can anyone here help me out? Or link me to somewhere that explains the layout process in-depth? I've been searching around to no avail.
Have you tried using the Animation framework? Use a RelativeLayout and align your child view to the bottom of the parent. Then use the following animation, maybe showing and hiding your view appropriately with View.setVisibility(int)
View myView = View(this);
TranslateAnimation slideUp = new TranslateAnimation(myView.getHeight(), 0, 0, 0);
slideUp.setDuration(250); // millis
slideUp.setFillAfter(true); // Required for animation to "stick" when done
myView.startAnimation(slideUp);
You might have to play with the TranslateAnimation constructor parameters to get it to work right (this is from the top of my head).
So I figured out the cause for the issue above, and I'm posting it here in case anyone ever runs into the same problem.
The reason why the ImageView was resizing was because in ImageView's onMeasure function, it resizes itself if it doesn't think that it will fit onto the screen. You can view the ImageView source here and see how it works: http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/widget/ImageView.java&d=3
To work around this, I created a custom view that extended ImageView and overrode the onMeasure method. In my new onMeasure method, I simply called setDimension to give my new view the dimensions that I wanted it to have, this effectively stopped the view from resizing as it was doing earlier
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), 300);
}

Categories

Resources