zoukankan      html  css  js  c++  java
  • android学习---Gallery

    Gallery控件

      一个画廊视图,用于展示一组图片,用固定在中间位置的水平滚动列表显示列表项的视图。

      Gallery的常用XML属性:

    • android:animationDuration:设置列表项切换时的动画持续时间,使用毫秒为单位。
    • android:gravity:设置列表项的对其方式。
    • android:spacing:设置Gallery内列表项之间的间距。
    • android:unselectedAlpha:设置没有被选中的列表项的透明度,范围是一个为0~1的float数,越接近0越透明

      作为一个列表框,声明的事件是定义在AdapterView类中,常用事件有如下几个:

    • AdapterView.OnItemCLickListener:列表项被点击时触发。
    • AdapterView.OnItemLongClickListener:列表项被长按时触发。
    • AdapterView.OnItemSelectedListener:列表项被选择时触发

      Gallery数据绑定

      Gallery的Adapter适配器,虽然可以直接实现Adapter接口,但是一般推荐继承另外一个抽象类:BaseAdapter,它已经实现了一些常用的方法。对于BaseAdapter而言,继承它必须要实现几个方法,下面先了解一下这几个方法:

    • int getCount():当前适配器中存在多少个数据项。
    • Object getItem(int position):返回数据集中指定位置的数据项。
    • long getItemId(int position):返回数据集中指定位置的数据行Id。
    • View getView(int position,View convertView,ViewGroup parent):返回一个视图,用于显示数据集中指定位置的数据项。

      对于BaseAdapter的getView()方法,这里着重讲解一下。在绑定Adapter适配器之后,系统会根据getCount()方法返回的数据项,循环调用getView()方法,用于返回每个position位置的显示视图,所以对于一个Adapter适配器,其主要的代码量就在getView()方法中。因为BaseAdapter的getView()方法返回的是一个View数据,所以一般自定义的展示效果都会用到BaseAdapter来做父类,继承实现其中的方法来实现自定义的View来展示在UI界面上。

    布局代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Gallery Demo" >
        </TextView>
    
        <!-- 定义一个Gallery,其中动画步进3秒,数据项间隔2dp,透明度为50% -->
    
        <Gallery
            android:id="@+id/gallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:animationDuration="3000"
            android:spacing="2dp"
            android:unselectedAlpha="0.5" />
    
        <ImageView
            android:id="@+id/imageview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ImageView>
    
    </LinearLayout>

    实现代码:

    package com.leaf.android;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    
    public class Main extends Activity {
        /** Called when the activity is first created. */
    
        private Gallery gallery;
        private ImageAdapter imageAdapter;
        private ImageView imageView;
        // 声明图片的数组
        private int[] resIds = new int[] { R.drawable.car1, R.drawable.car2,
                R.drawable.car3, R.drawable.car4, R.drawable.car5, R.drawable.car6,
                R.drawable.car7, R.drawable.car8, R.drawable.car9,
                R.drawable.car10, };
    
        // android的适配器
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            gallery = (Gallery) this.findViewById(R.id.gallery);
            imageView = (ImageView) this.findViewById(R.id.imageview);
            gallery.setAdapter(new ImageAdapter(this));
    
            gallery.setOnItemClickListener(new OnItemClickListener() {
    
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    imageView.setImageResource(resIds[position]);
                }
            });
        }
    
        // 声明一个BaseAdapter
        public class ImageAdapter extends BaseAdapter {
    
            private Context context;// 使用Adapter的上下文变量
            int mGralleyItemBackground;// 使用简单的计数器,填充背景图片
    
            public ImageAdapter(Context context) {
                this.context = context;
                // 读取属性
                TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
                mGralleyItemBackground = typedArray.getResourceId(
                        R.styleable.Gallery_android_galleryItemBackground, 0);
                typedArray.recycle();
            }
    
            public int getCount() {
                // 返回数据集中数据的个数
                return resIds.length;
            }
    
            public Object getItem(int position) {
                // 返回数据集中当前podition位置的数据
                return resIds[position];
            }
    
            public long getItemId(int position) {
                // 返回数据集中当前podition位置的数据的Id
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                // 自定义的适配器,需要用自定义的布局来显示,通常android的通用布局是不能满足我们的需求
                // 可以手工创建一个view视图,也可以用inflate填充一个xml
                // 从数据源中根数position获得每一个item的值,填充到指定的xml布局中
                // View convertView 是一个旧的布局,如果没有新的布局填充的时候,将使用旧的布局
                // 当前的布局会被追加到父布局中
                if (convertView == null) {
                    imageView = new ImageView(context);
                    imageView.setImageResource(resIds[position]);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView.setLayoutParams(new Gallery.LayoutParams(160, 120));
                } else {
                    imageView = (ImageView) convertView;
                }
                imageView.setBackgroundResource(mGralleyItemBackground);
                return imageView;
            }
        }
    }

      在代码中用到了一个XML文件定义的Android样式,这里仅仅是为了用于Gallery展示图片的背景样式,并不是必须的。下面是样式的XML资源文件attrs.xml的代码,文件存放地址为res/values中。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="Gallery">
            <attr name="android:galleryItemBackground"></attr>
        </declare-styleable>
    
    </resources>
  • 相关阅读:
    树莓派 无线网卡配置
    树莓派.Net MVC4 Mono4 Jexus
    springcloud超简单的入门3--负载均衡
    springcloud超简单的入门2--Eureka服务治理
    SpringCloud超简单的入门(1)--一些简单的介绍
    Tomcat9控制台中文乱码的解决方案
    win10 调整音量时 左上角弹框 的解决办法
    .NETCore 添加Docker支持,并上传镜像至Docker Hub,最后在CentOs中拉取镜像运行
    假设每台交换机上行有N条线,两跳内,可以最多让多少个交换机互通?
    .netcore微服务-SkyWalking安装部署IIS
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3296936.html
Copyright © 2011-2022 走看看