Android ApiDemos樣本解析(124):Views->ImageSwitcher

來源:互聯網
上載者:User

前面介紹Android ApiDemos樣本解析(97):Views->Animation->Push 時用到了ViewFlipper ,ViewFlipper 和ViewSwitcher 都是ViewAnimator 的子類,ViewAnimator (FrameLayout的子類)提供了不同View之間切換時的動畫效果支援,應為ViewAnimator 為FrameLayout的子類,因此ViewAnimator中包含的子View都是疊放在一起,一般情況下支援看到最上面的一個View。

ViewFlipper 可以包括2個以上的子View,自動定時切換顯示每個子View。而ViewSwitcher只能最多包括兩個子View。每次只顯示其中一個View,它有兩個子類TextSwitcher 和ImageSwitcher 。本例使用ImageSwitcher 用於在包含在其中的兩個映像之間切換.

ImageSwitcher 定義了setImageDrawable, setImageResource,setImageURI 可以通過不同的方式為ImageSwitcher指定映像,每次調用這些方法,新添加的映像和原來顯示的映像之間切換時將採用由setInAnimation和setOutAnimation指定的動畫效果:

[java] 
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 
mSwitcher.setFactory(this); 
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
 android.R.anim.fade_in)); 
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
 android.R.anim.fade_out)); 

mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
 android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
 android.R.anim.fade_out));

 

本例的Layout ,上方顯示ImageSwitcher,下面是用Gallery顯示映像的縮圖。Gallery用法參見Android ApiDemos樣本解析(119):Views->Gallery->1. Photos

使用者選擇Gallery中某個縮圖時,將使用ImageSwitcher顯示其對應大圖:

[java] 
public void onItemSelected(AdapterView parent, 
 View v, int position, long id) { 
 mSwitcher.setImageResource(mImageIds[position]); 
} 

public void onItemSelected(AdapterView parent,
 View v, int position, long id) {
 mSwitcher.setImageResource(mImageIds[position]);
}

 
作者:mapdigit
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.