本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
Gallery可以显示一系列的图片,并且可以横向滑动。下面展示如何使用Gallery去显示一系列的图片。
1. 创建一个工程,Gallery。
2. main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Images of San Francisco" />
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image1"
android:layout_width="320dp"
android:layout_height="250dp"
android:scaleType="fitXY" />
</LinearLayout>
3. 在res/values文件夹下面新建一个文件,attrs.xml。
4. attrs.xml中的代码。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
5. 准备一些图片。将这些图片放在res/drawable-mdpi下面。
6. GalleryActivity.java中的代码。
public class GalleryActivity extends Activity {
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
7. 按F11在模拟器上面调试。会看见一系列的图片,这些图片可以左右滑动。当单击单个图片的时候,会弹出消息。
首先,我们在main.xml中添加Gallery和ImageView控件:
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image1"
android:layout_width="320dp"
android:layout_height="250dp"
android:scaleType="fitXY" />
前面已经提到过,Gallery用来显示一系列的图片,ImageView用来显示被选中的图片。
这些图片的id被保存在imageIDs数组中:
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
接下来创建BaseAdapter的子类:ImageAdapter,这样一来,我们就能把Gallery与图片资源绑定在一起了。这个适配器起到了桥梁的作用。
使用BaseAdapter的视图的还有:
- ListView
- GridView
- Spinner
- Gallery
BaseAdapter也有一些子类:
- ListAdapter
- ArrayAdapter
- CursorAdapter
- SpinnerAdapter
在ImageAdapter中我们主要实现以下的方法:
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
分享到:
相关推荐
Android Gallery 自定义ImageView和Gallery实现图片浏览功能,类似Android图库软件,主要实现的功能有:图片左右滑动、放大缩小、放大弹回、缩小弹回。 一开始使用Viewpager实现图片浏览,发现两张图片滑动时没有...
RCImageView 自定义圆角ImageView,带边框效果
Android中ImageView实现平铺多张图片Android中实现平铺图片有两种方式:在drawable中定义平铺的Bitmap然后在ImageView中引
android自定义ImageView实现旋转动画
android自定义ImageView,圆角和圆形,抗锯齿.亲测可用。直接拷贝代码就行。
用于android的Imageview缩放库
Android开发之ImageView通过matrix实现两点缩放和图片拖动,内有较为详细的注释
ImageView 图片显示样式的几种类型。android:scaleType是控制图片如何resized/moved来匹对ImageView的size。
android 自定义属性实现 ImageView 透明度渐变效果
Android应用源码之imageView1_imageView
CaptchaImageView:自定义ImageView生成验证码图片
Android中使用自定义ImageView实现图片圆形遮罩效果(常和Glide搭配使用),详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/63834235
自定义可拖动可缩放的ImageView,通过Matrix和TouchListener实现,随后会加旋转。
Android用线程实现ImageView图片变换+可以停止和继续
Android不同形状的ImageView
android ImageView网络图片加载、动态设置尺寸、圆角(绝对好用)
Android ImageView点击选中后添加边框,详细内容可以到我的博客www.heimizhou.com中了解。
The idea of this android library is to give the possibility to display two texts inside the imageitself. These texts could be the title or the description of the image or something else. Also there ...
Gallery中间ImageView变大效果
android 将Imageview 将view生成图片后保存到本地相册的实现生成图片后保存到本地相册的实现,android 将Imageview生成图片后保存到本地相册的实现 android 将Imageview生成图片后保存到本地相册的实现, 有需要的朋友...