Android 画廊控件Gallary

发布时间:2025-12-09 17:21:33 浏览次数:4

Android 画廊控件Gallary。将图片显示成连续的带状。 

 

package com.gallerydemo;import java.lang.reflect.Field;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class MainActivity extends Activity {/************************************** 画廊控件的使用Gallery* * 1.Gallery 显示的是一个水平的列表选择框允许用户通过拖动来显示上一个下一个列表项 2.使用步骤:a:定义布局文件 b:定义数据适配器类* c:关联Gallery控件和数据适配器 d:为Gallery每个item添加事件。* 3.由于使用大量图片可能出现内存溢出,API16已经废弃,HorizontableScroolView和ViewPager* ************************************/private Gallery gallery; // 在API 16中已经不再支持private String TAG = "GalleryDemo";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);gallery = (Gallery) findViewById(R.id.gallery);// 关联Gallery和数据适配器try {gallery.setAdapter(new ImageAdapter(this));} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}Log.i(TAG, "====5");gallery.setOnItemClickListener(new OnItemClickListener() {// @Override/** parent:指向数据适配器的指针 v:对应于item view的句柄* position:item在数据适配器adapter中的位置 id:item在数据适配器的第几行*/public void onItemClick(AdapterView parent, View v, int position,long id) {// TODO Auto-generated method stubMainActivity.this.setTitle(String.valueOf(position));Log.i(TAG, "position: " + position + "id: " + id);}});}/** BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListView、Spinner、* Gallery及GridView等UI显示组件,它是继承自接口类Adapter, 有关BaseAdapter* http://www.cnblogs.com/mandroid/archive/2011/04/05/2005525.html*/// 为Gallery提供数据源private class ImageAdapter extends BaseAdapter {private Context mContext;private ArrayList<Integer> imgList = new ArrayList<Integer>();private ArrayList<Object> imgSize = new ArrayList<Object>();// 建立与资源之间的关联public ImageAdapter(Context context) throws IllegalArgumentException,IllegalAccessException {mContext = context;// 利用反射机制来获取资源中的图片ID和尺寸Field[] fields = R.drawable.class.getDeclaredFields();for (Field field : fields) {if (!"ic_launcher".equals(field.getName()))// 除了icon之外的图片{int index = field.getInt(R.drawable.class);imgList.add(index);// 这里把int 转换成了Integerint size[] = new int[2];Bitmap bmImg = BitmapFactory.decodeResource(getResources(),index);size[0] = bmImg.getWidth();size[1] = bmImg.getHeight();imgSize.add(size);}}}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imgList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub// int是JAVA的一个基本类型,而Integer是JAVA的一个类,对应// int。因为在某些地方不可以用int而要用Integer。而且基本类型运算的速度也要快。ImageView i = new ImageView(mContext);i.setImageResource(imgList.get(position).intValue());i.setScaleType(ImageView.ScaleType.FIT_XY);int size[] = new int[2];size = (int[]) imgSize.get(position);i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));return i;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


资源文件main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Galleryandroid:id="@+id/gallery"android:layout_width="fill_parent"android:layout_height="fill_parent"></Gallery></LinearLayout>


 

 

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477