转载请注明出处: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;
}
}