这玩意儿已经过时了
结构:
代码:
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>