Gallery与Spinner有共同父类:AbsPinner,说明Gallery与Spinner都是一个列表框。它们之间的区别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是一个水平的列表选择框。Spinner的作用是供用户选择,而Gallery则允许用户通过拖动查看上一个,下一个。
Gallery用法与Spinner的用法形似,只要为它提供一个内容Adapter就可以了。Adapter的getView方法返回View作为Gallery列表的列表项。如果程序需要监控Gallery选择项的改变,可以添加OnItemSelectedListener监听即可。
Gallery 的xml属性
下面通过一个幻灯片例子来熟悉Gallery
(1)activity_main.xml 布局一个ImageSwitcher 和Gallery ImageSwitcher用于显示Gallery选中的图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageSwitcher android:id="@+id/imgSwt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:unselectedAlpha="0.7" android:spacing="2pt"/> </LinearLayout>
(2)MainActivity.java
package com.example.gallery; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.RadioGroup.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { //定义组件 private ImageSwitcher imgSwt = null; private Gallery gallery = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取布局组件 imgSwt = (ImageSwitcher) findViewById(R.id.imgSwt); gallery = (Gallery) findViewById(R.id.gallery); //图片 final int images[] = new int[]{ R.drawable.name01,R.drawable.name02,R.drawable.name03, R.drawable.name04,R.drawable.name05,R.drawable.name06, R.drawable.name07,R.drawable.name08,R.drawable.name09, R.drawable.name10,R.drawable.name11,R.drawable.name12 }; //设置图片切换效果 imgSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imgSwt.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); //设置ViewFactory对象 imgSwt.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(350,350)); return imageView; } }); //创建BaseAdapter对象,负责提供Gallery显示所有图像 BaseAdapter adapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { //创建imageview ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(images[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); return imageView; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return position; } @Override public int getCount() { return images.length; } }; //给Gallery设置适配器 gallery.setAdapter(adapter); //添加事件 gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imgSwt.setImageResource(images[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } }
运行效果如下:
android学习---Gallery画廊视图,布布扣,bubuko.com
时间: 2024-10-11 05:52:27