android - 在android中为imageview平滑淡出

标签 android fadeout

我设法让 imageView 在 10 秒后消失(我的 mainActivity 上的教程图片)。但是我想做一个平滑的淡出,因为这样看起来不太好,任何人都可以向我推荐任何好的教程

     img=(ImageView)findViewById(R.id.ImageTutorial);
    if(getIntent()!=null)
    {

        Bundle extras = getIntent().getExtras();
        String TutorialDemo=extras !=null? extras.getString("TutorialDemo"):"false";

        if(TutorialDemo.equals("true"))
        {
            Runnable mRunnable;
            Handler mHandler=new Handler();

            mRunnable=new Runnable() {

                        @Override
                        public void run() {

                            img.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View    
                        }
                    };
                    mHandler.postDelayed(mRunnable,10*900);
        }
        else
        {
            img.setVisibility(View.GONE);
        }
         }

这是 ImageView xml

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:id="@+id/fullview"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical" >

      .
      .
      .
      .

    <ImageView
        android:contentDescription="tutorial"
        android:id="@+id/ImageTutorial"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/tutorial"
        android:layout_marginTop="40dp"
        android:gravity="center"
        />

</LinearLayout>

最佳答案

将代码中的 img.setVisibility(View.GONE) 替换为对 fadeOutAndHideImage(img) 的调用,其定义如下:

  private void fadeOutAndHideImage(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) 
            {
                  img.setVisibility(View.GONE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationStart(Animation animation) {}
    });

    img.startAnimation(fadeOut);
}

它会先应用淡出动画,然后隐藏 ImageView 。

关于android - 在android中为imageview平滑淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782260/

相关文章:

Android gradle 禁用删除没有默认值的资源

jquery - 如何在页面加载一段时间后淡出 div 的边框和背景颜色?

jquery - 页面滚动时淡入淡出文本

java - 为什么 getActionView() 返回 null

Jquery ReplaceWith - 淡出/淡入

jQuery 忽略 .fadeOut 上的元素内联 block

javascript - 淡入淡出加载CSS3动画的最佳方法

java - 调用 org.json.JSONObject.getJSONArray 时出现奇怪的行为

java - 自定义 setDropDownViewResource 微调项的示例

android - Android 设备上的假来电