Fade in animation blinks the view - android

I want to make three text views fade in one after another. I am trying to do this with start animation after finishing first and so on. But it not works for me. Please help
text_1.setVisibility(View.INVISIBLE);
text_2.setVisibility(View.INVISIBLE);
text_3.setVisibility(View.INVISIBLE);
private void fadingAnimation() {
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(2000);
AnimationSet animation1 = new AnimationSet(false); //change to false
final AnimationSet animation2 = new AnimationSet(false); //change to false
final AnimationSet animation3 = new AnimationSet(false); //change to false
animation1.addAnimation(fadeIn);
animation1.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
text_1.setVisibility(View.VISIBLE);
text_2.startAnimation(animation2);
}
});
text_1.startAnimation(animation1);
}

Change your menthod to
private void fadingAnimation() {
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(2000);
AnimationSet animation1 = new AnimationSet(false); //change to false
final AnimationSet animation2 = new AnimationSet(false); //change to false
final AnimationSet animation3 = new AnimationSet(false); //change to false
animation1.addAnimation(fadeIn);
animation1.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
text_1.setVisibility(View.VISIBLE);
text_2.startAnimation(animation2);
}
});
animation2.addAnimation(fadeIn);
animation2.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
text_2.setVisibility(View.VISIBLE);
text_3.startAnimation(animation3);
}
});
animation3.addAnimation(fadeIn);
animation3.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
text_3.setVisibility(View.VISIBLE);
}
});
text_1.startAnimation(animation1);
}

Related

How to time an animation in android

I am animating a loader on start up. How can I work around it so that instead of using ontouch listener to stop the timer, I use a time of say like 3 seconds? Here is my code:
private void showdiag() {
loader = findViewById(R.id.loader);
logo= loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this,R.anim.rotate));
loader.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loader.setVisibility(View.GONE);
}
});
}
You can use View.postDelayed() to run some action after a set delay:
private void showdiag() {
loader = findViewById(R.id.loader);
logo = loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}
logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.setVisibility(View.GONE);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});
Please try following code.
logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});

Animation in custom pager in not starting up

I have a custom pager that uses two animations
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
For some reason, the animations are not starting up. For example, I'm logging
Log.v("TAG", "L1 start animation"); //prints "L1 start animation"
But I'm not logging anything in onAnimationEnd
Log.v("TAG", "L1 animation end"); //nothing is printing out
Here is setAnimation() method of CustomPager class
public void setAnimation(){
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
l1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L1 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.v("TAG", "L1 animation start");
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("TAG", "L1 animation end");
l1.setVisibility(View.GONE);
l2.bringToFront();
clicked = false;
}
});
Log.v("TAG", "L1 start animation");
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
}
});
//etc
}
Here is CustomPager class
public class CustomPager extends PagerAdapter {
private Context mContext;
private View l1;
private View l2;
private LayoutInflater inflater;
private ViewGroup layout;
private Bitmap pic;
private Bitmap pic_blurred;
private boolean clicked = false;
public CustomPager(Context context) {
this.mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
inflater = LayoutInflater.from(mContext);
layout = (ViewGroup) inflater.inflate(R.layout.pager_play_panel, collection, false);
collection.addView(layout);
l1 = layout.findViewById(R.id.l1);
l2 = layout.findViewById(R.id.l2);
pic = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pic);
picture_blurred = blur(pic, 1, 100);
BitmapDrawable bkg1 = new BitmapDrawable(pic);
l1.setBackgroundDrawable(bkg1);
BitmapDrawable bkg2 = new BitmapDrawable(picture_blurred);
l2.setBackgroundDrawable(bkg2);
setAnimation();
return layout;
}
public void setAnimation(){
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
l1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L1 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.v("TAG", "L1 animation start");
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("TAG", "L1 animation end");
l1.setVisibility(View.GONE);
l2.bringToFront();
clicked = false;
}
});
Log.v("TAG", "L1 start animation");
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
}
});
l2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L2 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
l2.setVisibility(View.GONE);
l1.bringToFront();
Log.v("TAG", "L2 visibility set to gone");
clicked = false;
}
});
l2.startAnimation(fadeOut);
l1.setVisibility(View.VISIBLE);
l1.startAnimation(fadeIn);
}
}
});
}
//Etc.
}
Looks like the problem was I had to change the scope of my view variables
public void setAnimation(View l1, View l2){
//etc
}
Rather than using them globally
public void setAnimation(){
//etc
}

Play sound while animating imageview and should stop when imageview animation is stopped

Is there any solution that I can play sound while my imageView is animating.
I've tried this:
public void bounceInterpolar(View thumbnailView){
ImageView animatedImage = (ImageView) findViewById(R.id.img_animation);
Animation animation
= AnimationUtils.loadAnimation(this, R.anim.animation);
animatedImage.startAnimation(animation);
mp = MediaPlayer.create(this, R.raw.soho);
animatedImage = (ImageView)this.findViewById(R.id.img_animation);
animatedImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
}
});
Issue: By clicking on imageView, once the animation is over and then if I click on the imageView 2nd time, then the sound is played which is not my requirement. Need to play the sound parallel along with animation.
Please set animation listener to your animation object
animation.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
if (m_player != null && m_player.isPlaying())
{
m_player.stop();
m_player.release();
m_player = null;
}
}
});
You need to add listener to the animation.
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
mp.start();
}
#Override
public void onAnimationEnd(Animation animation) {
mp.stop();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Animation Listener not working on Button Click

I am trying to Hide/Show a TableLayout on button click but the animation listener is not working here is the code i am trying
Slide_down = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
Slide_up = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_up);
searchArea = (TableLayout) findViewById(R.id.TableLayout1);
SearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (check_tableView == 0) {
Slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
searchArea.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
searchArea.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
check_tableView = 1;
} else {
Slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
searchArea.setVisibility(View.GONE);
}
#Override
public void onAnimationEnd(Animation animation) {
searchArea.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
check_tableView = 0;
}
}
});
//just start animation
searchArea = (TableLayout) findViewById(R.id.TableLayout1);
SearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (check_tableView == 0) {
searchArea.startAnimation(Slide_up);
}
});
You just set animation listener outside of onClickListener. No need of setting each time View is clicked. Also make sure that you are calling searchArea.startAnimation(Slide_up) and searchArea.startAnimation(Slide_down) method. Call start animation method inside onClickListener.

Delay while restart of animation

I'm using value animator to animate a image view. I want to add a delay in Animation listener, i.e in "onAnimationRepeat()". How to accomplish this?
Maybe try something like this:
public class Test {
private int repeatNumber == 0;
public void startAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {Log.i("start", "start");}
#Override
public void onAnimationEnd(Animation animation) {Log.i("end", "end");}
#Override
public void onAnimationRepeat(Animation animation) {
Test.this.repeatNumber++;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Test.this.startAnimation();
}, 30000 );
}
textView.startAnimation(animation);
}
}

Categories

Resources