BaseAdapter + Gallery

程式碼修行者發表於2015-07-23

galleryAdapter.java

package com.example.administrator.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 Administrator on 2015/7/23.
 */
public class galleryAdapter extends BaseAdapter{
    private int[] resId;
    private Context mContext;

    public galleryAdapter(int[] resId,Context mContext) {
        this.resId = resId;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
//        return resId.length;
        return Integer.MAX_VALUE; //讓gallery迴圈,所以設定為最大數
    }


    @Override
    public Object getItem(int position) {
        return resId[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView img = new ImageView(mContext);
//        img.setBackgroundResource(resId[position]);
        img.setBackgroundResource(resId[position % resId.length]); //迴圈

        //設定縮放
//        img.setLayoutParams(new Gallery.LayoutParams(300,400));
//        img.setScaleType(ImageView.ScaleType.FIT_XY);

        return img;
    }
}

MainActivity.java

package com.example.administrator.gallery;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;


public class MainActivity extends ActionBarActivity implements ViewSwitcher.ViewFactory{
    private Gallery gallery;
    private ImageSwitcher switcher;
    private int[] resId = new int[]{R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12};


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

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

        galleryAdapter adapter = new galleryAdapter(resId,this);
        gallery.setAdapter(adapter);


        gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                img.setBackgroundResource(resId[position % resId.length]); //迴圈
                switcher.setBackgroundResource(resId[position % resId.length]);
            }

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

            }
        });

        //ImageSwitcher載入工廠
        switcher.setFactory(this);
        //動畫效果
        switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

    }


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

這裡寫圖片描述

相關文章