Gallery
Gallery.java
public class xiangbu extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*通过findViewById取得*/
Gallery g = (Gallery) findViewById(R.id.mygallery);
/* 添加一ImageAdapter并设置给Gallery对象 */
g.setAdapter(new ImageAdapter(this));
/* 设置一个itemclickListener并Toast被点击图片的位置 */
g.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick
(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText
(xiangbu.this, getString(R.string.my_gallery_text_pre)
+ position+ getString(R.string.my_gallery_text_post),
Toast.LENGTH_SHORT).show();
}
});
}
/* 改写BaseAdapter自定义一ImageAdapter class */
public class ImageAdapter extends BaseAdapter
{
/*声明变量*/
int mGalleryItemBackground;
private Context mContext;
/*ImageAdapter的构造器*/
public ImageAdapter(Context c)
{
mContext = c;
/* 使用在res/values/attrs.xml中的<declare-styleable>定义
* 的Gallery属性.*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
/*取得Gallery属性的Index id*/
mGalleryItemBackground = a.getResourceId
(R.styleable.Gallery_android_galleryItemBackground, 0);
/*让对象的styleable属性能够反复使用*/
a.recycle();
}
/* 覆盖的方法getCount,返回图片数目 */
public int getCount()
{
return myImageIds.length;
}
/* 覆盖的方法getItemId,返回图像的数组id */
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
/* 覆盖的方法getView,返回一View对象 */
public View getView
(int position, View convertView, ViewGroup parent)
{
/*产生ImageView对象*/
ImageView i = new ImageView(mContext);
/*设置图片给imageView对象*/
i.setImageResource(myImageIds[position]);
/*重新设置图片的宽高*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/*重新设置Layout的宽高*/
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
/*设置Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*返回imageView对象*/
return i;
}
/*建构一Integer array并取得预加载Drawable的图片id*/
private Integer[] myImageIds =
{
R.drawable.photo1,
R.drawable.photo2,
R.drawable.photo3,
R.drawable.photo4,
R.drawable.photo5,
R.drawable.photo6,
};
}
}
main.xml
<Gallery xmlns:andro android: android:layout_width="fill_parent" android:layout_height="wrap_content" />
最后更新:2017-04-02 16:48:16
上一篇:
开源项目(不断收集中....)
下一篇:
PicoContainer(Ioc容器)在测试用例中的应用(二)
iphone7plus屏幕被锁定怎么办 苹果8splus被锁死了怎么办
Applications are expected to have a root view controller at the end of application launch
WINDOWS 服务端 SVN自动部署/一键批处理 SVN 更新项目
Core Data浅谈系列之三 : 了解NSManagedObject和NSPredicate
性能调优攻略
关于数据分析,管理者的4个常规错误
SLF4J 使用手册
Ansible 怎么可以缺少阿里云
与Facebook毗邻 苹果再建新数据中心
朝鲜开始正式屏蔽Facebook 、Twitter等网站