The first time I open up my application in the emulator and click the image to run the animation nothing happens. When I go back to the home menu and then back to my app, the animation runs without me clicking anything. Subsequent clicks after this point run the animation as it was intended to run. This is using 2.3.3 in the emulator. I have not seen this issue using version 4.1.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) findViewById(R.id.ImageView01);
image2 = (ImageView) findViewById(R.id.ImageView02);
image2.setVisibility(View.INVISIBLE);
image1.setClickable(true);
image1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Log.d("== My activity ===","OnClick is called"); // This gets logged on every click
Animation fade1 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
image2.startAnimation(fade1);
The previous activity exit animation is overriding your enter animation. Usually you have to stop/override those with:
activity.overridePendingTransition(R.anim.fade1, R.anim.fade_in);
Related
I've a imageview and animating the image infinitely with YoYo. I need to start a new activity AnimalIntroducer when this imageview is clicked. I can start new activity AnimalIntroducer clicking this animated button. But when clicking the back button in AnimalIntroducer activity when I come back to my HomeActivity then this animation on imageview stops, and button click becomes unresponsive and shows application is not responding. Do I have to run the animation in background thread? If so how can I do this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
hangingMonkeyImageView = findViewById(R.id.monkey_hanging_image_view);
YoYo.with(Techniques.Bounce)
.duration(1000)
.repeat(-1)
.playOn(hangingMonkeyImageView);
hangingMonkeyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this,AnimalIntroducer.class);
startActivity(intent);
}
});
}
I would suggest you to stop the animation on view when the user navigates to the second activity, and restart the animation in onResume() method of HomeActivity.
If you want to learn more about it please see Android Activity Lifecycle
I want to create an animate img, so when I click a button I can see the animation.
in my project, I've just create buttons and images. In the drawable folder, I put the three png images and a frame_animation.xml, I set the images and the duration. In the java file, I put this code:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
}
});
but when I run the app, the animation doesn't work (I can see only one image, not all the three image)
You have to also START the animation, setting the resource is not enough.
In your case simply add myFrameAnimation.start();
Full example:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
myFrameAnimation.start();
}
});
Seems like you just left it out by accident, since you already got the AnimationDrawable from your resource.
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);
}
});
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?
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.