ImageSwitcher的用法

ImageSwitcher与TextSwitcher都继承了ViewSwitcher,其用法也是大同小异。都可以通过implements ViewSwitcher.ViewFactory来设置转换的view,自己照着demo学完后仿着TextSwitcher写了下面这个小例子。

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,不多解释。  

你可能感兴趣的:(ImageSwitcher的用法)