Android淡入并淡出2张图片

时间:2017-02-06 21:15:50

标签: android android-layout android-animation

在Android中我试图在点击图像时淡化和淡出2张图像。例如:如果我点击图像1它会淡出(消失)而图像2淡入(我可以看到),当我点击图像2时它淡出并显示我image1(淡入)。但这里的问题是fadding并没有像预期的那样发生。

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

}
}

所以当我点击图片时,只有fade1()方法运行,我应该在dirrenent screenlocations上多次点击以使fade2()方法运行。在我的控制台中我得到下面提到的日志,

 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 

请帮助解决此问题。

1 个答案:

答案 0 :(得分:2)

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

希望它可能适合你。