So, much to my chagrin, I found out while testing on an earlier device, that pre-API version 11, the View class has no setAlpha nor setX methods. Can someone suggest an alternative? I'm currently moving a view around by updating its X location, and fading it in and out based on user interaction.
You may use standard Animation to implement such program logic, such as AlphaAnimation and TranslateAnimation to make movement of your View. But also you should keep in mind that original position of View at it's parent layout isn't changed (only pixels are redrawn). That means, that when user would taps on new image of View, this View would not get event, as it's Rect remains in the old position.
You could relocate Views rectangle after the end of animation, using View.layout() method with 4 positioning parameters passed there, but in this case you should consider about your layout nature, because for example if you work with LinearLayout and change the position of it sibling by calling layout() method, next change of one of UI elements size will call reaqestLayout() method and your animated View will come back at it's initial position
Related
Another questions about setting position of a view. If you want to move a view around then you can do setX, setTranslationX, setLeft or LayoutParam.leftMargin
Offcourse there is also overriding the onDraw method and using Matrix/Bitmap/Canvas operation.
I was wondering what's the difference. Do they all ultimetly adjust the same value which is x coordinate of the view?
It would be good to have it all in one post for future readers as well
So far I know,
LayoutParam.LeftMargin: is available for all API (specific under Api
8)
SetTranslation: Difference between original left bound of the view and the new leftbound. Though I heard maybe it is not persistent?
Anyone can shed light on the differences and if they impact different properties or the same property?
I believe that the main difference between setLeft and setX() is that setLeft() is relative to its parent view, where setX() just sets the position relative to the whole screen, which is the same as setTranslationX().
In setLeft() terms it means that the layout system can change its layout position. For example if we would use setLeft while scrolling through a RecyclerView the system would change its position accordingly, so the proper use in this case would be setX().
I am implementing a animated book in Android and the animations are defined by an XML (not an Android XML). The images are positioned in fixed positions and when user touches in a element on the screen, the app plays a sound and animate the imagebutton, changing the image src and positions (X and Y).
I want to know how can I do that. As images dot not have the same size, I think the sprites solution is not a good way to solve it.
Its like the image 2:
Thank you
There are two types of animations:
View Animation and Proprety Animation.
The view animation can only animate View objects. It also lack a variety of animations, since it can do only stuff as scale, rotate, move... It cannot change background color, for example. Also, the dissadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Phisically, it still stays in the same position. That's why the button is unclickable, after the View Animation is finished upon it.
Property Animation, in the other hand, can animate both View and non-View objects and it doesn't have constraints as the View Animation. When objects are moved, for example, with the property animation, they are not just drawn on some other position on the screen, but they are actually MOVED there.
Now, Property Animation is a lot more complex to write than the View Animation, so if you don't really need all the advantages of the Property Animation, it is suggested to use View Animation.
Source: Property vs ViewAnimation
Tutorial and SupportLybrary up to API 1: Nine Old Androids
Is it possible to rotate views in XML with APIs previous to Honeycomb - maybe with the support package? Or is the only way to create a custom class, like described here Vertical (rotated) label in Android
Edit: What I need is a statically rotated view (specifically a TextView, but I guess it's enough to know how to do it with a View). Starting with honeycomb there's a rotation attribute which can be used in XML. I need something like that.
The only thing I have found until now is use an animation with duration 0 but this still moves a bit at start and I don't want that. I tried setting the views invisible and attaching a listener to the animation which makes them visible on animation finished callback, but that made strange results... that changed the position of the views, for some reason.
The best way is with the custom subclass implementation that you linked to, where you can rotate the canvas and resize the view appropriately. This ensures that the view bounds are also set to match the text that is drawn.
The only method of transforming views externally prior to HC is the animation framework, and applying an Animation to the view with a duration of 0 and fillAfter set to true will work, but you may notice flickering on some devices as often the view will render normally on its first frame and then animated to its final position from that point onward. You can work around this by hiding the view and displaying it a bit late...but you can see how hacks are starting to stack up.
In addition, doing an Animation prior to HC will not transform the view bounds themselves, so you won't be able to neatly pack other views around this one because its position from a layout perspective will still be the rectangle calculated for the horizontal (non-rotated) text.
The simple subclass is definitely the preferred method.
HTH
Is it possible to rotate views in XML with APIs previous to Honeycomb
There is RotateAnimation. However, depending on what you are trying to accomplish, that may not meet your needs.
I have a bit specific problem, but hopefully someone will chime in and help.
I've written a custom zoom animation (posting runnables that change the target view to a handler between certain amount of time) but when i change the view's width and height it is as if I've performed a zoom around (0,0) of the view whereas I want to zoom around its center so I move the view by changing its margins accordingly. The thing is though that when the zoom iteration step is too small (i.e 0.01f and less) I have to change the margins of the view by something like 1px sometimes only in one of the directions which makes it look as if the animation is glitchy. I'm not sure of the exact reason but I've tried the following things:
1) I tried changing the margins by overriding onLayout() of my parent view which will be caled when setting the layoutParams upon zooming( I did this in order to avoid a second call to setLayoutParams() upon moving which actually now doesn't seem quite reasonable since setLayoutParams() just sets some flag which will be used later on).
2) I'm checking the new margins to set so that they are set only when there's a difference between the new and the old margins in both X and Y directions.
3)I tried using view.offsetLeftAndRight() view.offsetTopAndBottom() instead of changing the layout params in order to move the view, but it was again to no avail.
Any suggestions what will do the trick?
If I undestand you correctly you need to get Bitmap cache from view and draw it manually. In this case bitmap will be drawn without glitches (if Bitmap filtering is on).
You can do it in following steps:
get view cache - through View.getDrawingCache or by calling View.draw function
hide view
get current system time - SystemClock.elapsedRealtime (thanks to this you can calculate animation progress)
start posting runnables to invalidate your screen and check whether animation is ended
show view
Possibly it can be done via Android Animation class but I do not use it as it quite limited.
I am currently trying to animate a card game using an AnimationSet of Translate and Rotate animations. Although I am using setFillAfter(true) and see the card visibly move and stay in it's new position, the actual position of the ImageView is still at it's old location.
In the Google documentation it says:
Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it. Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.
This means that all of my old onClickListeners, etc are still being associated with the old position rather than the newly updated visual representation, and any new animation applied to the View occur from the initial spot, not the newly located one.
What is the best way to get the effect that I seek (a card sliding from my hand to the center of the screen, which is a different LinearLayout, and then in a later sequence sliding from that spot off the screen)?
I know what I seek is possible as the people at bytesequencing.com have accomplished this smooth animation effect in their android game "Hearts! (free)" which I discovered in the market.
Thanks!