Android中Gallery的应用
本文讲述了Android中Gallery的应用。
例子:
有兴趣的朋友可以在登录后下载本文例子代码!
实现步骤:
第一步:建?Android 工程:GalleryDemo。
第二步:编写Activity 的子类别:GalleryDemo,其程序代码如下:
package com.a3gs.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.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.myGa);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(GalleryDemo.this, "这是图片" + arg2, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter{
/*声明变量*/
private int gItemBg;
private Context mContext;
private Integer[] imgIds = {
R.drawable.photo1,
R.drawable.photo2,
R.drawable.photo3,
R.drawable.photo4,
R.drawable.photo4,
R.drawable.photo5,
R.drawable.photo6
};
/*ImageAdapter的构造器*/
public ImageAdapter(Context c)
{
mContext = c;
/* 使用在res/values/attrs.xml中的<declare-styleable>定义
* 的Gallery属性.*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
/*取得Gallery属性的Index id*/
gItemBg = a.getResourceId
(R.styleable.Gallery_android_galleryItemBackground, 0);
gItemBg = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
/*让对象的styleable属性能够反复使用*/
a.recycle();
}
public int getCount() {
// TODO Auto-generated method stub
return imgIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
// 设置图片给ImageView对象
i.setImageResource(imgIds[position]);
// 重新设置图片的宽高
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 重新设置Layout的宽高
i.setLayoutParams(new Gallery.LayoutParams(150,120));
/*设置Gallery背景图*/
// i.setBackgroundResource(gItemBg);
return i;
}
}
}
第三步:修改res/layout/main.xml,其代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Gallery
android:id="@+id/myGa"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
第四步:添加一个res/values/attrs.xml,用来改变layout 的背景图,其代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
第四步:在res/drawable/..底下放入六张图片,如下:
扩展学习
在 Android:ScaleType 中定义了下列常数可供使用,通过“ObjectView.ScaleType 常数名称” 的方式,就可以改变图片的显示方式。
常数名称 | 值 |
Matrix | 0 |
fitXY | 1 |
fitStart | 2 |
fitCenter | 3 |
fitEnd | 4 |
center | 5 |
centerCrop | 6 |
centerInside | 7 |
另外,在主程序中,使用了下面这一段写法:
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
这是一个引用自制layout 元素的用法,必须在res/values 下面添加一个attrs.xml,并在其中定义 <declare-styleable> 标签TAG,目的是自定义layout 的背景风格,并且通过TypeArray 的特性,让相同的Layout 元素可以重复用于每一张图片。
更多相关文章
- C# mono android 图片上传进度条实现
- android笔记一 控件属性
- Android获取本地图片之ACTION_GET_CONTENT与ACTION_PICK区别
- [Android] 启动无线与网络设置的Action___android:duplicatePare
- android中属性动画
- Shape属性详解
- 关于文字颜色/图片背景---selector状态列表
- 在AndroidManifest.xml文件中的android:windowSoftInputMode属性
- [Android] 代码实现按钮/图片自旋转(中心旋转)