Gallery是一个可以拖动的列表,正中对应的是选中的东西.他和spinner有共同的父类:AbsSpinner

属性:

android:animationDuration="1000"   图片切换动画持续时间
android:spacing="8dp"            设置图片之间的间距
android:unselectedAlpha="0.6"      设置没有选择的图片的透明度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kale.gallery.MainActivity"
android:orientation="vertical"> <!--
android:animationDuration="1000" 图片切换动画持续时间
android:spacing="8dp" 设置图片之间的间距
android:unselectedAlpha="0.6" 设置没有选择的图片的透明度
-->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用ImageView作预览图"
android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView
android:id="@+id/imageView_id"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="下方是gallery"
android:textAppearance="?android:attr/textAppearanceMedium" /> <Gallery
android:id="@+id/gallery_id"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:animationDuration="1000"
android:spacing="8dp"
android:unselectedAlpha="0.3"/> </LinearLayout>

MainActivity.java

package com.kale.gallery;

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.Gallery.LayoutParams;
import android.widget.ImageView; @SuppressWarnings("deprecation")
public class MainActivity extends Activity { ImageView iV;
Gallery gallery; int[] imageIds = new int [] {
R.drawable.appstore,
R.drawable.cydia,
R.drawable.itunes,
R.drawable.mail,
R.drawable.safair
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); viewInit();
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO 自动生成的方法存根
iV.setImageResource(imageIds[arg2]); } @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO 自动生成的方法存根 }
});
} public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
} public int getCount() {
return imageIds.length;
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(imageIds[position]);
//设置缩放类型
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setAdjustViewBounds(true);
//设置布局参数
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
} private void viewInit() {
// TODO 自动生成的方法存根
gallery = (Gallery) findViewById(R.id.gallery_id);
iV = (ImageView)findViewById(R.id.imageView_id);
} }

Gallery和自定义Adapter配合使用,实现图片预览的更多相关文章

  1. 自定义type为file型input控件+该控件具有本地图片预览功能

    最近的一个项目需求是写一个type为filex型的input控件,这个控件: 第一,要自定义样式: 第二,要能直接在本地预览上传的图片: 第三,要能检测图片的尺寸是否符合要求. 故综合网上的资源写了下 ...

  2. dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

    http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...

  3. 实现QQ空间图片预览效果

    今天项目遇到需求 要求 实现图片预览效果 .  类似于扣扣空间那种,本人也到网上找过 代码量太大了   ,类多到处是注释看的有点恶心 .然后自己写了一个图片预览的效果,其实很简单的 . 首先我们来分析 ...

  4. angular中封装fancyBox(图片预览)

    首先在官网下载最新版的fancyBox(一定要去最新网站,以前依赖的jquery版本偏低),附上链接:http://fancyapps.com/fancybox/3/ 然后在项目中引用jquery,然 ...

  5. input[type='file']样式美化及实现图片预览

    前言 上传图片是常见的需求,多使用input标签.本文主要介绍 input标签的样式美化 和 实现图片预览. 用到的知识点有: 1.input标签的使用 2.filelist对象 和 file对象 3 ...

  6. 在 vue 中使用 vieiwer 图片预览插件

    https://blog.csdn.net/WestLonly/article/details/79801800?utm_source=blogxgwz0 首先,感谢原作者 官网链接 github地址 ...

  7. js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  8. DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

    DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库. 它是轻量级的,不依赖任何其他类库(如JQuery)并且高度可定制. 试试看! 将文件拖至此处或点击上传.(这仅仅是 dropzo ...

  9. Ionic实战三:Ionic 图片预览可放大缩小左右滑动demo-iClub图片预览

    这个demo的主要功能有两个,一个是首页的导航向上拉动会浮动在最上面的效果,另一个就是我们平时非常实用的功能,就是图片预览功能 点击可以放大图片,并且可以左右滑动,还可以双击放大缩小图片以及双手指控制 ...

随机推荐

  1. G 最短路

    题目描述三国时期,南蛮王孟获叛乱,诸葛亮起兵平乱.当深入南蛮之地时,遇当地人绘得地图,发现各地分别由各个寨主据守,若诸葛亮想兵分多路进军,尽快占领各个山寨(必须占领所有山寨),并且最终所有士兵都汇聚到 ...

  2. Ubuntu 18.04 上设置桌面程序开机自启动

    需求 在新安装的 Ubuntu 18.04 系统上,将有些不可描述的程序开机之后自动启动 Startup Applications Preferences 打开 Startup Application ...

  3. Redis高可用之主从复制实践(四)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

  4. JavaScript实现链式调用

    学习Jquery的时候,我们通常会看到链式调用的写法 $(window).addEvent('load', function(){ $('test').show().setStyle('color', ...

  5. nginx命令大全

    sudo nginx #打开 nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx nginx -t #测试配置是否有语法错误 n ...

  6. MVVM模式下关闭窗口的实现

    通过行为来实现 实现界面与逻辑的分离 窗口关闭行为:其中含有布尔型的Close属性,将相应的关闭行为绑定到该属性上,则可以实现窗口的关闭行为,从而实现VM与View的分离 public class W ...

  7. [POI2013]Łuk triumfalny

    [POI2013]Łuk triumfalny 题目大意: 一棵\(n(n\le3\times10^5)\)个结点的树,一开始\(1\)号结点为黑色.\(A\)与\(B\)进行游戏,每次\(B\)能选 ...

  8. KVM和QEMU简介

    KVM/QEMU简介 KVM虚拟机是基于linux内核虚拟化,自linux2.6.20之后就集成在linux的各个主要发行版本中.它使用linux自身的调度器进行管理,所以相对于xen,其核心源码很少 ...

  9. 【bzoj 1076】【SCOI2008】奖励关

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 891[Submit][Status ...

  10. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...