【问题标题】:How to do a fadein of an image on an Android Activity screen?如何在 Android Activity 屏幕上淡入图像?
【发布时间】:2011-02-05 12:59:46
【问题描述】:

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

【问题讨论】:

    标签: android image-processing fadein


    【解决方案1】:

    嗨 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

    【讨论】:

    • 工作得很好。不过,您可能不需要android:repeatCount="infinite"...
    【解决方案2】:

    我希望图像在从完全不透明度单击为 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);
    

    【讨论】:

    • 感谢这帮助我在回收站视图中制作动画
    【解决方案3】:

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

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

    你可以从你的棕褐色背景/图片淡化成你想要的任何东西......

    【讨论】:

    • 感谢豪尔赫和迈克。我会试试你的做法。
    【解决方案4】:

    另一个更简单的解决方案是只将一个存根 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 */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多