• 第31讲 UI组件之 Gallery画廊控件


    第31讲 UI组件之 Gallery画廊控件

    1.Gallery的简介

    Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息。Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到最后一张图片为止。Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图片的效果。

    Gallery常用的XML属性

    属性名称

    描述

    android:animationDuration

    设置布局变化时动画的转换所需的时间(毫秒级)。仅在动画开始时计时。该值必须是整数,比如:100。

    android:gravity

    指定在对象的X和Y轴上如何放置内容。指定一下常量中的一个或多个(使用 “|”分割)

    Constant

    Value

    Description

    top

    0x30

    紧靠容器顶端,不改变其大小

    bottom

    0x50

    紧靠容器底部,不改变其大小

    left

    0x03

    紧靠容器左侧,不改变其大小

    right

    0x05

    紧靠容器右侧,不改变其大小

    center_vertical

    0x10

    垂直居中,不改变其大小

    fill_vertical

    0x70

    垂直方向上拉伸至充满容器

    center_horizontal

    0x01

    水平居中,不改变其大小

    Fill_horizontal

    0x07

    水平方向上拉伸使其充满容器

    center

    0x11

    居中对齐,不改变其大小

    fill

    0x77

    在水平和垂直方向上拉伸,使其充满容器

    clip_vertical

    0x80

    垂直剪切(当对象边缘超出容器的时候,将上下边缘超出的部分剪切掉)

    clip_horizontal

    0x08

    水平剪切(当对象边缘超出容器的时候,将左右边缘超出的部分剪切掉)

    android:spacing

    图片之间的间距

    android:unselectedAlpha

    设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。

    一、实现图片的左右滑动浏览效果。

    //Gallery的使用方法类似于其余的ViewGroup控件,使用adapter进行布局。

    Gallery gallery=(Gallery)findViewById(R.id.gallery1);

    //设置图片适配器

    gallery.setAdapter(new MyAdapter(image,this));

    //image:定义整型数组 即图片源  

    final int[] image=new int[] {R.drawable.attack , R.drawable .boy1 , R.drawable .boy2 , R.drawable .doupo };

    MyAdapter定义如下:

    private class MyAdapter extends BaseAdapter{

    private int[] image;

    private Context context;

    public MyAdapter(int[] image, Context context) {

    super();

    this.image = image;

    this.context = context;

    }

    public int getCount() { return image.length; } //获取图片的个数

    public Object getItem(int arg0) { return null; }

    public long getItemId(int arg0) { return 0; }

    public View getView(int arg0, View arg1, ViewGroup arg2) {

    ImageView imageView=new ImageView(context);

    imageView.setImageResource(image[arg0]); //给ImageView设置资源

    return imageView;

    }

    }

    二、实现缩略图加放大图显示

    对上述程序做修改:

    首先,将layout设置为上方为ImageView,下方为Gallery显示缩略图。

    之后,通过设置点击Gallery时设置图片到ImageView中。

    final ImageView imageView=(ImageView)findViewById(R.id.imageView1);

    Gallery gallery=(Gallery) findViewById(R.id.gallery1);

    //设置图片适配器

    gallery.setAdapter(new MyAdapter(image,this));

    //设置监听器 

    gallery.setOnItemClickListener(newOnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, intposition, long arg3) {

    imageView.setImageResource(image[position]);

    }

    });

    public View getView(int arg0, View arg1,ViewGroup arg2) {

    ImageView imageView=new ImageView(context);

    //设置布局图片120x120显示 

    imageView.setLayoutParams(new Gallery.LayoutParams(120, 120)); 

    imageView.setImageResource(image[arg0]); //给ImageView设置资源

    return imageView;

    }

  • 相关阅读:
    读写excel文件
    数据库操作
    django项目搭建
    django基础
    string
    random函数
    vue-typescript入门
    Visual Studio 2019配置vue项目
    js css+html实现简单的日历
    python接口自动化4-绕过验证码登录(cookie)
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171591.html
Copyright © 2020-2023  润新知