Android uses animation effects after the control position is processed like a system notification bar under pull drawing

Source: Internet
Author: User

For the use of Android animations, please refer to. Android animation, in terms of design, I do not understand a bit, I think this is very strange, because after the control animation, even if set to stay at the end of the animation position, we do see the control is parked in that position, but the actual position of the control is still in front of the original animation there. For example, if you have a button, you animate it, let it move to another location, and when the move is done, you will find that clicking on the button has no effect, while the button's original position, which is the position before the animation, does not have any controls, But you see the effect of clicking the button. I don't know why Google designs this way.
Solution: The animation is not set to stop after the end position, through the Setanimationlistener method to set the animation listener, at the end of the animation, that is, the Onanimationend method, Manually move the control to the end of the animation using the layout or Setlayoutparams method.
Example Description: At startup, such as a button, the button has a high 10 pixels of white bar, in fact, this is another view, but I set it to negative Y axis, so only to see part of the other part is hidden in the top of the screen.

When you press the drop-down button, the white view moves downward, which is the effect of the next pull, which appears on the screen, such as:

When the click is hidden, this view will pull up again, reverting to the appearance of the first image, showing the original drop-down display button.

Source:
Layout main.xml:

1234567891011121314151617181920
<?xml version= "1.0" encoding= "Utf-8"? ><absolutelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "><!--because the following two linearlayout to overwrite , that is, the display has overlapping positions, so you must use Absolutelayout, absolute layout--><linearlayout android:orientation= "vertical" android:id= "@+id/ Main "android:layout_x=" 0DP "android:layout_y=" 0DP "android:layout_width=" fill_parent "android:layout_height=" fill _parent "android:weightsum=" 1 "><button android:layout_width=" wrap_content "android:id=" @+id/show "Android: layout_height= "Wrap_content" android:text= "drop-down display"/></linearlayout><linearlayout android:orientation= " Vertical "android:id=" @+id/selection "android:layout_x=" 0DP "android:layout_y=" -190px "android:layout_width=" Fill_ Parent "android:layout_height=" 200px "android:background=" @color/white "><!--the reason for the PX unit, Because the parameters of the layout method for setting the control position are PX units, the same unit is convenient to calculate the high 200,y coordinates-190, there are 10 pixels to see, is to facilitate the demonstration, of course, you can also set to 200, just all hidden--><button Android:layout_Width= "Wrap_content" android:id= "@+id/hidden" android:layout_height= "wrap_content" android:text= "hidden"/></ Linearlayout></absolutelayout>

Two XML files describing animations, show.xml and Hidden.xml:
Show.xml:

1234567891011
<?xml version= "1.0" encoding= "Utf-8"? ><set  xmlns:android= "http://schemas.android.com/apk/res/ Android "><translateandroid:fromxdelta=" 0 "android:toxdelta=" 0 "android:fromydelta=" 0 "android:toydelta=" 190 "Android:duration="/> <!--coordinates appear to be relative to the starting position of the control, not relative to the parent control--></set>

Hidden.xml:

12345678910
<?xml version= "1.0" encoding= "Utf-8"? ><set  xmlns:android= "http://schemas.android.com/apk/res/ Android "><translateandroid:fromxdelta=" 0 "android:toxdelta=" 0 "android:fromydelta=" 0 "android:toydelta="- "Android:duration=" "/></set>"

Main program code:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
Package Com.test; import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.view.animation.animation.animationlistener;import Android.widget.absolutelayout;import Android.widget.button;import Android.widget.linearlayout; public Class Androidtestactivity extends activity{/** called when the Activity is first created. */linearlayout main, selection; Button hidden, show; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); main = (LinearLayout) Findviewbyid (r.id.main); selection = (LinearLayout) Findviewbyid ( r.id.selection); hidden = (Button) Findviewbyid (R.id.hidden); show = (Button) Findviewbyid (r.id.show); Show.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubanimation SHOWANIM=ANIMATIONUTILS.LOadanimation (Androidtestactivity.this, r.anim.show);//translate.setfillafter (true);//If you save the moved position with the Setfillafter method only                                  , the real position does not move selection.startanimation (Showanim); If the view below is a ListView, you may need to execute Selection.requestfocusfromtouch () first, otherwise the animation will not be displayed for the second time//the following controls need to be ENA BLE is set to False to prevent the following control in the point show.setenabled (FALSE),//must be set to false, because if the continuous click two times, will be executed two times the moving position, not the results we want//and so pull down the view after the move back, Then set to Trueshowanim.setanimationlistener (new Animationlistener () {  @Overridepublic void Onanimationend (Animation Animation) {//TODO auto-generated method Stubhidden.setenabled (TRUE);//After the draw is finished, set the hidden button to the available selection.clearanimation (); Selection.layout (Selection.getleft (), 0, Selection.getright (), $);//selection.setlayoutparams (new Absolutelayout.layoutparams (AbsoluteLayout.LayoutParams.FILL_PARENT, 200, 0, 0) {});//The above two lines function the same//move the control to the end of the animation position, Clearanimation method can clear the animation, the screen will not flash, no flash//setfillafter can not be true, even if true, the control's true position will not change, but we see the position will change, if the layout method, The position we see will also change System.out.println (selection.GetLeft () + "+selection.gettop () +" "+selection.getright () +" "+selection.getbottom ());//The position after the output is moved, tested, If you do not use the layout method to move the control, the position of the control is unchanged before the animation, or/So, what you see is not the truth} @Overridepublic void Onanimationrepeat (Animation Animation) {// Todo auto-generated method stub} @Overridepublic void Onanimationstart (Animation Animation) {//Todo auto-generated Method StubSystem.out.println (Selection.getleft () + "" "+selection.gettop () +" "+selection.getright () +" "+ Selection.getbottom ());});}); Hidden.setonclicklistener (New Onclicklistener () {  @Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubanimation Hiddenanim=animationutils.loadanimation (Androidtestactivity.this, R.anim.hidden); Selection.startanimation (Hiddenanim); hidden.setenabled (false); Hiddenanim.setanimationlistener (new Animationlistener () {  @Overridepublic void Onanimationend (Animation Animation) {//TODO auto-generated method Stubshow.setenabled (True); Selection.clearanimation ();//selection.layout (Selection.getleft (), -190, Selection.getRight (), Selection.setlayoutparams (New Absolutelayout.layoutparams (AbsoluteLayout.LayoutParams.FILL_PARENT, 200, 0,-190) {});//above two lines, functionally identical}  @Overridepublic void Onanimationrepeat (Animation Animation) {//TODO auto-generated Method stub }  @Overridepublic void Onanimationstart (Animation Animation) {//TODO auto-generated method stub& nbsp;}});   }});  }}

The

Source code does not pass, basically all above, except a definition white strings.xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.