Android 之 Gallery画廊用法
程序员文章站
2022-05-16 23:32:58
...
Gallery 可以实现相册浏览效果:画廊
布局文件:
主程序入口:
适配器:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/myImg" android:layout_width="250dp" android:layout_height="200dp" android:src="@drawable/ic_launcher" /> <!-- 图片浏览组件,android:spacing 设置图片间距 --> <Gallery android:id="@+id/mygallery" android:layout_width="fill_parent" android:layout_marginTop="20dp" android:spacing="10dp" android:gravity="center" android:layout_height="100dp" /> </LinearLayout>
主程序入口:
package com.example.advancedview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.ImageView; /** * Gallery :相簿浏览 * 实现步骤: * 1、初始化Gallery 组件 * 2、创建一个新的Adapter,继承BaseAdapter ,这个新的Adapter 负责获取图片资源,图片名字、尺寸等信息; * 并通过重写getView 方法,实现设置图片的尺寸以及显示方式; * 3、为Gallery 组件添加Adapter,为新构建的类 * 4、通过Gallery的setOnItemClickListener 实现单机图片的效果 * @author Administrator * */ public class GalleryActivity extends Activity { private Gallery myGallery ; private ImageView myImg; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery_layout); myGallery = (Gallery) findViewById(R.id.mygallery); myImg = (ImageView) findViewById(R.id.myImg); try { myGallery.setAdapter(new ImageAdapter(this));// 添加适配器 myGallery.setOnItemClickListener(listener); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } // 图片点击事件 private OnItemClickListener listener = new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { GalleryActivity.this.setTitle(String.valueOf(position)); try{ myImg.setImageResource(new ImageAdapter(GalleryActivity.this).myImgList.get(position).intValue()); }catch(Exception ex){ ex.printStackTrace(); } } }; }
适配器:
package com.sun.gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; /** * 图片适配器 -- 结合 画廊进行使用 * @author Administrator * */ public class ImageAdapter extends BaseAdapter { private int[] images; // 数据源 private Context context; public ImageAdapter(Context context,int[] images) { super(); this.context = context; this.images = images; } // 数量 public int getCount() { return images.length; } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { return position; } // 显示的View public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); // 创建一个ImageView视图 imageView.setImageResource(images[position]); // 为当前对象设置图片资源 imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 图片的布局方式 imageView.setLayoutParams(new Gallery.LayoutParams(136, 88)); return imageView; } }
上一篇: 韩国计划2012年底前宽带速度提高到1G
下一篇: Android 之 Frame逐帧动画