Gallery 和ImageSwitcher实现照片墙功能

一.Gallery的简介

Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息。Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图片的效果。

Gallery常用的XML属性 
android:animationDuration设置布局变化时动画的转换所需的时间(毫秒级)仅在动画开始时计时。该值必须是整数,比如:100。
android:spacing 图片之间的间距
android:unselectedAlpha 设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。 

activity_main.xml 代码如下

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

    <ImageSwitcher
        android:id="@+id/is"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
MainActivity.java的代码如下

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
public class MainActivity extends Activity <strong>implements OnItemSelectedListener</strong>{
	@SuppressWarnings("deprecation")
	private Gallery gallery;
	private ImageSwitcher is;
	public int[] id = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
			R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
			R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
			R.drawable.p12, R.drawable.p13 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gallery = (Gallery) findViewById(R.id.gallery);
		<strong>MyGallery myGallery = new MyGallery(this, id);</strong>
		gallery.setAdapter(myGallery);
		is=(ImageSwitcher) findViewById(R.id.is);
		gallery.setOnItemSelectedListener(this);
	
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		<strong>is.setBackgroundResource(id[arg2%id.length]);</strong>
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}



}

我们要写一个MyGallery类。其代码如下

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MyGallery extends BaseAdapter {
	int[] id;
	Context context;

	public MyGallery(Context context, int[] id) {
		this.id = id;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		<strong>// return id.length;//这样的话不能无限循环
		return Integer.MAX_VALUE;// 这样的话能无限循环</strong>
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return id[arg0];
	}

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

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView imageView = new ImageView(context);
		<strong>imageView.setBackgroundResource(id[arg0 % id.length]);</strong>
		imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
		// imageView.setScaleType(ScaleType.FIT_XY);
		return imageView;
	}

}

运行效果如下



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值