码迷,mamicode.com
首页 > 其他好文 > 详细

Androd UI学习之ImageSwitcher

时间:2015-07-19 10:15:25      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:gallery和imageswitche

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


先贴出最终的效果图:

技术分享


逻辑部分代码:

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;
		}
		
	}
	
}

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

布局文件如下:

    <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。 运行的效果图就是上图所示。





版权声明:本文为博主原创文章,未经博主允许不得转载。

Androd UI学习之ImageSwitcher

标签:gallery和imageswitche

原文地址:http://blog.csdn.net/longwang155069/article/details/46947529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!