1.介绍
通过Gallery和ImageSwitcher结合的结果,上面的是ImageSwitcher,下面的是Gallery,通过滑动Gallery内的图片或单击图片,即可带动ImageSwitcher图片的跳转!


2.效果图

3.XML文件

<?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/imageSwitcher"        android:layout_width="wrap_content" android:layout_height="wrap_content"        android:layout_centerInParent="true" />    <Gallery android:id="@+id/gallery" android:background="#55000000"        android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_alignParentBottom="true" android:spacing="10dp" /></RelativeLayout>

4.Java代码

package wei.ye.g1;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;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.ViewSwitcher.ViewFactory;public class Hualang extends Activity {    // 显示图片的资源    private static int[] images = { R.drawable.baos, R.drawable.caoc,            R.drawable.chenyj, R.drawable.chenyy, R.drawable.gouj,            R.drawable.guany, R.drawable.hanx, R.drawable.lp, R.drawable.liub,            R.drawable.qinq, R.drawable.tiemz, R.drawable.wus,            R.drawable.xiangy, R.drawable.yuef, R.drawable.zhaoky,            R.drawable.zhugl, R.drawable.xis, R.drawable.yingz };    Gallery gallery;    ImageSwitcher is;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.hualang);        setTitle("这是Gallery和ImageSwitcher的效果");        gallery = (Gallery) findViewById(R.id.gallery);        is = (ImageSwitcher) findViewById(R.id.imageSwitcher);        gallery.setAdapter(new ImageAdapter(this));        // 让选定的图片在中心显示        gallery.setSelection(images.length / 2);        // 为Gallery绑定监听器;        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {            public void onItemSelected(AdapterView<?> parent, View view,                    int position, long id) {                // 当在Gallery中选定一张图片是 ImageSwitcher同步显示同一张                // position%images.length 为了让图片循环显示                is.setImageResource(images[position % images.length]);            }            public void onNothingSelected(AdapterView<?> parent) {            }        });        is.setFactory(new ImageFactory(this));    }    private class ImageAdapter extends BaseAdapter {        private Context context;        public ImageAdapter(Context context) {            this.context = context;        }        // 可以return images.lenght(),在这里返回Integer.MAX_VALUE        // 是为了使图片循环显示        public int getCount() {            return Integer.MAX_VALUE;        }        public Object getItem(int position) {            return null;        }        public long getItemId(int position) {            return 0;        }        public View getView(int position, View convertView, ViewGroup parent) {            ImageView iv = new ImageView(context);            iv.setImageResource(images[position % images.length]);            iv.setLayoutParams(new Gallery.LayoutParams(90, 90));            iv.setAdjustViewBounds(true);            return iv;        }    }    private class ImageFactory implements ViewFactory {        private Context context;        public ImageFactory(Context context) {            this.context = context;        }        public View makeView() {            ImageView iv = new ImageView(context);            iv.setLayoutParams(new ImageSwitcher.LayoutParams(200, 200));            return iv;        }    }}

PS:图片是读取drawable文件里的,不是SD卡里的

更多相关文章

  1. Android(安卓)RecyclerView+StaggeredGridLayoutManager实现瀑布
  2. Android之取消ViewPage+Fragment的预加载
  3. Android_API_临时翻译
  4. android 实现全屏代码
  5. Android通过手势实现翻页效果
  6. Android(安卓)手机卫士--参照文档编写选择器
  7. android中AlertDialog设置圆角
  8. android 在电脑上显示真机屏幕
  9. Android(安卓)获取图库中图片的坑

随机推荐

  1. ubuntu10.10下编译android内核源码
  2. android客户端程序访问服务器端webservic
  3. Android(安卓)Studio 1.5 opencv开发
  4. First day of android study
  5. Android(安卓)View和ViewGroup的关系
  6. Android构建工具Gradle知识1
  7. android 系统级 bug audio latency
  8. Android(安卓)Java执行Shell命令
  9. android 界面布局 很好的一篇总结 【转】
  10. Android(安卓)USB/MTP相关实现