I'm new to android development and ran into an issue when trying to fade a button out and then back in. In my app i'm using
myButton.animate().alpha(0f).duration(200)
to fade a button and after some other interaction with ui elements i'm doing
myButton.animate().alpha(1f).duration(200)
and this works fine. What i want to do now is fade the button out and then back in almost immediately so i tried:
myButton.animate().alpha(0f).duration(200)
myButton.animate().alpha(1f).duration(200).startDelay(300)
Unfortunately this two line do not seem to do anything. The button does not change alpa at all and stays visible throughout. Can anyone please explain what's happening? Does it have something to do with animations being transient?
You should use withEndAction instead of just listing the commands one after another.
myButton.animate().setDuration(200).alpha(0).withEndAction(new Runnable() {
#Override
public void run() {
myButton.animate().setDuration(200).alpha(1f).start();
}
}).start();
I have a simple activity that shows an animation with ObjectAnimator. The animation is created and started in onCreate method of the activity, it is a very simple animation:
cloudAnim = ObjectAnimator.ofFloat(cloud1ImageView, "x", sw);
cloudAnim.setDuration(35000);
cloudAnim.setRepeatCount(ValueAnimator.INFINITE);
cloudAnim.setRepeatMode(ValueAnimator.RESTART);
cloudAnim.setInterpolator(null);
cloudAnim.start();
it simply displays a cloud on the left of the screen and moves from the left to the right.
The problem is that in my nexus 5 (android 4.4 lastet version) the cloud is doing a frame jump when the activity starts.
This jump is only visible in my nexus 5, because i'm testing the app also in a huawei ascend y300 devide with android 4.1 and the jump is not visible, the movement is very smooth.
What is wrong with ObjectAnimator and Android 4.4?
Thanks
Starting animations in onCreate is not a good idea. When user will finally be able to see this animation (after activity being inflated and displayed on the screen with animation etc.) the animation is not in it's beginning but a bit after it, so user will miss the very beginning of the animation or perhaps will see some frame drops then as well. The final result really depends on the device, android version, standard window animations styles etc.
If you want to launch an animation right after creating an activity make use of onWindowFocusChanged method:
http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged(boolean)
Called when the current Window of the activity gains or loses focus.
This is the best indicator of whether this activity is visible to the
user.
In addition you need to do some checks:
1. Window has focus (hasFocus==true) - it's visible to the user
2. Create boolean variable indicating that animation was already started, so it will be launched only once
private boolean cloudAnimStarted;
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !cloudAnimStarted) {
cloudAnimStarted = true;
cloudAnim.start();
}
}
So creating a cloudAnim object is fine in onCreate, but launching it should be done in onWindowFocusChanged method instead.
I have a system which works like this:
- I start a spinner animation in my view when a network request is fired.
- When the request finishes (within a AsyncTask) the Animation is stopped.
This works fine except when the display turns off because of lack of user action. This code won't work in this situation:
imgVwSpinner.post(new Runnable() {
#Override
public void run() {
imgVwSpinner.clearAnimation();
imgVwSpinner.setImageResource(R.drawable.refresh);
}
});
Is there a workaround to ensure the animation is cleared?
you can flag the state in your handler , and check that state back in onResume
I have a button and while this button is playing an animation, I'm not able to click on button. I've set click listener and touch listener but in debug mode it's not entering in OnClick and in onTouch methods. Do you know why? Thanks
edit: I've tried something like:
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object... objects) {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
toast.show();
}
});
return null;
}
;
};
task.execute(button1);
but it's not working
Edit: here is the full source code
Before Android 3.0, using any of the animation classes only changes where the view is drawn - it won't adjust its touchable-bounds during (or after) the animation:
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
You could:
Make a ViewGroup which moves its children every onDraw
Override your View's onDraw to move itself - Could use margin or padding, or position (if view is in a FrameLayout or something similar).
Only use 3.0+ devices
Override the parent's onTouch (or onInterceptTouchEvent), calculate where the View is being drawn (you can get how far into and the offset from real position from the Animation *) and handle accordingly... * Looking at your code (since you generate a random direction each time it finishes), this might not be possible without tracking which directions you've previously take..
you are facing same issue that i was recently ... when you apply animation on button or any other view and use setFillAfter(True) it means that the image of view is moved not the actual view thats why its not listening to your click listener because its just image of view not your actual view you have to do something like that i explained in answer to my own question according to your situation... means you have to also move the actual view on the end place of animation and use setFillAfter(false) so that when you click after anmation then it should be an actual view not just image used for animation purpose by android
check this link....
EditText stucks after animation and alive back on scrolling......?
In your code use setFillafter(false) and actually place your button at end position of animation by somehow like setting margin or according to your layout use appropriate properties for placement. By Applying these changes your click listener will work perfectly.
==> if you are trying that your button's click listener work while its moving (being animate) then as far as i know its not possible because android uses just image of your view to perform animation not the actual.
This might be a threading problem. You should read Event dispatch thread and how to do thing in android async by reading painless threading and take a look at AsyncTask JavaDoc.
In short: the main thread should not be blocked, since it is used for responing to UI events, such as button presses. Therefore, whenever you do something that take more than some milliseconds, you should do it asynchroniously in another thread.
I think there are two things
1)You can handle one event at one time for one object.Like animation is playing on you button that means you can not click on that untill one work get completed(As i understand you animation is on button not on other part of screen)
2)Second if you have playing on rest part of screen and you want to stop it on click of button.Then i think its a problem of focus.once set onfoucslistener to button and check that when you are clicking it Is it getting focus?
I would put the animation into the async task and then the button click should be handled normally on the main thread I think. Because the way you do it at the moment is: The animation starts and blocks the main thread. This means that the click event can't be excecuted in the async task
You must try to use AsyncTask . Programming Android without it would be and horrible usability serious problem.
See this link for how use an asynctask.
Here an example:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initialisation of the Activity
MyLongTask task = new MyLongTask();
task.execute("http://blog.fr4gus.com/api/test.json");
}
#Override
protected void onPause() {
// If you wanna make something in the pause of the Asynctask
}
class MyLongTask extends AsyncTask<String, Void, Void>{
#Override
protected void onPreExecute() {
// Before executing what you want to execute
}
#Override
protected Void doInBackground(String... params) {
// what you want to execute come here :D
return null; // Or whatever you want pass to onPostExecute
}
#Override
protected void onPostExecute(Void result) {
// Here we can update for example the UI with something (who knows :? )
}
}
}
MODIFIED
You must always extend the Asynctask Activity , since you are trying to pass params through it , you must define them in the head of your extended class :
class MyLongTask extends AsyncTask<String, Void, Void>{ ...
Then first is the doInBAckground param , next is the onPreExecute param , and the last is the onPostExecute param . That way you can send the params like a Button.
You can learn more about here or in my first link.
I'm trying to make a logo animation in a TitleBar.
Everything would work just fine, if an animation didn't reuqire a trigger.
I have a method
public void animateLogo() {
if (imageView != null) {
imageView.setBackgroundResource(R.drawable.logo_animation);
logoAnimation = (AnimationDrawable) imageView.getBackground();
logoAnimation.start();
}
}
When triggered by a button works just fine. But I want it to start independently of any button presses. It should work since starting the App till the end without any interactions.
When calling the method in OnCreate(..) it doesn't work. onStart() the same.
Any ideas how to achieve the result ?
Try starting the animation from onWindowFocusChanged() instead of from onCreate(). I guess animations won't start until after the creation process is totally finished.