Android UI, Gallery(过时), ImageSwitcher, 简单图片浏览工具

来源:互联网 发布:re take 知乎 编辑:程序博客网 时间:2024/05/14 15:24

ImageSwitcher和Gallery

ImageSwitcher:图片切换

Gallery:画廊,开发中也可用于做滑动的菜单

1.定义布局文件activity_main.xml

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.     <ImageSwitcher  
  6.         android:id="@+id/imageSwitcher1"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:layout_alignParentLeft="true"  
  10.         android:layout_alignParentTop="true" >  
  11.     </ImageSwitcher>  
  12.   
  13.     <Gallery  
  14.         android:id="@+id/gallery1"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="60dp"  
  17.         android:layout_alignParentBottom="true"  
  18.         android:layout_alignParentLeft="true"  
  19.         android:background="#55000000"  
  20.         android:spacing="10dp" />  
  21. </RelativeLayout>  


2.定义控制器Activity

[java] view plaincopy
  1. public class MainActivity extends Activity implements ViewFactory,  
  2.         OnItemClickListener {  
  3.     private ImageSwitcher imageSwitcher;  
  4.     private Gallery gallery;  
  5.     // 存放大图的数组  
  6.     private int[] imagesLarge = { R.drawable.sample_0, R.drawable.sample_1,  
  7.             R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,  
  8.             R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };  
  9.     // 存放小图的数组  
  10.     private int[] imagesSmall = { R.drawable.sample_thumb_0,  
  11.             R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,  
  12.             R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,  
  13.             R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,  
  14.             R.drawable.sample_thumb_7 };  
  15.   
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         // 设置窗体无标题  
  20.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  21.         setContentView(R.layout.activity_main);  
  22.         // 得到组件  
  23.         imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);  
  24.         imageSwitcher.setFactory(this);  
  25.         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  26.                 android.R.anim.fade_in));  
  27.         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  28.                 android.R.anim.fade_out));  
  29.   
  30.         gallery = (Gallery) this.findViewById(R.id.gallery1);  
  31.         gallery.setAdapter(new ImageAdapter());  
  32.         //设置默认选择的图片  
  33.         gallery.setSelection(imagesSmall.length/2);  
  34.         //注册事件监听器  
  35.         gallery.setOnItemClickListener(this);  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onCreateOptionsMenu(Menu menu) {  
  40.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  41.         return true;  
  42.     }  
  43.   
  44.     @Override  
  45.     public void onItemClick(AdapterView<?> adapterview, View view, int postion,  
  46.             long id) {  
  47.         imageSwitcher.setImageResource(imagesLarge[postion]);  
  48.   
  49.     }  
  50.   
  51.     // 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置  
  52.     @Override  
  53.     public View makeView() {  
  54.         ImageView imageView = new ImageView(MainActivity.this);  
  55.         //imageView.setLayoutParams(new ImageSwitcher.LayoutParams(  
  56.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  57.         return imageView;  
  58.     }  
  59.   
  60.     /** 
  61.      * 负责产生gallery中的图片 
  62.      */  
  63.     private class ImageAdapter extends BaseAdapter {  
  64.   
  65.         // 返回图片的个数,比如你想得到图片的个数  
  66.         @Override  
  67.         public int getCount() {  
  68.             return imagesSmall.length;  
  69.         }  
  70.   
  71.         @Override  
  72.         public Object getItem(int position) {  
  73.             return imagesSmall[position];  
  74.         }  
  75.   
  76.         @Override  
  77.         public long getItemId(int position) {  
  78.             return position;  
  79.         }  
  80.   
  81.         @Override  
  82.         public View getView(int position, View convertView, ViewGroup parent) {  
  83.   
  84.             ImageView imageView = new ImageView(MainActivity.this);  
  85.             // 设置imageView中的图像资源  
  86.             imageView.setImageResource(imagesSmall[position]);  
  87.             // 设置图像大小尺寸自适应  
  88.             imageView.setAdjustViewBounds(true);  
  89.             return imageView;  
  90.         }  
  91.     }  
  92. }  


3.给画廊中的图片加边框

步骤1:在values下定义item.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="HelloGallery">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6. </resources>   


步骤2:修改ImageAdapter类

[java] view plaincopy
  1. /** 
  2.      * 负责产生gallery中的图片 
  3.      */  
  4.     private class ImageAdapter extends BaseAdapter {  
  5.         int mGalleryItemBackground;;  
  6.         public ImageAdapter(){  
  7.             TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);  
  8.             mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);  
  9.             typedArray.recycle();  
  10.         }  
  11.   
  12.         // 返回图片的个数,比如你想得到图片的个数  
  13.         @Override  
  14.         public int getCount() {  
  15.             return imagesSmall.length;  
  16.         }  
  17.   
  18.         @Override  
  19.         public Object getItem(int position) {  
  20.             return imagesSmall[position];  
  21.         }  
  22.   
  23.         @Override  
  24.         public long getItemId(int position) {  
  25.             return position;  
  26.         }  
  27.   
  28.         @Override  
  29.         public View getView(int position, View convertView, ViewGroup parent) {  
  30.   
  31.             ImageView imageView = new ImageView(MainActivity.this);  
  32.             // 设置imageView中的图像资源  
  33.             imageView.setImageResource(imagesSmall[position]);  
  34.             /*// 设置图像大小尺寸自适应 
  35.             imageView.setAdjustViewBounds(true);*/  
  36.             imageView.setBackgroundResource(mGalleryItemBackground);  
  37.             return imageView;  
  38.         }  
  39.     }  


效果图:

0 0
原创粉丝点击