安卓高级组件-----图像切换器

        安卓图像切换器<ImageSwitch>是一种能够实现图像序列播放的组件,类似于“windows照片查看器”点击左右按钮实现按顺序查看照片。ImageSwitch实际上是继承了ViewSwitch,重写了ViewSwitch的showNext() showprevious()两个方法,这使得查看上下某张图片变得十分简单。

        ImageSwitch提供了一个ViewFactory接口,ViewFactory生成的View组件必须是ImageView。进行图片切换时,只要调用setImageResource(int resid) 方法更换图片。

图片切换器的实现:

1.新建工程,布局中放入ImageSwitch组件和两个按钮

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageSwitcher
            android:id="@+id/imageSwitcher1"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ImageSwitcher>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上一张" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下一张" />

        </LinearLayout>

    </LinearLayout>

2.在主活动创建一个图像id数组和图像切换器对象还有按钮

    private int[] image = new int[]{R.drawable.photo1,R.drawable.photo2,
            R.drawable.photo3,R.drawable.photo4};              //图像数组
    
    private int index = 0;             //下标
    
    private ImageSwitcher is;         //切换器

private Button up,down;

3.获取组件实例化并设置ImageSwitch.setFactory()

        is = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
        
        up = (Button)findViewById(R.id.button1);
        down = (Button)findViewById(R.id.button2);
        up.setOnClickListener(this);
        down.setOnClickListener(this);
        
        is.setFactory(new ViewFactory() {
            
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                                
                return imageView;
            }
        });
        is.setImageResource(image[index]);
    }

4.改按钮加监听,监听接口在Activity实现。此处注意,监听事件加上以后,获取的是点击的View组件id,通过switch判断点击的按钮是上一张还是下一张的按钮。

其实,监听接口使用的是View的监听接口,返回的对象是View,通过View.getId()获取

        up.setOnClickListener(this);
        down.setOnClickListener(this);

    public void onClick(View v) {
        
        switch(v.getId()){
        
        case R.id.button1:
            
            if(index > 0){
                index --;
            }else {
                index = image.length - 1;
            }
            is.setImageResource(image[index]);break;
            
        case R.id.button2:
            if(index == image.length - 1){
                index = 0;
            }else {
                index ++;
            }
            is.setImageResource(image[index]);break;            
        }
        
    }

运行效果:

总结:我们主Activity实现的View的接口, public void onClick(View v) {},在这个方法里面我们队v进行判断,反应了安卓组件是继承自View类。

原文地址:https://www.cnblogs.com/divingpig/p/6493591.html