简单图片浏览工具—ImageSwitcher和Gallery组件结合使用
2012-09-23 18:34
447 查看
ImageSwitcher和Gallery
ImageSwitcher:图片切换
Gallery:画廊,开发中也可用于做滑动的菜单
1.定义布局文件activity_main.xml
<RelativeLayout 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" > <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ImageSwitcher> <Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#55000000" android:spacing="10dp" /> </RelativeLayout>
2.定义控制器Activity
public class MainActivity extends Activity implements ViewFactory, OnItemClickListener { private ImageSwitcher imageSwitcher; private Gallery gallery; // 存放大图的数组 private int[] imagesLarge = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; // 存放小图的数组 private int[] imagesSmall = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置窗体无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 得到组件 imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); gallery = (Gallery) this.findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter()); //设置默认选择的图片 gallery.setSelection(imagesSmall.length/2); //注册事件监听器 gallery.setOnItemClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onItemClick(AdapterView<?> adapterview, View view, int postion, long id) { imageSwitcher.setImageResource(imagesLarge[postion]); } // 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置 @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } /** * 负责产生gallery中的图片 */ private class ImageAdapter extends BaseAdapter { // 返回图片的个数,比如你想得到图片的个数 @Override public int getCount() { return imagesSmall.length; } @Override public Object getItem(int position) { return imagesSmall[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(MainActivity.this); // 设置imageView中的图像资源 imageView.setImageResource(imagesSmall[position]); // 设置图像大小尺寸自适应 imageView.setAdjustViewBounds(true); return imageView; } } }
3.给画廊中的图片加边框
步骤1:在values下定义item.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
步骤2:修改ImageAdapter类
/** * 负责产生gallery中的图片 */ private class ImageAdapter extends BaseAdapter { int mGalleryItemBackground;; public ImageAdapter(){ TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); typedArray.recycle(); } // 返回图片的个数,比如你想得到图片的个数 @Override public int getCount() { return imagesSmall.length; } @Override public Object getItem(int position) { return imagesSmall[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(MainActivity.this); // 设置imageView中的图像资源 imageView.setImageResource(imagesSmall[position]); /*// 设置图像大小尺寸自适应 imageView.setAdjustViewBounds(true);*/ imageView.setBackgroundResource(mGalleryItemBackground); return imageView; } }
效果图:
相关文章推荐
- Gallery和imageSwitcher结合使用浏览图片(简单图片浏览器)
- 简单图片浏览工具——ImageSwitcher:实现上一幅、下一幅切换功能
- 使用ImageSwitcher和Gallery实现图片的浏览
- 使用ImageSwitcher和Gallery实现图片的浏览
- Android UI, Gallery(过时), ImageSwitcher, 简单图片浏览工具
- Android 使用ViewPager结合PhotoView开源组件实现网络图片在线浏览功能
- Gallery单独使用,Gallery和ImageSwitcher组件结合使用的例子
- Android 使用ViewPager结合PhotoView开源组件实现网络图片在线浏览功能
- 循环浏览图片的另一种方法—Gallery组件的使用
- 【Android】使用Gallery组件实现图片播放预览
- 使用ImageSwitcher实现点击按钮浏览图片功能
- 【Android基础知识】使用Gallery和ImageSwitcher实现图片轮播效果
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- 微信小程序图片轮播组件gallery slider使用方法详解
- Ubuntu下几个命令行方式使用的图片浏览工具
- (转载)Gallery 与ImageSwitcher的结合使用(ImageSwitcher中拖拽也实现图片的切换)
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- android中画廊视图Gallery和ImageSwitcher组件的使用
- 一个简单的相册GridView、Gallery的结合使用