2013-05-28 59 views
1

我试图做一个简单的幻灯片,淡出和淡入一些图像没有按钮点击。我发现了几个例子来帮助我,但我无法让第一张图像淡入第二张图像。到目前为止,它只是淡出,就是这样。下面我有我的代码。使用ViewSwitcher的Android图像幻灯片

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="0.1" 
     android:toAlpha="1.0" 
     android:duration="5000" 
     android:repeatCount="infinite" 
     /> 
</set> 

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="1.0" 
     android:toAlpha="0.1" 
     android:duration="5000" 
     android:repeatCount="infinite" 
     /> 
</set> 

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/switcher" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:inAnimation="@anim/fade_in" 
    android:outAnimation="@anim/fade_out" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/image1" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/image2" /> 
</ViewSwitcher> 

MainClass.Activity

public class MainClass extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.next); 

     slideshow(); 
    } 

    public void slideshow() { 
     ViewSwitcher switching = (ViewSwitcher)findViewById(R.id.switcher); 
     if (switching.getDisplayedChild() == 0) { 
      switching.showNext(); 
     } else { 
      switching.showPrevious(); 
     } 
    } 

} 

回答

3

在这种情况下,您需要使用timer。你这样使用它:

public class MainClass extends Activity { 

    private Timer timer; 
    private ViewSwitcher switching; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.next); 

     switching = (ViewSwitcher)findViewById(R.id.switcher); 

     timer = new Timer(); 
     timer.scheduleAtFixedRate(new NextImageTask(), 0, 5000); 
     //5000 is in milliseconds, meaning 5 seconds 

    } 

    public void slideshow() { 
     if (switching.getDisplayedChild() == 0) { 
      switching.showNext(); 
     } else { 
      switching.showPrevious(); 
     } 
    } 

    private class NextImageTask extends TimerTask { 

     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        slideshow(); 
       } 
      }); 
     } 
    } 
} 

这段代码是做什么的?那么,让我们一步一步来看看。首先声明的意见

private Timer timer; 
private ViewSwitcher switching; 

然后初始化它们在onCreate方法

switching = (ViewSwitcher)findViewById(R.id.switcher); 

timer = new Timer(); 

然后,我们开始计时!要启动计时器,并告诉它做什么,我们做它像这样

timer.scheduleAtFixedRate(new NextImageTask(), 0, 5000); 

这行代码基本上意味着嘿定时器,安排任务,在5秒的固定利率。立即开始!

第一个参数是要安排的任务,一个对象,NextImageTask()。该对象包含一个Runnable,其中包含要运行每个时间间隔的代码。间隔是固定的5秒。但在代码中,它被写为5000。这是因为该方法接受第三个参数为毫秒。要得到5秒,我们必须乘以5乘1000,结果是,你猜对了,5000。通过传递第二个参数0,定时器被要求立即开始。这是延迟。如果您通过2000,计时器将在2秒延迟后开始。就像第三个参数一样,该方法接受第二个参数为毫秒

接下来是与OP相同的方法slideshow()。这是OP想要做的。

最后是私人对象NextImageTask()

private class NextImageTask extends TimerTask { 

    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       slideshow(); 
      } 
     }); 
    } 
} 

这是定时器以固定速率/每5秒完成的任务。它应该延长TimerTask让计时器正常工作。 TimerTask是一个抽象类,所以NextImageTask()应实现一种方法,即run()方法。该方法应包含计时器以固定间隔运行的代码。这在不同的线程上运行,所以如果你需要像UI的情况那样在UI中做一些事情,我们需要在UI线程上运行。这就是我们在这里称为runOnUiThread的原因。最后,里面是另一个可运行的调用OP的方法slideshow()