I want to rotate an Android View with API level 8. For example, I want to rotate an EditText by 90 degrees so that when the user enters text into a left justified EditText, the first character is at the bottom (rotated 90 degrees) and subsequent characters are entered upwards.
I first tried using an animation with duration of 0, but you still see the field rotate. Unfortunately, this is a non-starter. If I could find a way to hide the animation completely, this method looks to be the simplest.
I then tried rotating the canvas in onDraw which works great for square Views but not so great for ones that aren't square (and I don't control the dimensions of the EditText). I tried various attempts at clipping and translating the canvas, but while I could get the cursor to come into view at the start of text input, it would do weird things once somebody started entering more content (usually the content would disappear out of view).
I also tried making the View square in onMeasure, then rotating the canvas in onDraw, then putting the View dimensions back in a subsequent onMeasure. The first two steps worked great. But the third step produced similar results as described above: things looked ok until the user started entering more text at which point the field text did strange things (usually disappearing).
Has anybody been able to successfully rotate a non-square Android View (an EditText for example) without animation, and with API level 8 or lower?
I've spent a pretty good amount of time trying to rotate a view exactly on API level 8 and I think it's impossible to make it work properly. FYI, it doesn't work correctly on android 3.2 either although there's a setRotation(int angle) method in the View class. This won't work correctly at all for VideoView for example.
P.S back then it seemed that I've managed to make the rotation thing work for anything other than VideoView though. Have you overriden the onTouchEvent method? If you have not your view won't be receiving the touch event since rotating the canvas will just make the view draw rotated but it will still receive the touch events in its old area. You have to manually apply rotation matrix to the touch event coordinates in order to offset them to the proper location.
Related
What is the best way to draw circles on a canvas that should have an alpha layer and change sizes? Should I use a View or a Surfaceview? The circles should also be clickable. And it should be smooth transitions when changing color size and position?
Should I put this in a runnable or use invlaidate in onDraw?
I would prefer that something like this also worked smoothly in low-end devices.
Any suggestions? I'm new to this kind of animations in Android.
If you are constantly drawing and taking user input at the same time, I would use a SurfaceView. However, if the only draw changes you plan on making to the circles happen when you touch them, then a simple View onDraw() override would probably do the trick. In the end it will just depend on what all is going on.
The point of the SurfaceView is to have that separate thread for drawing. If what you're doing could be in any way considered "game-like," then go for a SurfaceView; otherwise, stick with a View.
I say this because I'm currently working on a project with constant drawing using a View. The shapes that I'm drawing respond to touch and you can scroll through the View while it is still invalidating over and over. All this with a View and it still runs just fine on lower-end devices (I've only gone back to GingerBread, though).
Good luck!
I should also mention that in the project drawing in a View, almost everything has various alpha values and what not and runs fine.
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 am trying to make a 3D-looking scrollable list, with elements stacking in a row moving forward and backwards, and all that needs to be 3D-looking (back elements faded out and scaled down, etc). Problem is that the front element, when moving out of the view (more to the front, that is) needs to rotate on X-axis and sort of "fall down", as if it was falling from an end of a conveyor belt.
I have searched far and wide for an elegant solution that does not involve developing a real 3D environment or applying whole code libraries, but could not find anything of the sort. I am really a noob at developing for Android, so I guess I might have overlooked something.
The only solution that came close to it was using a 0-duration animation that applied rotation transformation via Camera class onto View's canvas, but that wasn't a good enough 'cause the View's boundaries were clipping the rotated content, and let's face it - that's a crooked way to apply something as trivial as rotation.
Is there really no simple way to rotate Views? I mean, iOS has it, Flash has it - even CSS3 gives a way to do it without breaking a sweat.
My target is API 10 (2.3.3) and up.
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'm new to Android application development and I'm currently experimenting with various UI ideas. In the image below, you can see a vertically scrolling list of horizontally scrolling galleries (and also textviews as you can see). I'm also doing some matrix and camera transformations which I will come to in a minute.
For the background of the list elements, I use green. Blue is the background of the galleries, and red is the background for the images. These are just for my benefit of learning.
The galleries being used are extended classes where I overrode the drawChild method to perform a canvas scale operation in order for the image closest to the center (width) to be larger than the others.
The list view going vertically, I overrode the drawChild method and used the camera rotations from lack of depth dimension in the canvas functionality. The items in the list are scaled down and rotated relative to their position's proximity to the center (height).
I understood that scrolling and clicking would not necessarily follow along with the image transforms, but it appears as though the parent Gallery class's drawing is constrained to the original coordinates as well (see photo below).
I would love to hear any insight any of you have regarding how I can change the coordinates of the galleries in what is rendered via gallery scroll and the touch responsiveness of said gallery.
Images in the gallery are not same dimensions, so don't let that throw you in looking at the image below
Thanks in advance!
Ben
link to image (could not embed)
-- Update:
I was using my test application UI and noticed that when I got the UI to the point of the linked image and then I touched the top portion of the next row in the list, the gallery updated to display the proper representation. So, I added a call to clearFocus() in the drawChild method and that resulted in more accurate drawing. It does seem a tad slower, and since I'm on the Incredible, I'm worried it is a bloated solution.
In any event, I would still appreciate any thoughts you have regarding the best way to have the views display properly and how to translate the touch events in the gallery's new displayed area to its touchable coordinates so that scrolling on the actual images works when the gallery has moved.
Thanks!
As I updated earlier, the issue of the graphics of the gallery not fully refreshing was resolved by calling clearFocus in drawChild method for the ListView extending class.
The problem with registering the touch events turned out to be where I had used an example for the basis of my experimental program which called a pre-translation on the matrix used for painting. Once I removed that call and adjusted the post-translate call to compensate not having the pre-translate call any longer, I was able to scroll through the galleries regardless of their position, size or rotation (around x axis).