画廊视图使用Gallery表示,能够按水平方向显示内容,并且可以手指直接拖动图片和移动,一般用
来浏览图片,,被选中的选项位于中间,并且可以响应事件显示信息.在使用画廊视图时,首先在屏幕
上添加Gallery组件,通常使用标记在XML而布局文件中添加.

  

画廊视图在4.0后已经过期,但是我们仍然可以使用,不过后面我们将用水平的ListView
自定义组件来实现这个效果,在自定义视图中讲解。

常用属性:
1. android:animationDuration 用于设置列表项切换时的动画持续时间
2. android:gravity 用于设置对齐方式
3. android:spacing 用于设置列表项之间的间距
4. android:unselectedAlpha 用于设置没有选中的列表项的透明度

()

1、搭建布局

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_margin="30dp"
android:src="@drawable/ic_launcher" /> <Gallery
android:id="@+id/gallery"
android:layout_below="@id/img"
android:layout_margin="20dp"
android:spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

activity_main.xml

2、MainActivity.java中将所有组件找到,并设置监听

 public class MainActivity extends Activity implements OnItemSelectedListener {

     Gallery gallery;
ImageView img;
int[] imgId = new int[6]; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
MyBaseAdapter adapter = new MyBaseAdapter();
gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(this); } private void initView() {
for (int i = 0; i < imgId.length; i++) {
try {
imgId[i] = R.drawable.class.getField("img0"+(i+1)).getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} gallery = (Gallery) findViewById(R.id.gallery);
img = (ImageView) findViewById(R.id.img);
} class MyBaseAdapter extends BaseAdapter{ @Override
public int getCount() {
return imgId.length;
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = new ImageView(MainActivity.this);
}
ImageView iv = (ImageView) convertView; //设置数据
iv.setImageResource(imgId[position]); return convertView;
} } @Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) { img.setImageResource(imgId[position]); } @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub } }

MainActivity.java

Android BaseAdapter Gallery 画廊视图 (左右拖动图片列表拖至中间时图片放大显示)的更多相关文章

  1. android学习---Gallery画廊视图

    Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...

  2. Ant Design -- 图片可拖拽效果,图片跟随鼠标移动

    Ant Design 图片可拖拽效果,图片跟随鼠标移动,需计算鼠标在图片中与图片左上角的X轴的距离和鼠标在图片中与图片左上角的Y轴的距离. constructor(props) { super(pro ...

  3. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  4. android 32 Gallery:横着滚动的列表

    Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...

  5. jQuery图片列表拖拽排序布局

    在线演示 本地下载

  6. Android应用开发学习之画廊视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 画廊视图Gallery能按水平方向显示一组图片,并可以拖动图片.下面我们来看一个使用画廊视图的例子,其运行效果如下: ...

  7. <Android>列表、网格、画廊视图及适配器的绑定

    列表视图和适配器的绑定 列表视图既可以使用ListView组件,也可以继承ListActivity.显示可以是ArrayAdapter,也可以是游标SimpleCursorAdapter,还可以是继承 ...

  8. android学习日记17--Gallery(画廊视图)

    一.Gallery 1.简述 Gallery有道翻译为画廊,就叫它画廊控件,主要用来可切换的图片展示,左右滑动可以选择上一张或下一张图片. 个人感觉蛮实用的,不过google貌似不推荐使用啊! 2.常 ...

  9. 画廊视图(Gallery)的功能和用法

    Gallery与Spinner组件有共同的父类:AbsSpinner,表明Gallery和Spinner是同一个列表框.它们之间的区别是Spinner显示的垂直的列表选择框,而Gallery显示的是一 ...

随机推荐

  1. 该项目中不存在目标 precomputecompiletypescript The target "PreComputeCompileTypeScript" does not exist in the project

    Open Microsoft.TypeScript.targets file located under C:\Program Files (x86)\MSBuild\Microsoft\Visual ...

  2. XML约束图解

  3. NHibernate考察实例:简单映射

    建立一个Company类用来测试,对应的表为TBLCOMPANY.   1. 下载NHibernate(版本1.2.0.CR1),将NHibernate \bin\net-2.0下面的文件拷贝到lib ...

  4. 21 Merge Two Sorted Lists(两链表归并排序Easy)

    题目意思:对两个递增链表进行归并排序 思路:没什么好说的,二路归并 /** * Definition for singly-linked list. * struct ListNode { * int ...

  5. PHP应用程序的安全性

    无论在开发中,还是在面试时或者技术讨论时,安全性都是需要深入了解及掌握的. 目标 本教程目标是使您了解应该如何保护自己构建的 Web 应用程序.讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET ...

  6. centos git 升级应用

    在阿里云服务器上部署Git 升级centos 自带的Git yum库自带版本为git1.7.1-3.el6_4.1. -------------------升级-------------------- ...

  7. split和join和pop和remove用法

    简单字符串分割不需要import re,多字符串分割要导入re,多个字符串分割的分隔符要用 | 分开 >>> msg='chen hang wuhan keji daxue aljl ...

  8. The Managed Metadata Service or Connection is currently not available

    Does the following error message looks familiar to you?  when you go to site Actions -> Site Sett ...

  9. 搬瓦工搭建VPN

    搬瓦工VPS的PPTP VPN搭建其实和上面都是很简单的,这个要用到上面的搬瓦工VPS控制面板. Root shell - advanced 下载 centos 6 一键安装包 wget --no-c ...

  10. HTML 表格、区块、其他常用控件

    1. HTML 表格 主要关键字: table:表格: table border 属性:定义边框 -- <table border="1">: caption:表名: ...