Sandeep Raj Urs
Sandeep Raj Urs

Reputation: 53

Android fade In and fade out 2 images

In Android I trying to fadein and fadeout 2 images upon clicking on image.Ex: If i click image1 it fades out (disappears) and image2 fades in (i can see) and when i click image 2 it fades out and shows me image1 (fades in). But problem here is fadding is not happening as expected.

package com.example.sandeep.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

public void fade(View view){
ImageView img1=(ImageView) findViewById(R.id.img1);
ImageView img2=(ImageView) findViewById(R.id.img2);
img1.animate().alpha(0f).setDuration(2000);
img2.animate().alpha(1f).setDuration(2000);
Log.i("Info:","fade method running");

}

public void fade1(View view) {
    ImageView img1 = (ImageView) findViewById(R.id.img1);
    ImageView img2 = (ImageView) findViewById(R.id.img2);
    img2.animate().alpha(0f).setDuration(2000);
    img1.animate().alpha(1f).setDuration(2000);
    Log.i("Info:","fade1 method running");
}
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
}

So when i click on the image only fade1() method runs and I should click multiple times on dirrenent screenlocations to make fade2() method run.In my console i am getting the below mentioned logs,

 02-07 02:29:44.088 10720-10720/com.example.sandeep.myapplication I/Info::           

 fade1 method running
 02-07 02:29:49.728 10720-10720/com.example.sandeep.myapplication I/Info::   

 fade1 method running

 [ 02-07 02:29:50.005  8757: 8757 E/         ]
 [adb] handle_packet() t->online(1) p->msg.arg0(23439) p->msg.arg1(0) OPEN


 [ 02-07 02:29:50.005  8757: 8757 E/         ]
 [adb] handle the adb command, and the command = adb shell:cat   

  /proc/net/xt_qtaguid/stats | grep 10181

 [ 02-07 02:29:50.032  8757: 8757 E/         ]
 [adb]   cuurent command is A_CLSE 

Please help to solve this issue.

Upvotes: 1

Views: 1019

Answers (1)

Ashok Kumar
Ashok Kumar

Reputation: 131

ImageView img1 = (ImageView) findViewById(R.id.img1);
ImageView img2 = (ImageView) findViewById(R.id.img2);
img2.setVisibility(View.GONE);

img1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
fadeIn(img1, img2);
   }
});

img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    fadeOut(img1, img2);
       }
    });

public void fadeIn(ImageView img1, ImageView img2) {
            Animation fadeIn = new AlphaAnimation(0, 1);
            fadeIn.setInterpolator(new AccelerateInterpolator());
            fadeIn.setDuration(1000);
            fadeIn.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    img2.setVisibility(View.VISIBLE);
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });

            img2.startAnimation(fadeIn);
            img1.setVisibility(View.GONE);
        }

public void fadeOut(ImageView img1, ImageView img2) {
 Animation fadeOut = new AlphaAnimation(1, 0);
                fadeOut.setInterpolator(new AccelerateInterpolator());
                fadeOut.setDuration(1000);
                fadeOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        img2.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                img2.startAnimation(fadeOut);
                img1.setVisibility(View.VISIBLE);
            }
        });
 }

Hope it may work for you.

Upvotes: 2

Related Questions