图集

举个栗子
xml

<ImageSwitcher android:id="@+id/imageSwitcher"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_centerInParent="true" />

<Gallery android:id="@+id/gallery" android:background="#55000000"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" android:spacing="0dp" />复制代码

java

public class xx extends Activity {
// 显示图片的资源
private static int[] images = {R.mipmap.wallpaper_x, xx};
Gallery gallery;
ImageSwitcher is;
@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xx);
    gallery = (Gallery) findViewById(R.id.gallery);
    is = (ImageSwitcher) findViewById(R.id.imageSwitcher);
    gallery.setAdapter(new ImageAdapter(this));
    // 让选定的图片在中心显示
    gallery.setSelection(images.length / 2);
    // 为Gallery绑定监听器;
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            // 当在Gallery中选定一张图片是 ImageSwitcher同步显示同一张
            // position%images.length 为了让图片循环显示
            is.setImageResource(images[position % images.length]);

        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    is.setFactory(new ImageFactory(this));

}

private class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context context) {
        this.context = context;
    }

    // 可以return images.lenght(),在这里返回Integer.MAX_VALUE
    // 是为了使图片循环显示
    public int getCount() {
        return images.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv = new ImageView(context);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setImageResource(images[position % images.length]);
        iv.setLayoutParams(new Gallery.LayoutParams(250, 350));
        iv.setAdjustViewBounds(true);
        return iv;
    }
}

private class ImageFactory implements ViewFactory {
    private Context context;

    public ImageFactory(Context context) {
        this.context = context;
    }

    public View makeView() {
        ImageView iv = new ImageView(context);
        iv.setLayoutParams(new ImageSwitcher.LayoutParams(-1, -1));
        return iv;
    }
}复制代码

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值