GalleryAndroid中的图片库控件。可以浏览自己的图片相册。

效果图

本程序main.xml源码

本程序ImageAdapter类源码

package com.sx.Gallery;

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello" />  <Gallery      android:id="@+id/gallery"      android:layout_width="fill_parent"      android:layout_height="fill_parent"    android:spacing="2px" />      <!-- android:spacing="2px"用来设置图片之间的间距 -->     </LinearLayout>




JAVA源码

import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter  {           private Context context;               private int[] MyImageIDs =           {   R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,R.drawable.img14,R.drawable.img15,R.drawable.img16,R.drawable.img17,R.drawable.img18,R.drawable.img19,R.drawable.img20         };      public ImageAdapter(Context context)  {  this.context = context;   }  // 获取图片个数  public int getCount()  {    return MyImageIDs.length;//return Integer.MAX_VALUE;}  // 获取图片在库中的位置  public Object getItem(int position)  {   return position;  }  // 获取图片在库中的ID  public long getItemId(int id)  {   return id;  }  public View getView(int position, View convertView, ViewGroup parent)  {  ImageView imageview = new ImageView(this.context);  // 给ImageView设置资源   imageview.setImageResource(this.MyImageIDs[position]);  // 设置布局 图片120*120   imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));   // 设置显示比例类型   imageview.setScaleType(ImageView.ScaleType.FIT_XY);//imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);   return imageview;   }  }  //要想实现图片无限循环可拖动修改getCount()  函数改为返回值为return Integer.MAX_VALUE;//imageview.setImageResource(this.MyImageIDs[position] % MyImageIDs.length.);  即可 Activity源码package com.sx.Gallery;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.Toast;public class GalleryActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);CreateGallery();}             private void CreateGallery()         {        //获得Gallery对象                  Gallery gellery=(Gallery)this.findViewById(R.id.gallery);                         //添加ImageAdapter给Gallery对象 注意哦Gallery类并没有setAdapter这个方法 这个方法是从AbsSpinner类继承的                 gellery.setAdapter(new ImageAdapter(this));                 //设置Gallery的背景图片                  //gellery.setBackgroundResource(R.drawable.bg0);                  //设置Gallery的事件监听                 gellery.setOnItemClickListener(new GalleryItemListener());              }            class GalleryItemListener implements OnItemClickListener         {                public void onItemClick(AdapterView<?> parent, View view, int position,  long id)             {                    Toast.makeText(GalleryActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();                }            }}



更多相关文章

  1. okhttp源码学习分析一
  2. Android—将Bitmap图片保存到SD卡目录下或者指定目录
  3. Android开发经验总结
  4. [置顶] android中加载图片
  5. Android(安卓)textView文字添加图片 imageSpan使用
  6. Android(安卓)Cocos2d实现:一个图片围绕一个圆心做圆运动
  7. phonegap sqlite (android)
  8. Android(安卓)获取网络时间
  9. android 获取web 内容简单实现

随机推荐

  1. android中button设计UI的一个小技巧
  2. Android实现图片轮播切换
  3. Android删除系统自带软件对照表
  4. Android 7.0 之后抓包 unknown 和证书无
  5. android ADB (1) 键盘之adb keycode值
  6. Android 使用 AudioManager 类控制音量
  7. Android DrawerLayout和NavigationView
  8. Android自动化工具Monkeyrunner使用(三)
  9. 使用ant自动化构建android项目
  10. Android:创建网格状的RadioGroup