Android之Gallery使用例子
程序员文章站
2023-12-20 15:25:34
刚刚学习android,用gallery作了一个小demo,用来记录一下。
package com.example.galleryex02;
import...
刚刚学习android,用gallery作了一个小demo,用来记录一下。
package com.example.galleryex02; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); settitle("电影海报"); gallery gallery = (gallery) findviewbyid(r.id.gallery1); mygalleryadapter galadapter = new mygalleryadapter(this); gallery.setadapter(galadapter); } public class mygalleryadapter extends baseadapter { context context; integer[] posterid = { r.drawable.mov11, r.drawable.mov12, r.drawable.mov13, r.drawable.mov14, r.drawable.mov15, r.drawable.mov16, r.drawable.mov17, r.drawable.mov18, r.drawable.mov19, r.drawable.mov20 }; public mygalleryadapter(context c) { context = c; } public int getcount() { // todo auto-generated method stub return posterid.length; } public object getitem(int arg0) { // todo auto-generated method stub return null; } public long getitemid(int position) { // todo auto-generated method stub return 0; } public view getview(int position, view convertview, viewgroup parent) { imageview imageview = new imageview(context); imageview.setlayoutparams(new gallery.layoutparams(100, 150)); //设置image大小 imageview.setscaletype(imageview.scaletype.fit_center); imageview.setpadding(5, 5, 5, 5); imageview.setimageresource(posterid[position]); final int pos = position; //点击galley的image的时候 imageview.setontouchlistener(new view.ontouchlistener() { public boolean ontouch(view v, motionevent event) { imageview ivposter = (imageview) findviewbyid(r.id.ivposter); ivposter.setscaletype(imageview.scaletype.fit_center); ivposter.setimageresource(posterid[pos]); return false; } }); return imageview; } } }
效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。