前几天,听说室友的老师要求他们做一个图片效果。其效果如下图所示(可左右滑动切换图片):

我当时晃眼一看,第一感觉好高级的样子。我还没做过这种效果呢,但室友说他们同学已经有人做出来了,我觉得既然有人做出来了,那么我也应该做出来,于是上个星期五的时候在教室研究了一下午,最后在网上找到了两个Demo都与这个效果类似但又不完全一样,果断下载下来研究研究,结果才发现其实这个效果并没有你想的那么难。

废话不多说,上代码:

main.xml:(其实就是两个控件的使用ImageSwitcher和Gallery)

<?xml version="1.0" encoding="utf-8"?>

<!-- 相对布局 -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 单个图片显示 -->
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<!-- 显示图片列表控件 -->
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dip" >
</Gallery> </RelativeLayout>

创建一个新的JAVA文件galleryImage.java(并在清单文件中设置为默认):

public class galleryImage extends Activity {
private Gallery gallery;
ImageSwitcher imageSwitcher; // 声明ImageSwitcher对象,图片显示区域
public List<Map<String, Object>> list;
private int[] myImageIds = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
R.drawable.i4 }; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Init(); } /**
* 初始化
*/
public void Init(){
// 通过控件的ID获得imageSwitcher的对象
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
// 设置自定义的图片显示工厂类
imageSwitcher.setFactory(new MyViewFactory(this)); Animation();
/*adapter = new SwitchereAdapter(this);
adapter.createReflectedImages(); // 创建倒影效果
gallery.setAdapter(adapter);*/
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 通过求余数,循环显示图片 imageSwitcher.setImageResource(myImageIds[position
% myImageIds.length]);
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub } });
} public void ChangeScale(){ } /**
* 动画效果
*/
public void Animation() {
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
} /**
* Gallery的图片适配器
* @author AF74776
*
*/
public class ImageAdapter extends BaseAdapter {
// 声明一个变量
int mGalleryItemBackGround;
private Context mContext; public ImageAdapter(Context c) {
mContext = c;
// 实用布局文件中的Gallery属性
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
// 取得gallery属性饿index id
mGalleryItemBackGround = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
// 让对象的styleable属性能反复实用
a.recycle();
} public int getCount() {// 返回数组长度
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {// 得到图片ID
return position;
} public View getView(int position, View converView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
// i.setImageResource(myImageIds[position %myImageIds.length ]);
i.setImageResource(myImageIds[position % myImageIds.length]);
i.setAdjustViewBounds(true); // 图片自动调整显示
i.setScaleType(ImageView.ScaleType.FIT_XY);// 重新设置图片的宽高
i.setLayoutParams(new Gallery.LayoutParams(200, 200));// 设置layout的宽高
i.setBackgroundResource(mGalleryItemBackGround);// 设置背景
return i;// 返回对象
} } // 自定义图片显示工厂类,继承ViewFactory class MyViewFactory implements ViewFactory { private Context context; // 定义上下文 // 参数为上下文的构造方法 public MyViewFactory(Context context) { this.context = context; } // 显示图标区域 public View makeView() { ImageView iv = new ImageView(context); // 创建ImageView对象 iv.setScaleType(ImageView.ScaleType.FIT_CENTER); // 图片自动居中显示 // 设置图片的宽和高 iv.setLayoutParams(new ImageSwitcher.LayoutParams(
400, 400)); return iv; // 返回ImageView对象 }
}
}

代码中有详细的注释,我觉得我都能看懂(自认为自己比较笨),相信大家也一定能看懂。

Android ImageSwitcher和Gallery的使用的更多相关文章

  1. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

  2. Android -- ImageSwitch和Gallery 混合使用

    1. 实现效果

  3. android ImageSwitcher

    <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...

  4. Android学习笔记 Gallery图库组件的使用

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  5. Android学习之Gallery

    在Android中,画廊控件Gallery用来显示图片列表,可以用手指直接拖动图片左右移动.Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到显示到最 ...

  6. 备忘-Android ViewPager 与Gallery滑动冲突解决方法

    解决方法,重新定义gallery,禁止触发pager的触摸事件 1 public class UserGallery extends Gallery implements OnGestureListe ...

  7. Android源代码之Gallery专题研究(1)

    前言 时光飞逝,从事Android系统开发已经两年了,总想写点什么来安慰自己.思考了非常久总是无法下笔,认为没什么好写的.如今最终决定写一些符合大多数人需求的东西,想必使用过Android手机的人们一 ...

  8. Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题

    最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...

  9. Android基础TOP6_2:Gallery +Image完成画廊

    Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

随机推荐

  1. (转载)腾讯CMEM的PHP扩展

    (转载)http://blog.renren.com/share/223341289/7693783476 题外话 最近公司在做相关的业务,由于Memcached协议缺少返回码,为了保证业务数据的安全 ...

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.3

    (1). Let $\sed{A_\al}$ be a family of mutually commuting operators. Then, there exists a common Schu ...

  3. ActiveMQ中的安全机制 [转]

    本文简单介绍ActiveMQ通过JAAS实现的安全机制.JAAS(Java Authentication and Authorization Service)也就是java认证/授权服务.这是两种不同 ...

  4. Cocos2d-x ios 下http请求的另一种实现

    简单描述下需求:游戏要加入事件log,比如玩家升到10级:创建角色:或是,触发这些事件后要求客户端忘后台抛送一条log信息.一般情况下,我们可以直接使用cocos自带的HttpClient(底层用li ...

  5. IT小说

    最近迷上了IT小说,连着读了好几个连载.伴随着一个项目的一些事,一些矛盾,也能体现出一个社会的缩影.最吸引的应该是一种熟悉感,常常想要是拍成电视剧也应该很好看,像<半泽植树>似的.看完了, ...

  6. ubuntu下arm-linux-gcc安装

    我下载的地址随便找的,4.4.3版本的,地址:http://www.cr173.com/soft/42654.html#address 1.我放在了/work/tools/ 2.sudo tar  x ...

  7. Android SharedPreference 数据存储

    参考:http://www.cnblogs.com/friends-wf/p/4835818.html 应用开发过程中,数据存储几乎是肯定会遇到的问题,根据要存储的数据类型和数量,可以选择合适的存储方 ...

  8. httpclient在抓取网页时出现速度慢的情况分析

    问题: 最近在使用httpclient3.0 来做项目,在一台机器部署系统后,发现此机器比其它机器在抓取页面的速度上慢了大概4s,左右. 项目是部署在局域内网,所以代码中都是直接写的IP地址 .在使用 ...

  9. centos svn快速搭建

    搭建SVN服务,有效的管理代码,以下三步可以快速搞定. 1.安装 #yum install subversion 判断是否安装成功 [root@]# svnserve --version 有了SVN软 ...

  10. Fastjson反序列化泛型类型时候的一个问题

    import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.u ...