Gallery、ImageSwitcher使用实现画廊功能

1 ImageSwitcher是图片切换的控件,它能实现图片切换时的动画效果,包括图片导入效果、图片消失效果等等。

   Android系统提供了许多不同的动画效果供我们选择。

2  TypedArray obtainStyledAttributes 作用就是从我们自己定义的attr.xml读取所需信息 

3 Gallery 组件的作用是提供图片的缩图功能,它可以将好多的图片排列成一列让使用者浏览和点选,

它也会随着使用者的操作来自动转换图像的位置

 代码:

 1 Activity 

  

public class Gallery2Activity extends Activity implements ViewFactory,OnItemClickListener  {

 
    private ImageSwitcher imageSwitcher;  
    private Gallery gallery;  
   //保存图片的数组
   int[] imageIds = new int[] { R.drawable.beibei, R.drawable.huanhuan,
			R.drawable.jingjing, R.drawable.nini, R.drawable.yingying };
	
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        // 设置窗体无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_gallery2);  
        // 得到组件  
        imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);  
        imageSwitcher.setFactory(this);  
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_in));  
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_out));  
  
        gallery = (Gallery) this.findViewById(R.id.gallery1);  
        gallery.setAdapter(new ImageAdapter());  
        //设置默认选择的图片  
        gallery.setSelection(imageIds.length/2);  
        //注册事件监听器  
        gallery.setOnItemClickListener(this);  
    }  
  
 
  
    @Override  
    public void onItemClick(AdapterView<?> adapterview, View view, int postion,  
            long id) {  
        imageSwitcher.setImageResource(imageIds[postion]);  
  
    }  
  
    // 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置  
    @Override  
    public View makeView() {  
        ImageView imageView = new ImageView(this);  
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(  
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
        return imageView;  
    }  
  
    /** 
     * 负责产生gallery中的图片 
     */  
    private class ImageAdapter extends BaseAdapter {  
        int mGalleryItemBackground;;  
        public ImageAdapter(){  
        	//获取自定义的背景属性值 
            TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);  
            mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);  
            typedArray.recycle();  //必须调用的方法
        }  
  
        // 返回图片的个数,比如你想得到图片的个数  
        @Override  
        public int getCount() {  
            return imageIds.length;  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return imageIds[position];  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
  
            ImageView imageView = new ImageView(Gallery2Activity.this);  
            // 设置imageView中的图像资源  
            imageView.setImageResource(imageIds[position]);  
            /*// 设置图像大小尺寸自适应 
            imageView.setAdjustViewBounds(true);*/  
            imageView.setBackgroundResource(mGalleryItemBackground);  
            return imageView;  
        }  
    }  
}

 2   页面布局 

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
    <ImageSwitcher  
        android:id="@+id/imageSwitcher1"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true" >  
    </ImageSwitcher>  
  
    <Gallery  
        android:id="@+id/gallery1"  
        android:layout_width="fill_parent"  
        android:layout_height="60dp"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:background="#55000000"  
        android:spacing="10dp" />  
</RelativeLayout> 

 3 自定义背景属性 values中的items.xml

   

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="HelloGallery">  
        <attr name="android:galleryItemBackground" />  
    </declare-styleable>  
</resources> 

猜你喜欢

转载自username2.iteye.com/blog/2188218
今日推荐