Gallery控件与Adapter的应用

        如何实现手机上相册里面的画廊效果,或者是查看图片的轮播效果,现在就讲一下如何实现。

        效果图:


        

Activity部分:

<span style="font-size:14px;">public class GalleryActivity extends Activity {
	
	ImageView iv = null;
	Gallery gallery = null;
	
	int[] drawId = {R.drawable.hongloumeng,R.drawable.sanguoyanyi,R.drawable.shuihuzhuan,R.drawable.xiyouji};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_layout);
		
		iv = (ImageView)findViewById(R.id.image);
		gallery = (Gallery)findViewById(R.id.gallery);
		
		gallery.setAdapter(new MyBase());
		//通过点击事件来更换上面图片。注:区分listview的监听事件
		gallery.<strong>setOnItemSelectedListener</strong>(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				iv.setImageDrawable(getResources().getDrawable(drawId[position]));
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});	
		
	}
	class MyBase extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return drawId.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
                                //加载布局
				LayoutInflater flater = getLayoutInflater();
				View view = flater.inflate(R.layout.gallery_layout_1, null);
				ImageView iv = (ImageView)view.findViewById(R.id.innerImage);	
			        iv.setImageDrawable(getResources().getDrawable(drawId[position]));
			
			return view;
		}

	}
}
</span>

layout文件

a、gallery_layout.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
   <ImageView 
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"        //是图片所占的位置确定,不根据图片大小改变
        />

    <Gallery 
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spacing="10dp"        
        />

</LinearLayout>
</span>
b、gallery_layout_1.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  
    <ImageView 
        android:id="@+id/innerImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout></span>
附上源码:

http://download.youkuaiyun.com/detail/chris_pei/9223027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值