I just do a simple project where I try to show/hide a layout on the top of a LinearLayout with TranslateAnimation. There was a flicker because when I call onAnimationEnd(), the animation wasn't finished for 0.1sec.
Example:
#Override
public void onAnimationEnd(Animation animation) {
retractableLayout.setVisibility(View.GONE);
}
When I search on stackoverflow, I found there's another way to do it. With ObjectAnimator. After using it, my animation was fine without a View.GONE
What is the difference between TranslateAnimation and ObjectAnimator? Is one of them is deprecated and they do the same thing or there's a time when one or the other is better.
Here's a github repo with the 2 versions (https://github.com/charlesvigneault/AAA_Test1)
Thanks
The difference is mainly that if you use a TranslateAnimation, the view which you are animating does not really leave its original position on the screen, it just makes it look like it is moving. So the view basically doesnt change its coordinates.
Check this video about View Animations :
https://www.youtube.com/watch?v=_UWXqFBF86U
If you use an ObjectAnimator the view really changes its actual position.
TranslateAnimation is not deprecated, you can still find it on Lollipop, but for most cases I can recommend a class called ViewPropertyAnimator , which many people still dont seem to know about, it is probably the easiest and most straight forward way to animate a view, and can also save you a lot of code. Heres an example :
retractableLayout.animate()
.translationX(toX)
.translationY(toY)
.setDuration(duration)
.setInterpolator(interpolator)
.setStartDelay(startDelay);
You can also set a listener etc., be sure to check the available methods.
And check out this really helpful video :
https://www.youtube.com/watch?v=3UbJhmkeSig
TranslateAnimation is depracated since android 3.0 and ObjectAnimator is the way to go. Object animator is much more flexible as it allows you to "animate" any object property that has proper setter and getter implemented. Check official android dev guide
http://developer.android.com/guide/topics/graphics/overview.html
Related
I'm developing an Android App with Xamarin.Android and try to update a View about 50 times per second. I used the RelativeLayout.LayoutParams to change the Margin of the View, so that it is moving around depending on the accelerometer values. Unfortunately this does not work well, because it seems like android does not redraw fast enought. Is there an alternative for LayoutParams, or how can I force Android to redraw my View faster?
The View is only a small ImageView. It should not be a lot of work to draw it to it's new position. So I don't know why this is a problem for Android.
Thanks in advance!
Ok, it seems like the problem was not the android performance. Had some thread based mistakes in my code. Sorry for that. By the way I like to tell you what the solution was.
I wrote a new class extending RelativeLayout and override the onDraw() Method. This class wrapped my other stuff which is needed for calculation and movement. To move the View I also had to call postInvalidate() Method.
Thanks!
I have to make a volume button.
The button have to be used like in real life : Means that the user drag it in circle
The thing is i have no idea how to do it.
I already got through google and the only thing i found was how to do a circular seekbar, which is not good since i have only one image/button
How can i acheive that ?
EDIT 1 :
For now i found this :
http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868
http://upload.wikimedia.org/wikipedia/commons/1/1a/Cartesian_coordinates_2D.svg
Best way is to create your own custom View
But if you don't want to do this.
Then just use this SeekArc around your image and customize its thumb and color to look like your view and change volume as you want.
if you need any assistant,ask me
Custom Rotatory knob
I have two HorizontalScrollViews in my layout and I want to move them at the same. So if I touch scrollview a I want to move scrollview b at the same time and vice versa. I have done some searching around and haven't found a good solution. I have gotten it close by setting an onTouchListener() and trying to manually move each but I can't get it to move smoothly. Any ideas?
I was able to figure out what I needed to do. I noticed that there is a protected method called onScrollChanged() in the ScrollView and HorizontalScrollView classes. So I subclassed HorizontalScrollView and implemented that method manually. From there I just created a callback that gets called when onScrollChanged() is invoked. Works like a charm. Why Google didn't make that method public is beyond me.
I have developed many applications for desktop or web in java, but never for android.
What little I have practiced with was weird because I am used to SWING. Since it uses .xml files for applications I was wondering if you needed this also in games?
For example there is a a good example at http://developer.android.com/resources/samples/JetBoy/index.html. I was hoping someone could explain two things for me.
How is the image drawn to the screen?
How do you listen for user input? (Eg. Clicks, drawing, swiping, ect...)
I have made games on the desktop so you don't have to get in depth, but I found it hard to understand the code. (What I was suppose to do special for android.)
(Another way to put it:)
I guess basically what I am asking is how to I draw an image that will in turn listen to clicks and such without adding a button? Or do I need to add an invisible button? Then if I did have to add a button how would I listen for swiping and drawing?
Also I saw some methods like doDraw(), but did not see where they were putting the image so it would appear on the android device.
JetBoy is rather complicated for a beginner, check these simple examples which are based on the same principles as JetBoy: How can I use the animation framework inside the canvas?
Everything is done by drawing bitmaps on the view's canvas and an event which detects when and where the sreen was touched.
I think this will tell you everything you need to know, in a comprehensive, 31 part series detailing the creation of Light Racer 3d for Android.
If you are not going to use any Game Engine ,
basically you extend android.view.SurfaceHolder
and then overide these methods,
public boolean onTouchEvent(MotionEvent event)
protected void onDraw(Canvas canvas)
go through
these articles.It teaches everything you need from the scratch
I created a simple Animation-derived class and added an onclick listener to a view (a LinearLayout in this example, just to make a quick proof-of-concept) that called startAnimation on the object (passing it an instance of my class). In my Animation class, I simply get a reference to the Translation's Matrix object and call setTranslate. So, the translation looks fine, but if I attempt to click again where I now see the object (the translated location), nothing happens. If I go back to the original location and click, it repeats the same animation from the same starting point. I was under the impression that the translation would actually translate the object itself and not just the visual representation. Is there a way to do what I was expecting to happen with an animation class? Or, is there some other standard way of doing this sort of thing? Any insight into why this behavior is the way it is? Thanks.
Nope the documentation states that it only modifies the matrix. If you want to actually change the rest of the system's interpretation of the view, set an animation listener that sets the position when the animation ends.
You can use View.setTranslationX() and View.setTranslationY(), those should do the trick in Android Honeycomb and after :)