Start an animation automatically without the need of click on screen - android

i know this question has been asked many times before but i could never quite understand as the other codes were quite a bit more complex or different than mine. so like the question says ... how to start animation automatically instead of on click .. ill leave the code below ... thanks in advance.. !!
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});

Something like this should work... This will be triggered everytime the activity is "displayed".
#Override
public void onResume() {
super.onResume();
// layout should have bee initialized in onCreate
layout.post(new Runnable() {
#Override
public void run() {
rImage.startAnimation(a);
}
});
}

you can put your code in onResume, or onCreate depends of your needs, also you can set a timer to active this code, there are many ways...
How to start Animation immediately after onCreate?
http://damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

How to make my ImageView responsive to onClickevents while .startAnimation() is running?

I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}
You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});

An Issue animating a new TextView added to the activity on Nexus4 vs Other devices

Im very new to java and android programming.
I am trying to create a button that when pressed, a text will appear at the location of the click and immediately fade out.
At the moment, the problem is that when clicked - the text does appear, but the animation doesn't play, and the text disappears after a few seconds (just like I want).
I have edited this question a few times down bellow, following some progress on solving this problem with great help from the guys replying. Thanks!
The code I tried is this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClickMe = (Button) findViewById(R.id.button1);
mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
mClickMe.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int xClickPos = (int) event.getRawX();
int yClickPos = (int) event.getRawY();
final TextView scorePop = new TextView(getApplicationContext());
scorePop.setText("+10");
scorePop.setX(xClickPos);
scorePop.setY(yClickPos);
scorePop.setVisibility(TextView.INVISIBLE);
// Create the score pop animation
Animation scoreAnimation = new AlphaAnimation(1, 0);
scoreAnimation.setDuration(1000);
scoreAnimation.setAnimationListener (new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
scorePop.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mRelativeLayout.removeView(scorePop);
}
});
mRelativeLayout.addView(scorePop);
scorePop.setAnimation(scoreAnimation);
scoreAnimation.start();
}
return false;
}
});
}
EDIT: I have also noticed that if the button is clicked quickly on different spots, the text appears at those spots and disappears with a "choppy" fade. Yet still, if clicked once - the text appears and disappears but no animation.
Thanks for the help!
EDIT 2: With the help I got here it seems the problem has something to do with the device. I am using Nexus 4, and running an emulator to test my apps as Nexus 4 as well. Oddly, running emulator for another device such as Galaxy Nexus seemed to solve the issue.
EDIT:
I was able to recreate your original bug and it had something to do with setX(),setY(). After executing either of those methods the TextView gets "numb" and won't animate. The workarround I figured was to set the layout's margins not the TextView's coordinates:
// Set the margins instead of the actual position
ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)scorePop.getLayoutParams();
params.leftMargin=xClickPos;
params.topMargin=yClickPos;
On my device Galaxy Nexus it is showing animation fine on first time also. May be your device takes time here "mRelativeLayout.addView(scorePop);" Add a little delay after that and then start animation It should run fine.
try it...
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
// set animation listener
animFadein.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animFadein);
}
});

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

How to run animation sequences more than single time

I am using sequence for animation in my project. I have set onClickListener to that image where I put animation sequences. now when first time I run the code, and click on image, it start run animation correctly but then it never run again on click. I have to again run the code. So how I can run animation sequences more than one time? I have put my codebelow to start animation sequence.
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animation_door = (AnimationDrawable) image.getBackground();
animation_door.start();
} });
Well, before clicking your button again you have to stop the animation also, so you can try it like this,
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
animation_door = (AnimationDrawable) image.getBackground();
animation_door.stop();
animation_door.start();
}
});
Is your animation set as a oneShot? It's most likely in the end state and you just need to hint it back to the start state.
AnimationDrawable door = (AnimationDrawable) image.getBackground();
door.setOneShot(false);
door.start();
Keep in mind this will most likely lead to a looping animation, which you probably don't want. You could consider using an Animation Set instead since you'll have a bit more control over the animation itself.

Categories

Resources