【Android Demo】图片之画廊效果(Gallery Switcher)

简介:

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卡里的






本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/archive/2012/04/12/2444263.html,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
54 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
12天前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
26天前
|
存储 缓存 编解码
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
46 20
Android经典面试题之图片Bitmap怎么做优化
|
2月前
|
数据处理 开发工具 数据安全/隐私保护
Android平台RTMP推送|轻量级RTSP服务|GB28181接入之文字、png图片水印的精进之路
本文探讨了Android平台上推流模块中添加文字与PNG水印的技术演进。自2015年起,为了满足应急指挥及安防领域的需求,逐步发展出三代水印技术:第一代为静态文字与图像水印;第二代实现了动态更新水印内容的能力,例如实时位置与时间信息;至第三代,则优化了数据传输效率,直接使用Bitmap对象传递水印数据至JNI层,减少了内存拷贝次数。这些迭代不仅提升了用户体验和技术效率,也体现了开发者追求极致与不断创新的精神。
|
2月前
|
自然语言处理 定位技术 API
Android经典实战之如何获取图片的经纬度以及如何根据经纬度获取对应的地点名称
本文介绍如何在Android中从图片提取地理位置信息并转换为地址。首先利用`ExifInterface`获取图片内的经纬度,然后通过`Geocoder`将经纬度转为地址。注意操作需在子线程进行且考虑多语言支持。
151 4
|
2月前
|
XML 前端开发 Android开发
Android经典实战之Kotlin中实现圆角图片和圆形图片
本文介绍两种实现圆角图像视图的方法。第一种是通过自定义Kotlin `AppCompatImageView`,重写`onDraw`方法使用`Canvas`和`Path`进行圆角剪裁。第二种利用Android Material库中的`ShapeableImageView`,简单配置即可实现圆角效果。两种方法均易于实现且提供动态调整圆角半径的功能。
41 0
|
4月前
|
JSON 编解码 Apache
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
47 1
|
4月前
|
前端开发 Java API
Android系统中读写和显示图片
Android系统中读写和显示图片
37 0
|
3天前
|
XML 存储 Java
探索安卓开发之旅:从基础到进阶
【9月更文挑战第37天】安卓开发,一个充满无限可能的领域。它不仅关乎技术的深度与广度,更关乎开发者的成长与突破。本文将带你走进安卓开发的世界,从基础知识的学习到进阶技巧的掌握,一起感受编程的魅力与乐趣。
|
Android开发 数据格式 XML
Android异步加载图片详解之方式二(3)
main.xml如下:   listviewitem.xml如下:   ...
812 0