2013-03-13 35 views
1

在我的應用程序中使用ImageSwitcher,但我不希望圖像淡入淡出。我想要顯示一張圖像,然後將「淡出」顯示爲黑色,對另一張圖像顯示「淡入淡出」。但由於某種原因,它會使兩幅圖像交叉漸變。如果有人能幫助我,我將不勝感激。如何在不重疊或交叉淡入的情況下淡入淡出圖像?

Activity_intro.java

public class IntroActivity extends Activity implements ViewFactory { 

    private static final String TAG = "IntroActivity"; 

    private final int[] images = { R.drawable.wheelpaper1, R.drawable.wheelpaper2}; 
    private int index = 0; 
    private final int interval = 4000; 
    private boolean isRunning = true; 

    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.activity_intro); 

     startAnimatedBackground(); 

    } 

    private void startAnimatedBackground() { 
     Animation aniIn = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in); 
     aniIn.setDuration(1500); 
     Animation aniOut = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out); 
     aniOut.setDuration(1500); 

     final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); 
     imageSwitcher.setInAnimation(aniIn); 
     imageSwitcher.setOutAnimation(aniOut); 
     imageSwitcher.setFactory(this); 
     imageSwitcher.setImageResource(images[index]); 

     final Handler handler = new Handler(); 
     Runnable runnable = new Runnable() { 

      @Override 
      public void run() { 
       if (isRunning) { 
        index++; 
        index = index % images.length; 
        Log.d("Intro Screen", "Change Image " + index); 
        imageSwitcher.setImageResource(images[index]); 
        handler.postDelayed(this, interval); 
       } 
      } 
     }; 
     handler.postDelayed(runnable, interval); 

    } 

    @Override 
    public View makeView() { 
     ImageView imageView = new ImageView(this); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     return imageView; 
    } 

    @Override 
    public void finish() { 
     isRunning = false; 
     super.finish(); 
    } 
} 
+0

有人可以幫我嗎? – Warren0618 2013-03-13 22:53:05

+0

我希望有人會請回復我的問題! – Warren0618 2013-03-14 04:07:13

回答

0

那麼這裏有一個建議。

我總是覺得它有助於尋找到源,看到here例如。

你會看到圖像切換器是真的只是一個ImageView的特定視圖的鰭狀肢。現在沒有任何方法會暴露你想要的東西 - 這不是一個非常複雜的類。

所以,你可以從它繼承紡出你的own-創建自定義視圖和使用,在你的XML。我建議是這樣的:

public class CustomImageSwitcher extends ImageSwitcher { 

private Queue<Runnable> mAfterAnimationQueue = new LinkedList<Runnable>(); 

private final GradientDrawable blackDrawable; // just incase you want a gradient too... 

public CustomImageSwitcher(Context context) { 
    super(context); 
    blackDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] { 
      getResources().getColor(android.R.color.black), 
      getResources().getColor(android.R.color.black) }); 
} 

public CustomImageSwitcher(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    blackDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, 
      new int[] { getResources().getColor(android.R.color.black) }); 
} 

public void postAfterAnim(Runnable runnable) { 
    mAfterAnimationQueue.add(runnable); 
} 

protected void onFadeToBlackEnded() { 
    while (!mAfterAnimationQueue.isEmpty()) { 
     mAfterAnimationQueue.remove().run(); 
    } 
} 

private Animation.AnimationListener biDirectionalCustomListener = new AnimationListener() { 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     onFadeToBlackEnded(); 
     animation.reset(); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     ; // we only really want the end event! 
    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     ; 
    } 
}; 

// THIS ISN'T A COMPLETED EXAMPLE AS THIS WOULD UNSET ANY PREVIOUS ANIMATION 
// LISTENERS 
@Override 
public void setInAnimation(Animation inAnimation) { 
    inAnimation.setAnimationListener(biDirectionalCustomListener); 
    super.setInAnimation(inAnimation); 
} 

// THIS ISN'T A COMPLETED EXAMPLE AS THIS WOULD UNSET ANY PREVIOUS ANIMATION 
// LISTENERS 
@Override 
public void setOutAnimation(Animation outAnimation) { 
    outAnimation.setAnimationListener(biDirectionalCustomListener); 
    super.setOutAnimation(outAnimation); 
} 

// override the image switcher's methods 

@Override 
public void setImageResource(final int resid) { 
    super.setImageDrawable(blackDrawable); 
    postAfterAnim(new Runnable() { 
     @Override 
     public void run() { 
      CustomImageSwitcher.super.setImageResource(resid); 
     } 
    }); 
} 

// obviously override more if you want.... just change the method name 

} 

這是一個有點深夜的草圖的一些問題是我們沒有正確的代理在未來動畫(你應該或至少非常清楚這將是未設置)。

但粗略的想法是什麼,你真正想要的是在保持一個簡單的黑色繪製,是一箇中間階段的觀點腳蹼的中間視圖。希望它是自我解釋。

在另一方面,在你的榜樣,你可能想拉出來幾個你每個方法創建對象並將它們作爲字段你打算使用它們everytime-例如Handler

作爲最後一點,我真的不要像使用這樣的視圖腳蹼。如果你重寫onDraw等,你可能會發現你可以用一個圖像視圖做所有事情,但這是另一個問題的開始,而不是這個問題的結束。

最良好的祝願,希望這有助於。