Gallery 初体验

这玩意儿已经过时了
这里写图片描述
结构:
结构
代码:
MainActivity.class

package com.superxingyun.gallery;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{

    private int [] res = {R.mipmap.pic1, R.mipmap.pic2, R.mipmap.pic3, R.mipmap.pic4, R.mipmap.pic5, R.mipmap.pic6};
    private Gallery gallery;
    private ImageAdapter adapter;
    private ImageSwitcher is;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gallery = (Gallery) findViewById(R.id.gallery);
        is = (ImageSwitcher) findViewById(R.id.is);


        //gallery加载适配器
        adapter = new ImageAdapter(res, this);
        gallery.setAdapter(adapter);

        gallery.setOnItemSelectedListener(this);

        is.setInAnimation(MainActivity.this, android.R.anim.fade_in);
        is.setOutAnimation(MainActivity.this, android.R.anim.fade_out);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    //imageView.setBackgroundResource(res [i%res.length]);
        is.setBackgroundResource(res [i%res.length]);
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {


    }

    @Override
    public View makeView() {
        ImageView image = new ImageView(this);
        image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return image;
    }
}

自定义适配器ImageAdapter

package com.superxingyun.gallery;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Created by 月满轩尼诗丶大帅~~ on 2016/8/20.
 */
public class ImageAdapter extends BaseAdapter{

    private int [] res;
    private Context context;

    public ImageAdapter(int [] res, Context context){

        this.res = res;
        this.context = context;
    }

    //返回数据源的数量
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int i) {
        return res [Integer.MAX_VALUE] ;
    }

    @Override
    public long getItemId(int i) {
        return Integer.MAX_VALUE;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ImageView imageView = new ImageView(context);
        imageView.setBackgroundResource(res [i%res.length]);
        //imageView.setImageResource(res [i%res.length] );
        imageView.setLayoutParams(new Gallery.LayoutParams(400, 300));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.superxingyun.gallery.MainActivity"
    android:orientation="vertical">

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </Gallery>

    <ImageSwitcher
        android:id="@+id/is"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp">
    </ImageSwitcher>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值