600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 利用Gallery和ImageView实现图片浏览器

利用Gallery和ImageView实现图片浏览器

时间:2020-02-18 03:01:44

相关推荐

利用Gallery和ImageView实现图片浏览器

首先,先看一下实现的截图:

如图所示,

其中布局的第一个控件是Gallery,显示的图片滑动浏览,这里用到一个继承自BaseAdapter的类对象,用于填充和显示Gallery中的内容;

布局的第二个控件是ImageView,当用户滑动Gallery时,显示其中的图片,这其中实现了两个重载方法

1.当用户点击Gallery的图片资源时,以Toast的形式告诉用户点击的是哪一幅图片,实现的代码如下:

/*设定一个itemclickListener并Toast被点选图片的位置*/ g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(EX04_10.this, getString(R.string.my_gallery_text_pre) + position+ getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show();}});

2.当Gallery获得焦点时,ImageView显示用户选择的是哪一幅图片,实现的代码如下:

g.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){// TODO Auto-generated method stubimageView.setImageResource(myImageIds[arg2]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0){// TODO Auto-generated method stub}});

下面给出本实例用到的布局文件和具体的实现源代码:

布局文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><Gallery android:id="@+id/mygallery"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ImageViewandroid:id="@+id/imageview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:scaleType="center"/></LinearLayout>

具体的实现代码:

public class EX04_10 extends Activity { /*建构一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, };private ImageView imageView;/** Called when the activity is first created. */ @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); /*透过findViewById取得*/ Gallery g = (Gallery) findViewById(R.id.mygallery);imageView=(ImageView)findViewById(R.id.imageview);/*新增一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(EX04_10.this, getString(R.string.my_gallery_text_pre) + position+ getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show();}});g.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){// TODO Auto-generated method stubimageView.setImageResource(myImageIds[arg2]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0){// TODO Auto-generated method stub}});}/*改写BaseAdapter自定义一ImageAdapter class*/ public class ImageAdapter extends BaseAdapter {int mGalleryItemBackground;private Context mContext; /*ImageAdapter的建构子*/ public ImageAdapter(Context c) {mContext = c; /* 使用在res/values/attrs.xml中的定义 * 的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;} /*一定要重写的方法getItem,传回position*/ public Object getItem(int position) { return position; } /*一定要重写的方法getItemId,传回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, 110));/*设定Gallery背景图*/ i.setBackgroundResource(mGalleryItemBackground); /*传回imageView物件*/ return i;} }}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。