一.Gallery的简介
Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息。Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图片的效果。
Gallery常用的XML属性
android:animationDuration设置布局变化时动画的转换所需的时间(毫秒级)仅在动画开始时计时。该值必须是整数,比如:100。
android:spacing 图片之间的间距
android:unselectedAlpha 设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。
<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;
}
}
运行效果如下