![]() Then you will notice that not a single animation in those examples is ever defined in xml. To futher corroborate this argument look at Google's excellent DevBytes videos about animation: But in general you cannot define anything dynamic in xml. As I said above only view animations provide a way around the static nature by providing the option of specifying fractions instead of absolute values. It works great for dimensions or string translations but sometimes with animations it can be a bit of a pain. So you see, the resources are really just designed for anything that's static and unchanging. Aside from working with selectors you cannot customise the animation for each device. Through selectors you could alter this animation for devices with smaller or bigger screens where the animation may be moving the View too much or too little. Dp is a measurement of physical size, 100dp will be the exact same physical size one any screen with any pixel density. You can for example define an animation that moves a View by 100dp. ![]() then you will realise that it is designed to do one thing: If you look at how the resources work with the many available selectors and also with dp, sp. Those values are static and cannot be changed after compiling your app. As you already noticed every animation you define in xml has absolute start and end values. What we really should be addressing here is what I think is a misconception on your part. But as long as you are able to use view animations this should pretty much solve your problem. There are cases in which one has to use ObjectAnimators and cannot use view animations. I realise that this might not be of any help to you. You can use it like this: Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down) In other words from completely of the screen to the position of the View. This would animate a View from -100% to 0% of the screen height. With view animations you can specify fractions instead of just absolute values: One possible solution is to use view animations instead of an ObjectAnimator. Dynamic values like the screen size of the device the app is installed on cannot be a part of those resources since everything in them is compiled into your app when you build it. And that is also an answer to your other question: There is no resource or constant or anything which would be accessible in xml which contains the current screen size. Everything contained in your xml files is compiled into your APK when you build it and cannot be changed at runtime. You cannot insert any value from code into any xml resource. As I explained above, the setter method defines what actually happens.įor example, calling a custom function that returns the width would beįine, or defining a constant that code can set, having code set saidĬonstant to equal the screen width, then accessing that constant from ObjectAnimators just interpolate from a start value to and end value. With an ObjectAnimator there is no way to achieve that. Is there really no way to get the actual screen pixel dimensions (not So to summarise, the ObjectAnimator itself doesn't actually do much animating, it just calls getter and setter methods through reflection. Only by doing this can the ObjectAnimator do anything else besides animating between absolute values. ![]() They are implemented in a way to set the x value relative to the width of the FrameLayout. The question you are linking to ( Animate the transition between fragments) is subclassing FrameLayout to add a setXFraction() and a getXFraction() method. You need to provide the ObjectAnimator with the necessary getter and setter methods to do its magic as it essentially just calls those getter and setter methods to perform the Animation. If you want to use an ObjectAnimator you have no choice but to subclass the View you want to animate. So I guess my question is: How can I define an animation in XML which will slide a Fragment off screen and at the same time fit on all devices?Įxisting answers on SO suggest subclassing the FrameLayout. On a bigger device the View might not slide of screen completely and on a smaller device the View moves to much and continues moving when he is already off screen. I need to specify an absolute starting and ending position of the View and since different devices have different dimensions I cannot define an Animation in XML that fits all devices. Kind of like the Animation the ViewPager uses. ![]() The Animation animates moving between tabs, for that purpose I need to animate the current Fragment to move completely off screen while the need Fragment slides in from the opposite side. I created an Animation for the Fragments in my application. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |