Android图片不会淡入淡出

时间:2017-03-25 07:33:23

标签: android imageview relativelayout fade

package com.example.badwalbrothers.layouts;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    public void fade(View view){
        ImageView iv= (ImageView) findViewById(R.id.imageView);
        ImageView iv2= (ImageView) findViewById(R.id.imageView2);
        iv.animate().alpha(0f).setDuration(1500);
        iv2.animate().alpha(1f).setDuration(1500);
    }
   public void fade2(View view){
        ImageView iv= (ImageView) findViewById(R.id.imageView);
        ImageView iv2= (ImageView) findViewById(R.id.imageView2);
        iv2.animate().alpha(0f).setDuration(1500);
        iv.animate().alpha(1f).setDuration(1500);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.rellayout);
    }
}

我已将onClick功能添加到imageViews,即功能淡入淡出和fade2仍然图像没有交换

1 个答案:

答案 0 :(得分:0)

// code for Fade In and Fade Out animation on imageview    

private void fadeInImage(final ImageView img)
      {
        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new AccelerateInterpolator());
        fadeIn.setDuration(1000);

        fadeIn.setAnimationListener(new AnimationListener()
        {
                public void onAnimationEnd(Animation animation) 
                {
                      // code which you need to after animation end
                }
                public void onAnimationRepeat(Animation animation) {}
                public void onAnimationStart(Animation animation) {}
        });

        img.startAnimation(fadeIn);
    }

private void fadeOutImage(final ImageView img)
      {
        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setDuration(1000);

        fadeOut.setAnimationListener(new AnimationListener()
        {
                public void onAnimationEnd(Animation animation) 
                {
                      // code which you need to after animation end
                }
                public void onAnimationRepeat(Animation animation) {}
                public void onAnimationStart(Animation animation) {}
        });

        img.startAnimation(fadeOut);
    }

希望这些可以帮助你