Gallery为一个画廊控件,只能显示一行,并支持左右滑动的效果。如下:
如上效果的实现包括了Gallery组件和ImageSwitcher组建的使用,完成的效果是点击图片则显示其大图片,程序说话:
- package mobile.android.ch05.gallery;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.content.res.TypedArray;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.view.ViewGroup.LayoutParams;
-
import android.view.animation.AnimationUtils;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.BaseAdapter;
-
import android.widget.Gallery;
-
import android.widget.ImageSwitcher;
-
import android.widget.ImageView;
-
import android.widget.ViewSwitcher.ViewFactory;
-
-
public class Main extends Activity implements ViewFactory {
-
private Gallery gallery;
-
private ImageSwitcher imageSwitcher;
-
private ImageAdapter imageAdapter;
-
-
private int[] resIds = 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, R.drawable.item13, R.drawable.item14,
- R.drawable.item15 };
-
-
-
public class ImageAdapter extends BaseAdapter {
-
int mGalleryItemBackground;
-
private Context mContext;
-
-
public ImageAdapter(Context context) {
- mContext = context;
- TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
-
- mGalleryItemBackground = typedArray.getResourceId(
-
R.styleable.Gallery_android_galleryItemBackground, 0);
- }
-
-
-
public int getCount() {
-
return Integer.MAX_VALUE;
- }
-
-
-
public Object getItem(int position) {
-
return position;
- }
-
-
-
public long getItemId(int position) {
-
return position;
- }
-
-
-
-
-
public View getView(int position, View convertView, ViewGroup parent) {
-
ImageView imageView = new ImageView(mContext);
-
-
- imageView.setImageResource(resIds[position % resIds.length]);
-
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
-
-
imageView.setLayoutParams(new Gallery.LayoutParams(136,88));
-
- imageView.setBackgroundResource(mGalleryItemBackground);
-
return imageView;
- }
- }
-
-
-
-
-
public View makeView() {
-
ImageView imageView = new ImageView(this);
-
-
imageView.setBackgroundColor(0xFF000000);
-
- imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
-
-
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
-
return imageView;
- }
-
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- gallery = (Gallery) findViewById(R.id.gallery);
-
imageAdapter = new ImageAdapter(this);
- gallery.setAdapter(imageAdapter);
-
-
-
-
-
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageswithcher);
-
-
imageSwitcher.setFactory(this);
-
-
-
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
-
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
gallery.setOnItemClickListener(new OnItemClickListener() {
-
-
@Override
-
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
-
long arg3) {
-
- imageSwitcher.setImageResource(resIds[arg2 % resIds.length]);
-
- }
-
- });
-
- }
- }
package mobile.android.ch05.gallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class Main extends Activity implements ViewFactory {
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private ImageAdapter imageAdapter;
private int[] resIds = 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, R.drawable.item13, R.drawable.item14,
R.drawable.item15 };
//设置适配器类
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
// 获取背景资源ID
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
// 返回传入的图片ID数组的长度,也就是图片的总数。既然是数组,那么一定要注意越界问题。
public int getCount() {
return Integer.MAX_VALUE;
}
// 返回position所对应的图片对象
public Object getItem(int position) {
return position;
}
// 取得列表中与指定位置的行关联的ID.
public long getItemId(int position) {
return position;
}
/**
* 设置图像的显示风格图像资源
*/
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
//设置imageView的源,并循环显示
imageView.setImageResource(resIds[position % resIds.length]);
// 设置图片的显示方式
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
// 将图片缩小设置
imageView.setLayoutParams(new Gallery.LayoutParams(136,88));
//设置imageView的图像资源
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
// 返回一个视图,这个视图用来填充imageSwitcher,此为重写 的ViewFactory接口中的类,相当与getView().
public View makeView() {
ImageView imageView = new ImageView(this);
// 设置背景色
imageView.setBackgroundColor(0xFF000000);
// 设置图片显示的缩放方式
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 设置显示的图片对相对容器的填充方式
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return imageView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
// gallery.setUnselectedAlpha(0.5f); // 设置未选中图片的透明度
// gallery.setSpacing(0); // 设置图片之间的间距
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageswithcher);
imageSwitcher.setFactory(this);
//设置淡入淡出效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
// //为ImageSwitcher组建设置其图片源,是当图片滑到屏幕正中,则视为自动选中,在滑动的过程中会触发
// gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
//
// public void onItemSelected(AdapterView<?> arg0, View arg1,
// int arg2, long arg3) {
// imageSwitcher.setImageResource(resIds[arg2%resIds.length]);
//
// }
//
// public void onNothingSelected(AdapterView<?> arg0) {
// // TODO Auto-generated method stub
//
// }
//
// });
//是需要用手点击才触发,滑动时不触发
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(resIds[arg2 % resIds.length]);
}
});
}
}
Gallery的使用主要包括如下几个步骤:
1 资源文件,复制进res/drawable文件夹中
2 定义Gallery 组件的背景样式文件
3 含有Gallery组件和ImageSwitcher组件的main.xml文件
4 BaseAdapter类,并实现其中的方法
5 Gllery组件绑定Adapter
6 Gallery组件的onItemSelected方法,为ImageSwitcher组件设置其图片源
7 ImageSwitcher中的ImageView控件
附录:
** TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);R.styleable.Gallery
** 对于BaseAdapter类中getView()方法的解析:
public abstract View getView (int position, View convertView, ViewGroup parent) 此为抽象方法,一般与ListView这个组件一起使用。
三个参数的意义:POSITION就是LISTVIEW中的位置,如果你对LIST几个特殊位置的VIEW要改变,比如TITLE要改变,那么就是加判断
if(position == 0){
//加入自己的view
return view;
}
convertView是系统的,如果你在重写的时候没有绑定自己的VIEW就调用这个。
最后,关于Gallery的响应事件:
1、点击事件OnItemClickListener,是需要用手点击才触发,滑动时不触发
2、选中事件OnItemSelectedListener,是当图片滑到屏幕正中,则视为自动选中,在滑动的过程中会触发
适用场景:
1、点击事件OnItemClickListener,是在确定要选中该项时,才点击进行逻辑处理
2、选中事件OnItemSelectedListener,可以用来提醒用户,当前获取焦点的项,如果确认为该项则需要点击 OnItemClickListener后,进行下一步的逻辑处理。
相关推荐
Gallery控件一般是用于显示图像列表,因此也称为是画廊控件, Gallery只能水平显示一行,而且支持水平滑动效果。也就是说,单击、选中或者拖动Gallery中的图像, Gallery图像中的列表会根据不同的情况向左向右移动...
031_android UI组件之 Gallery画廊控件,很细致的视频教程,这个可以学哦。
画廊Gallery能够水平方向显示内容,并且可用手指直接拖动图片移动,一般用来浏览图片,被选中的选项位于中间,并且可用影响事件显示信息。在使用画廊视图时,首先在屏幕上添加Gallery组件。使用画廊视图,也需要使用...
android画廊,利用android自带的Gallery和ImageSwitcher控件,实现滚动画面
介绍:效果不错的画廊控件 可以设置画廊一次可见图片的张数,和其他第三方Gallery控件不同的是,该控件直接继承自View,而不是sdk中的Gallery控件。如果想获得包含demo的可运行代码,请在本站的下载链接中下载。项目...
Gallery画廊控件用法实例源代码,包含2D及3D两种用法
Android使用更多更好看图像控件的代码例子。用于演示在app中操作画廊控件Gallery、图像切换控件ImageSwitcher、调色板控件Palette、卡片视图控件CardView的使用工程与效果。
ListView添加数据源,SimpleAdapter 重点是ListView事件处理,OnItemClickListener 如果和上下文菜单关联,点上下文菜单...画廊控件:gallery imageswitch HorizontalScrollView,在一个屏幕上显示出超过屏幕尺寸的内容
使用自定义 LayoutManager 实现 Android 中 Gallery 或者 ViewPager 控件的效果,支持垂直和水平两个方向,支持 RecycleView 的试图回收机制
Imageswitcher和Gallery实现同步 同时加载网络图片 并保持自动播放
完全自定义的gallery控件,仿照ios的3D画廊效果,代码里边的注释很清楚了,看了就会了。
Android高级图片滚动控件,3D版的图片轮播器Demo.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
react-image-gallery - 响应式图像画廊、旋转木马、图像滑块react组件
Android-图库-应用这是一个 android 画廊应用程序,使用 android studio 开发
Android Gallery画廊 图片展示控件 博客地址:http://blog.csdn.net/csdnyuandaimaxuexi/article/details/48809349
Gallery是一个内部元素控件,可以水平滚动,并且可以把当前选择的子元素定位在它中心的布局组件;画廊Gallery一般用来显示可左右移动图片的列表(具体请看效果:http://blog.csdn.net/zhh_csdn_ard)
自动播放的Gallery,底部有圆点显示当前第几页,实现循环播放
这是一个android下的自定义控件,3d的Gallery效果,详细请参见: http://blog.csdn.net/allen315410/article/details/39932689
该画廊代码实例,主要介绍画廊这一控件的基本使用。希望能为您提供帮助,谢谢您的下载。
个人比较认可这种效果,相对于安卓的Gallery控件(已经不推荐使用)以及后来的ViewPager,横向滚动的ListView,以及网上用RecyclerView实现的各种所谓Gallery,搜狐视频的这个Gallery要有意味的多。 实际上搜狐视频...