Gallery、ImageSwitcher

转载请注明出处Gallery、ImageSwitcher_mr-gallery_Mr_Leixiansheng的博客-CSDN博客

 

使用方式类似于ListView

步骤:

1、设置好数据源

2、设置适配器

3、添加适配器

4、实现监听

代码如下:

1、界面设置

<?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:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.leixiansheng.mygallery.MainActivity">

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

    <ImageSwitcher
        android:id="@+id/image_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ImageSwitcher>

</LinearLayout>

2、设置适配器

package com.example.leixiansheng.mygallery;

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 Leixiansheng on 2017/3/20.
 */

public class Myadpter extends BaseAdapter {

    private int[] pics;
    private Context context;

    public Myadpter(int[] pics, Context context) {
        this.pics = pics;
        this.context = context;
    }

    @Override
    public int getCount() {
//        return pics.length;
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int i) {
        return pics[i];
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView = new ImageView(context);
        //不循环显示
//        imageView.setBackgroundResource(pics[i]);
        //循环显示
        imageView.setBackgroundResource(pics[i% pics.length]);
        //设置图片大小
        imageView.setLayoutParams(new Gallery.LayoutParams(400, 300));
        //设置拉伸模式
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }
}

3、主程序实现功能

package com.example.leixiansheng.mygallery;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 AppCompatActivity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{

    private int[] pics = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,
            R.drawable.pic7};

    private Gallery gallery;
    private Myadpter myadpter;
    private ImageSwitcher imageSwitcher;

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

        gallery = (Gallery) findViewById(R.id.gallery);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);

        myadpter = new Myadpter(pics, this);
        gallery.setAdapter(myadpter);
        gallery.setOnItemSelectedListener(this);

        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    }


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


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

    }


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



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值