ImageSwitcher與TextSwitcher都繼承了ViewSwitcher,其用法也是大同小異。均可以經過implements ViewSwitcher.ViewFactory來設置轉換的view,本身照着demo學完後仿着TextSwitcher寫了下面這個小例子。java
public class ImageSwitcherexcercise extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener{ int index=0; private ImageSwitcher imageSwitcher; private int[] images; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); images=new int[]{R.drawable.one,R.drawable.two,R.drawable.three}; Button button=(Button) findViewById(R.id.button1); imageSwitcher=(ImageSwitcher) findViewById(R.id.imageswitcher); imageSwitcher.setFactory(this); button.setOnClickListener(this); showImage(); } private void showImage() { // TODO Auto-generated method stub imageSwitcher.setImageResource(images[index]); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub index++; showImage(); } @Override public View makeView() { // TODO Auto-generated method stub ImageView imageView=new ImageView(this); return imageView; } }
能夠對比TextSwitcher學習ImageSwitcher,很少解釋。 ide