How to animate an image in TitleBar? - android

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.

Related

Animate views before activity finished

I use view animations in a predrawlistener to animate the activity start.
Exist a similar listener to animate my views before my activity is finished?
I know the overridependingtransition method for animations in xml but i want to use ViewPropertyAnimations in a programmatically way for specific views.
I suggest you to override finish() method, then add your animation there:
#Override
public void finish (){
// do your animation
super.finish();
}
You can call YourActivity.finsih(); when you want to finish your activity from code.

Animating Button alpha to 0 and the 1 after a delay does not work

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();

ImageView in SplashScreen is not showing

I am very new to android and trying to add a splash screen I am half way succeeded. But a weird think happening that surely an easy one. here i have tried :-
I wanted to stop the SplashScreen for some time. This splash screen layout contain a ImageView that show the appLogo.
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.SplashScreen);
ImageView splashScreenImage = FindViewById<ImageView>(Resource.Id.appLogo);
splashScreenImage.SetImageResource(Resource.Drawable.splLogo);
Thread.Sleep(30000);
StartActivity(typeof(MainActivity));
}
}
Actualy my splash screen waiting stopping for some time but ImageView is not showing it come out at the last moment when new activity is going to start.
Why is this happening ? any help is appreciated :)
problem:
Thread.Sleep(30000);
It is not showing because you are blocking your UI thread to process the SetContentView to display in the screen thus it is not showing.
What it is really doing is that it will wait for 30 second without the display/black screen and change activity.
Solution:
Use a timer or Handler instead of sleeping the thread.

Android AnimationDrawable start

I'm using AnimationDrawable to show missing network connection.
Show/hide logic is linked to network status change receiver. It works fine.
But when start activity knowing status and try to start animation - animated drawable shows and freezes on first frame. I've read in documentation - 'do not start animation in OnCreate'.
So I wrote code in onResume, but animation still not playing - only shows first frame.
Starting from button or event works fine.
Tried to start with separate thread and wait some time - but this doent sounds good.
Any idea?
This code works when called from net status change handler
private void _NetStatus(boolean start)
{
if (start)
{
m_NetStatus.setVisibility(View.VISIBLE);
m_NetStatusFrameAnimation.start();
}
else
{
m_NetStatusFrameAnimation.stop();
m_NetStatus.setVisibility(View.INVISIBLE);
}
}
Hmm. After trying some samples advising me to use new Runnable at end of onCreate - I tried to start animation when activity shows on screen:
#Override
public void onWindowFocusChanged(boolean hasFocus)
Works fine for now.

android button not clickable while playing animations

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.

Categories

Resources