Android点击时交换图片

时间:2016-09-21 16:48:20

标签: java android xml

我有两个ImageView,我想互相交换图像。我知道我可以使用setImageDrawable或setImageBitmap方法来更改图像,但这并不是我想要的。我想要的是图像应该互相改变它们的位置。

我注意到onFade1()没有被调用但是onFade2()。 单击图像时没有任何反应。请帮忙!我想XML文件中的alpha值存在一些问题

Java代码:

public class MainActivity extends AppCompatActivity {

ImageView image1;
ImageView image2;

public void onFade1(View view){
    image1 = (ImageView) findViewById(R.id.iv_img1);
    image2 = (ImageView) findViewById(R.id.iv_img2);

    image1.animate().alpha(0).setDuration(2000);
    image2.animate().alpha(1).setDuration(2000);
    System.out.println("Hi");
}

public void onFade2(View view){
    image1 = (ImageView) findViewById(R.id.iv_img1);
    image2 = (ImageView) findViewById(R.id.iv_img2);

    image2.animate().alpha(0).setDuration(2000);
    image1.animate().alpha(1).setDuration(2000);
    System.out.println("Hello");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

XML代码:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image1"
    android:id="@+id/iv_img1"
    android:onClick="onFade1" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image2"
    android:id="@+id/iv_img2"
    android:alpha="0"
    android:onClick="onFade2" />

2 个答案:

答案 0 :(得分:0)

onFade2仅被调用,因为它指的是xml中最后一个ImageView,它是最顶层的,并且即使alpha为零,也会始终获得Touch。你要做的是添加一个动画监听器,并使图像点击可见性为GONE

 public void onFade1(View view) {
    image1 = (ImageView) findViewById(R.id.iv_img1);
    image2 = (ImageView) findViewById(R.id.iv_img2);

    image1.animate().alpha(0).setDuration(2000).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            image1.setVisibility(View.GONE);
            System.out.println("image1 Gone");
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    image2.animate().alpha(1).setDuration(2000).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            image2.setVisibility(View.VISIBLE);
            System.out.println("image2 VISIBLE");
        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    System.out.println("Hi");
}

public void onFade2(View view) {
    image1 = (ImageView) findViewById(R.id.iv_img1);
    image2 = (ImageView) findViewById(R.id.iv_img2);

    image2.animate().alpha(0).setDuration(2000).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            image2.setVisibility(View.GONE);
            System.out.println("image2 GONE");
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    image1.animate().alpha(1).setDuration(2000).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            image1.setVisibility(View.VISIBLE);
            System.out.println("image1 VISIBLE");
        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    System.out.println("Hello");
}

答案 1 :(得分:0)

XML更改为此:

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image1"
android:id="@+id/iv_img1"
android:onClick="onFade" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image2"
    android:id="@+id/iv_img2"
    android:alpha="0"
    android:onClick="onFade" />

Activity代码:

public class MainActivity extends AppCompatActivity {

ImageView image1;
ImageView image2;

public void onFade(View view){
    switch(view.getId()) {
        case R.id.iv_img1:
            MainActivity.this.findViewById(R.id.iv_img2)
                .setVisibility(View.GONE);
            view.setVisibility(View.VISIBLE);
            view.animate().alpha(1).setDuration(2000);
            ((ImageView) MainActivity.this.findViewById(R.id.iv_img2))
                .animate().alpha(0).setDuration(2000);
        case R.id.iv_img2:
            MainActivity.this.findViewById(R.id.iv_img1)
                .setVisibility(View.GONE);
            view.setVisibility(View.VISIBLE);
            view.animate().alpha(1).setDuration(2000);
            ((ImageView) MainActivity.this.findViewById(R.id.iv_img1))
                .animate().alpha(0).setDuration(2000);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}