Gallery图片轮播
2014-10-20 14:37
302 查看
这是本人做的安卓Gallery图片轮番效果,相对比较简单,但是容易说明。我们看下效果图:
图片上面菜单是一个Gallery,下面图片显示是一个ImageSwitcher。
下面我们看下我们的主布局文件,比较简洁:(就定义了Gallery和ImageSwitcher)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery //标题栏
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageSwitcher //下面的大图片显示
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ImageSwitcher>
</LinearLayout>
下面时我们MainActivity的程序部分主要代码:
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{
private int[] res; //用来装所要显示的图片
private Gallery gallery;
private ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery=(Gallery) findViewById(R.id.gallery); //初始化Gallery
imageSwitcher=(ImageSwitcher) findViewById(R.id.image); //初始化ImageSwitcher
res=new int[]{R.drawable.four1,R.drawable.four2,R.drawable.four3, //初始化图片资源,一共有七张图片
R.drawable.four4,R.drawable.four5,R.drawable.four6,
R.drawable.four7
};
Myadater adapter=new Myadater(res, this); //实例化自定义的适配器,将图片资源和上下文环境传进去
gallery.setAdapter(adapter); //加载适配器
gallery.setOnItemSelectedListener(this); //加载监听器,给imageSwitcher响应
imageSwitcher.setFactory(this); //ImageSwitcher的视图工厂,因为imageSwicther需要借助工厂中makeView方法的才能产生View
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//imageSwicther的动画效果,其比imageView强大的地方-淡入。
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
imageSwitcher.setBackgroundResource(res[position%res.length]); //监听器响应,设置imageSwitcher的背景
}
@Override
public View makeView() { //该方法用于imageSwitcher的View制作,由imageSwitcher.setFactory(this)绑定
// TODO Auto-generated method stub
ImageView imageView=new ImageView(this);
imageView.setScaleType(ScaleType.FIT_CENTER); //返回视图的样式,自适应居中
return imageView;
}
}
至此,我们的准备工作已经做完,其实无非就是在设置布局,配置Gallery监听器,和ImageSwitcher的图片样式。万事具备,只欠适配器,就能将图片放上安卓中。
我们自定义了一个适配器Myadater继承BaseAdapter。下面时Myadater的代码:
public class Myadater extends BaseAdapter{
private int[]res;
private Context context;
public Myadater(int[] res, Context context) { //接收传进来的图片资源和上下文对象
super();
this.res = res;
this.context = context;
}
@Override
public int getCount() { //告诉MainActivity你所要显示内容的的个数,由于我们想要无线循环,即给了一个大数Integer.MAX_VALUE。这个必须得填,不然图片出不来
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) { //获得item对象,一般都会写res[position],在本实例中写不写不重要
// TODO Auto-generated method stub
return res[position];
}
@Override
public long getItemId(int position) { //获得对象的位置id,同样在本实例不重要
// TODO Auto-generated method stub
return position;
}
<!-- 这个非常重要,图片能不能出来就靠它-->
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setBackgroundResource(res[position%res.length]); //设置每个图片相对的背景
imageView.setLayoutParams(new Gallery.LayoutParams(100,100)); //设置图片的大小
imageView.setScaleType(ScaleType.FIT_XY); //设置显示图片样式
return imageView;
}
}
总体思路:
利用适配器将上一栏的菜单栏显示出来,然后设置监听器设置imageSwitcher的背景。
这是本人的理解,还有什么完善的地方欢迎指出。
图片上面菜单是一个Gallery,下面图片显示是一个ImageSwitcher。
下面我们看下我们的主布局文件,比较简洁:(就定义了Gallery和ImageSwitcher)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery //标题栏
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageSwitcher //下面的大图片显示
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ImageSwitcher>
</LinearLayout>
下面时我们MainActivity的程序部分主要代码:
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{
private int[] res; //用来装所要显示的图片
private Gallery gallery;
private ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery=(Gallery) findViewById(R.id.gallery); //初始化Gallery
imageSwitcher=(ImageSwitcher) findViewById(R.id.image); //初始化ImageSwitcher
res=new int[]{R.drawable.four1,R.drawable.four2,R.drawable.four3, //初始化图片资源,一共有七张图片
R.drawable.four4,R.drawable.four5,R.drawable.four6,
R.drawable.four7
};
Myadater adapter=new Myadater(res, this); //实例化自定义的适配器,将图片资源和上下文环境传进去
gallery.setAdapter(adapter); //加载适配器
gallery.setOnItemSelectedListener(this); //加载监听器,给imageSwitcher响应
imageSwitcher.setFactory(this); //ImageSwitcher的视图工厂,因为imageSwicther需要借助工厂中makeView方法的才能产生View
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//imageSwicther的动画效果,其比imageView强大的地方-淡入。
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
imageSwitcher.setBackgroundResource(res[position%res.length]); //监听器响应,设置imageSwitcher的背景
}
@Override
public View makeView() { //该方法用于imageSwitcher的View制作,由imageSwitcher.setFactory(this)绑定
// TODO Auto-generated method stub
ImageView imageView=new ImageView(this);
imageView.setScaleType(ScaleType.FIT_CENTER); //返回视图的样式,自适应居中
return imageView;
}
}
至此,我们的准备工作已经做完,其实无非就是在设置布局,配置Gallery监听器,和ImageSwitcher的图片样式。万事具备,只欠适配器,就能将图片放上安卓中。
我们自定义了一个适配器Myadater继承BaseAdapter。下面时Myadater的代码:
public class Myadater extends BaseAdapter{
private int[]res;
private Context context;
public Myadater(int[] res, Context context) { //接收传进来的图片资源和上下文对象
super();
this.res = res;
this.context = context;
}
@Override
public int getCount() { //告诉MainActivity你所要显示内容的的个数,由于我们想要无线循环,即给了一个大数Integer.MAX_VALUE。这个必须得填,不然图片出不来
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) { //获得item对象,一般都会写res[position],在本实例中写不写不重要
// TODO Auto-generated method stub
return res[position];
}
@Override
public long getItemId(int position) { //获得对象的位置id,同样在本实例不重要
// TODO Auto-generated method stub
return position;
}
<!-- 这个非常重要,图片能不能出来就靠它-->
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setBackgroundResource(res[position%res.length]); //设置每个图片相对的背景
imageView.setLayoutParams(new Gallery.LayoutParams(100,100)); //设置图片的大小
imageView.setScaleType(ScaleType.FIT_XY); //设置显示图片样式
return imageView;
}
}
总体思路:
利用适配器将上一栏的菜单栏显示出来,然后设置监听器设置imageSwitcher的背景。
这是本人的理解,还有什么完善的地方欢迎指出。
相关文章推荐
- 【Android基础知识】使用Gallery和ImageSwitcher实现图片轮播效果
- 微信小程序图片轮播组件gallery slider使用方法详解
- javascript图片轮播之gallery.js
- 微信小程序----gallery slider(图片轮播)组件
- 移植性很高的jquery图片轮播插件 推荐
- 常用实用图片轮播效果jquery插件
- 安卓:ViewPager图片轮播(法2)
- 非常优秀的swiper插件————幻灯片播放、图片轮播
- 一步一步教你写淡入淡出带注释的图片轮播插件(一)
- Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
- 仿网易云音乐 专辑图片折叠轮播
- 7款jQuery图片轮播滑动插件
- (Android学习笔记四)立体Gallery和多点触控浏览图片
- jquery几个实例图片轮播等
- 用jquery面向对象实现的简单的图片轮播,还能学习一下闭包哦
- Android事件分发学习应用-图片轮播实现
- jquery banner广告幻灯片图片轮播切换,模仿实现当当网滚动广告效果
- Android广告图片轮播控件
- Javascript实现图片轮播效果(一)让图片跳动起来
- 网站项目必备——12款经典的白富美型 jQuery 图片轮播插件