【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,如需转载请自行联系原作者
目录
相关文章
|
3天前
|
Android开发
Android中Glide加载Https图片失败的解决方案
Android中Glide加载Https图片失败的解决方案
11 1
|
5月前
|
XML JSON Java
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
85 0
|
1月前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
28 1
|
5月前
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
57 0
|
5月前
|
Android开发
[Android]制作9-Patch图片
[Android]制作9-Patch图片
43 0
|
5月前
|
XML JSON Android开发
Android App开发实战项目之给用户推荐旅游信息图片(附源码 简单易懂)
Android App开发实战项目之给用户推荐旅游信息图片(附源码 简单易懂)
50 0
|
5月前
|
XML 缓存 Java
Android App开发之利用Glide实现图片的三级缓存Cache讲解及实战(附源码 超详细必看 简单易懂)
Android App开发之利用Glide实现图片的三级缓存Cache讲解及实战(附源码 超详细必看 简单易懂)
135 0
|
6天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
9天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
10天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。