Androd UI学习之ImageSwitcher

2022-05-08 15:57:27 浏览数 (1)

上一节学习了Gallery,本节结合Gallery来学习ImageSwitcher。

先贴出最终的效果图:

逻辑部分代码:

代码语言:javascript复制
public class ImageSwitcherActivity extends Activity
{
	private static ImageSwitcher mImageSwitcher;
	private static Gallery mGallery;
	private int[] images = 
	{
		R.drawable.a, R.drawable.b,
		R.drawable.c, R.drawable.d,
		R.drawable.e, R.drawable.f,
		R.drawable.h
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_imageswitcher);
		
		mImageSwitcher = (ImageSwitcher)findViewById(R.id.image_switcher);
		mGallery = (Gallery)findViewById(R.id.gallery);
		
		mImageSwitcher.setFactory(new MyViewFactory(this));
		//默认显示中间图片
		mImageSwitcher.setImageResource(images[images.length/2]);
		mGallery.setAdapter(new MyAdapter(this));
		mGallery.setSpacing(10);
		//默认显示中间图片
		mGallery.setSelection(images.length/2);
		//设置监听器,
		mGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mImageSwitcher.setImageResource(images[position%images.length]);
			}
		});
	}
	
	class MyViewFactory implements ViewFactory
	{
		private Context context;
		public MyViewFactory(Context context)
		{
			this.context = context;
		}
		@Override
		public View makeView() {
			ImageView view = new ImageView(this.context);
			view.setBackgroundColor(0xFF000000);
			//设置显示位置
			view.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			return view;
		}
		
	}

	class MyAdapter extends BaseAdapter
	{

		private Context context;
		public MyAdapter(Context context)
		{
			this.context = context;
		}
		
		@Override
		public int getCount() {
			// 获取图片资源的总数
			//return images.length;
			return Integer.MAX_VALUE;
		}

		@Override
		public Object getItem(int position) {
			// 获得图片当前位置
			return position;
		}

		@Override
		public long getItemId(int position) {
			// 获得当前位置的图片ID
			return images[position];
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 获得当前图片资源
			
			ImageView view = new ImageView(this.context);
			
			view.setImageResource(images[position%images.length]);
			view.setAdjustViewBounds(true);
			//设置图片的大小
			view.setLayoutParams(new Gallery.LayoutParams(100, 100));
			//view.setPadding(15, 10, 15, 10);
			return view;
		}
		
	}
	
}

本示例可实现循环选择, 刚进来默认是中间的一张图片

布局文件如下:

代码语言:javascript复制
    <ImageSwitcher 
        android:id="@ id/image_switcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        
    </ImageSwitcher>
    
	<Gallery 
    	android:id="@ id/gallery"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:layout_alignParentBottom="true"
    	android:layout_alignParentLeft="true"
    	/>

ok。 运行的效果图就是上图所示。

0 人点赞