Perform animation while view is being dragged - android

I have a view which contains 2 image view that is using the ondraglistener to move them around the screen and that all works fine. I am now trying to animate one of the image views but I'm not sure if this effect is possible.
The animation works correctly but I can't get it to stick with the container view while it is being dragged around and still animate at the same time. It looks like once you start the dragging the view just freezes until the drag is finished. Any pointers on how to accomplish this or if it is even possible? Thanks for advice.
EDIT: It looks like I will have to override the DragShadowBuilder so I guess the next question is whether you can send in an animatable view to the DragShadowBuilder...
EDIT: I guess you can't do it with DragShadowBuilder at all so attempting to do it by overriding onTouch...

So a solution here is to override onTouch and kick off the animation when the click is first 'touched down' and then on touch move just update the margins (or use an x/y translation) based of the motion event's getX and getY and then when 'touch up' just stop the animation. Also the on touch has to be on a wrapper view rather than on the view where you are resetting the layout parameters. I can post the code if anyone is looking for a more concrete explanation.

Related

Android Animation - Button stays clickable

I am making a game in which I have 5 buttons, looking like clouds, falling from the "sky".
That means that when my activity starts, 'clouds' cannot be seen, since the marginTop is set to -100dp.
From that position they start falling down untill they get lost on the bottom side of the screen.
The thing is, I need those buttons to be clickable, during the process of animation.
So far, I found some documentation about how I can make the buttons clickable AFTER the animation ends. But I don't need that. I need to be able to click on the buttons through the animation time itself.
NOTE: I need something that works with versions before 3.0.
Anybody has any link to documentation or some example or anything ?
After doing some research, I found out that there are two types of animations:
View Animation and Property 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 disadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Physically, it still stays in the same position.
That's why the button is un-clickable, 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 View Animation
Tutorial and SupportLybrary up to API 1:
nineoldandroids
You can change the buttons to imageViews and then do
imageView.setOnClickListener(myListener)
then set myListener to do whatever you previously wanted to happen on the buttons onClick. Your activity will have to implement OnClickListener
Added bonus: you can make the images look like clouds :)

Implement Slide To Open

I want to implement the concept of "Slide to open" concept in my application. Basically, I am using an ImageView. If the user slide it to a specific direction (to the right for example), the next activity is started. I do not know how to implement this in Android, I have searched in lots of place but found nothing.
Any clues ?
What I came into :
No I do not think that the use of ViewPager or HorizontalScrollView is appropriate in this case.
I don't know if the concept of Drag And Drop helps (how to imply a drag in a specific direction ? How to let the ImageView return to its initial location if the drag ends midway ? ...)
Use frame layout and put a ImageView in it. Then add touchEvents to Imageview. update imagViews Layout parameters at ON_MOVE event.This is the basic idea of slide and drag stuff in android.Good luck.
you may use View.Animation for moving back the slider to start position again ON_UP event
you can move view only in x axis by only changing the views :
LayoutParams.leftMargin=event.getRawX();
(always set the gravity to NO_GRAVITY)
If you want to implement smth like "slide to unlock" you can implement drag and drop and forbid moving by Y axis for example. Also you can check image position and analys if this shift is enough to open\unlock, and if not - simple move this image back.

Android: How to Auto Scroll a GridView vertically on dragging an item to the edge

I am trying to implement drag and drop on a GridView in android (ICS), but when I drag the item to the edge of the screen, the GridView doesn't scroll. How can I implement this functionality?
First of all if you don't need to support android 2.x ( API <11 ) this Andorid Drag and Drop tutorial is what you are looking for. I've never tried that way cause i had to implement it on 2.x but i took a look at it seems pretty straight forward.
To implement the autoscroll i guess you could use ACTION_DRAG_EXITED or ACTION_DRAG_LOCATION on the grid view and fire the scroll manually when you see that the location is next to the view bounds.
If you instead have to implement it on 2.x then it's going to be much more hard, you basically need to implement all the DRAG events, or a subset of them, by yourself.
I did in once with a ListView and what you have to do is:
Override the gridView touchEvent.
Use the actions ACTION_MOVE, ACTION_DOWN, ACTION_UP to fire the Drag events
Enable the drawing cache on the GridView items( setDrawingCacheEnabled ) and use getDrawingCache() to copy the bitmap of the view on DRAG_START.
Than what you have to do is to use that bitmap drawn on top of the GridView at the position that the ACTION_MOVE event gives you.
it's not easy but if you take your time you can manage to get it.
what more, I haven't check today but maybe you can try to google a bit an see if someone took the time to implement it and release it Open Source.

Vertical smooth scrolling of values in a text view

I have a text view where in i have to keep ages between 1-99. I also have two buttons ^ and v(i mean up and down arrows) on clicking them the values of the age should smoothly scroll to next or previous value.
I have been trying different ways but couldnt achieve smooth scrolling. Can anyone please give me any idea of how to achieve the task.
I think the easiest way is to simply put the TextView within a ScrollView, and let the arrows interact with the ScrollView, by using things like the fling method and playing around with the velocity parameter to suit your needs.
Use the animation framework.
When pressing down, start the 'down'-animation.
When pressing up, start the 'up'-animation.
Read more about animation here: http://developerlife.com/tutorials/?p=343
View animation is not much matured and hence i am noy sure if that can be used for moving the views.
Please find the description below:
Another disadvantage of the view
animation system is that it only
modified where the View was drawn, and
not the actual View itself. For
instance, if you animated a button to
move across the screen, the button
draws correctly, but the actual
location where you can click the
button does not change, so you have to
implement your own logic to handle
this.
Source
To scroll smoothly you can try using the scroller component.
Reference
What you would need to do is pass the duration of the scroll in the constructor and then use the property
setFinalY(int newY)
to increment the counter position by 1 unit (equal to the height of the item).
Please let me know if that helps!

ViewFlipper caching issue

The views are not cached in a ViewFlipper. Is there a way wherein we can get an image of the view and show it to user so that he sees the Ui as we see on Home scrren(when we swipe the previous view also moves along and when we lift our finger, only then the next view is shown completely.)
What I want to do is that when the user starts moving his finegr on screen, the view should also move along(create an image of view).
I am not getting to do this, as when we swipe the present view goes and next view comes, we do not get both visible when we r moving our finger on screen.
Please if anyone gets what I am trying to do, do help me.
Thanks,
Farha
It's tricky to get scroll and swipe tracking working on Android, while using ViewAnimator or its subclasses.
They allow you to set in and out animations and start them at a given moment, but they work with discrete, either-this-or-the-other-view animations. They are actually using FrameLayout and after in or out animation is executed, other views' visibility is set to View.GONE to hide them from showing up under/over your current View.
The Launcher and the Gallery application are actually doing the functionality you want, by using a different approach.
They track the user touch input (onTouchEvent()), on MotionEvent.ACTION_MOVE they perform animations manually and on MotionEvent.ACTION_UP snap to the appropriate view, just like in the iPhone.
Unfortunately, this approach is actually more complicated than it looks like.
With the manual handling, you have to ensure that you are taking care of everything related to the touch input. This includes a lot of flag-raising, value-checking, event-delegating, etc.
If you want to get better acquainted with this, take a look at these classes from Gallery3D or Launcher's source code.
One other way to get nice horizontal scrolling is to use HorizontalScrollView.
You have to figure out a way to recycle your views, like you would with a ListView and you have to add the snap-to-view logic, but if you have to take care of a small number of views it could be the easiest approach.
Hope that helps.

Categories

Resources