如何在 Android 活动屏幕上淡入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2597329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:29:52  来源:igfitidea点击:

How to do a fadein of an image on an Android Activity screen?

androidimage-processingfadein

提问by Hiroshi Iwatani

I'd like to display a photo on an Android Activity screen with doing gradual and continual fade-in from pale monotone sepia to the final full color. I know how to do it on a Java Image/BufferedImage for the Graphic object but unfortunately I know nothing for the Android programming environment. Could anyone help?

我想在 Android Activity 屏幕上显示一张照片,从苍白的单调棕褐色逐渐和连续淡入到最终的全色。我知道如何在 Java Image/BufferedImage 上为 Graphic 对象执行此操作,但不幸的是我对 Android 编程环境一无所知。有人可以帮忙吗?

回答by Jorgesys

Hi Hiroshi you can do this for the fade in:

Hi Hiroshi 你可以为淡入做这个:

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

and inside your res\anim\ folder the animation file fadein.xml

在 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>

but for the gradual fade in from sepia to the full color, you must use TransitionDrawable

但是对于从棕褐色到全色的逐渐淡入,您必须使用TransitionDrawable

回答by Ricky

I wanted an image to fade (and then disappear) once clicked from full opacity to 0. Here is how I did it:

一旦从完全不透明度点击到 0,我想要一个图像淡入淡出(然后消失)。这是我是如何做到的:

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

回答by Mike Droid

One method for this would be to use the animation set. See here;

一种方法是使用动画集。看这里;

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

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

Some example code I have done (infinite loop fade out in this example) ;

我做过的一些示例代码(在这个例子中无限循环淡出);

In the animation .xml file;

在动画 .xml 文件中;

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

In the java file;

在java文件中;

 ImageView introanim = (ImageView) findViewById(R.id.introanim);
    Animation StoryAnimation = AnimationUtils.loadAnimation(this, R.anim.intro_anim);
    introanim.startAnimation(StoryAnimation);

You could fade from your sepia background/picture to whatever you want...

你可以从你的棕褐色背景/图片淡入任何你想要的......

回答by Emanuel Adrian Lucaci

Another much simpler solution is to just put a stub onClick method to your ImageView you want to fade, then inside the method you add this:

另一个更简单的解决方案是将存根 onClick 方法放在要淡化的 ImageView 中,然后在方法中添加以下内容:

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 */