如何在Android Activity屏幕上实现图像的淡入效果?

43

我想在Android Activity屏幕上显示一张照片,并希望它从淡黄色的单色褐变成最终的全彩色,具有渐进和持续的渐变效果。我知道如何在Java Image/BufferedImage上为Graphic对象实现此效果,但不幸的是,我对Android编程环境一无所知。有人能帮忙吗?

4个回答

82

你好Hiroshi,你可以使用以下代码实现渐变效果:

  ImageView myImageView= (ImageView)findViewById(R.id.myImageView);
  Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
  myImageView.startAnimation(myFadeInAnimation); //Set animation to your ImageView

在您的res\anim\文件夹中有一个名为fadein.xml的动画文件。

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha 
            android:fromAlpha="0.0" 
            android:toAlpha="1.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:duration="3000"/>
</set>

但是如果想要实现从棕黑色到全彩色的渐变效果,你需要使用TransitionDrawable


4
运行得很好。不过你可能不需要 android:repeatCount="infinite",虽然... - Bojan Radivojevic

52

我想让一张图片在被点击后从完全不透明到透明,并最终消失。这是我实现的方式:

Animation a = new AlphaAnimation(1.00f, 0.00f);

a.setDuration(1000);
a.setAnimationListener(new AnimationListener() {

    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    public void onAnimationEnd(Animation animation) {
        yourView.setVisibility(View.GONE);

    }
});

yourView.startAnimation(a);

谢谢,这对我在RecyclerView内部进行动画有所帮助。 - AndyRoid

7

其中一种方法是使用动画集。请参见这里;

http://developer.android.com/guide/topics/resources/available-resources.html#animation

以下是我编写的一些示例代码(本示例中为无限循环淡出);

在动画 .xml 文件中;

<alpha android:fromAlpha="1.0" 
       android:toAlpha="0.3"  
       android:duration="7000"
       android:repeatMode="restart"
       android:repeatCount="infinite"/>

在Java文件中;
 ImageView introanim = (ImageView) findViewById(R.id.introanim);
    Animation StoryAnimation = AnimationUtils.loadAnimation(this, R.anim.intro_anim);
    introanim.startAnimation(StoryAnimation);

你可以将你的棕黄色背景/图片渐变到任何你想要的颜色...

谢谢Jorge和Mike。我会尝试你们的方法。 - Hiroshi Iwatani

1
另一个更简单的解决方案是在您想要淡化的 ImageView 上放置一个 stub onClick 方法,然后在该方法内添加以下内容:
view.animate().alpha(0).setDuration(2000);      

/* Alpha attribute translates to opacity. 
A solid View means that the alpha attribute is set to 1 (which is the 
default) and completely invisible is 0 */

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接