xml布局

 <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:orientation="vertical"
tools:context="com.example.android.gallery.MainActivity" > <Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageSwitcher
android:id="@+id/is"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>

自定义适配器:

 package com.example.android.gallery;

 import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType; public class MyAdapter extends BaseAdapter {
private int[] res;
private Context context; public MyAdapter(int[] res, Context context) {
this.res = res;
this.context = context;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;//设置数量为整型的最大值,使图片循环播放
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return res[position];
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(context);
image.setBackgroundResource(res[position%res.length]);//设置背景图片无限循环
image.setLayoutParams(new Gallery.LayoutParams(400, 300));//设置缩略图的大小
image.setScaleType(ScaleType.FIT_XY);//设置按比例缩放
return image;
} }

MainActivity类:

 package com.example.android.gallery;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
/**
* ImageSwitcher:和ImageView的功能类似,都适用与显示图片,
* 区别:ImageSwitcher的效果更炫,它可以指定图片切换的动画效果
*
* ImageSwticher可以粗略理解为ImageView的选择器,需要设置ViewFactory,
* 一般来说,会把ViewFactory的makeView()方法,返回ImageView。
* @author Administrator
*
*/
public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{
//准备数据源
private int[] res = {R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,
R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,
R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12};
private Gallery gallery;
private ImageSwitcher is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
//自定义适配器
MyAdapter adapter = new MyAdapter(res,this);
//为gallery设置适配器
gallery.setAdapter(adapter);
//为gallery设置监听器,用来监听Gallery选中的图片
gallery.setOnItemSelectedListener(this); is = (ImageSwitcher) findViewById(R.id.is);
is.setFactory(this);//为imageSwitcher设置视图工厂
//为imageSwitcher设置动画效果
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView image = new ImageView(this);
image.setScaleType(ScaleType.FIT_CENTER);//使图片按比例缩放并居中
return image;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
is.setBackgroundResource(res[position%res.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}

Android_Gallery的更多相关文章

  1. Android_Gallery(画廊)

    转:http://blog.csdn.net/tianjf0514/article/details/7521398 Gallery是画廊的意思,可以实现图片的浏览功能. 主要内容 Gallery控件的 ...

  2. android 基本控件使用

    http://tech.it168.com/a2012/0321/1327/000001327704.shtml Android_ListView_用代码控制ListView的位置 有三种方法 mli ...

随机推荐

  1. HDU 1074 Doing Homework 状压DP

    由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...

  2. application/x-www-form-urlencoded等字符编码的解释说明

    关于application/x-www-form-urlencoded等字符编码的解释说明 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏 ...

  3. MSP430 flash的操作

    今天顺便研究了一下msp430的flash操作,很多人也许看了我的博客,会发现网站上有很多的人总结得比我要好,这点我承认,因为自己能力有限,但是,从这篇博客起,我会参照以前大神们写的博客,添加大神们写 ...

  4. 相当管用了 mstha插件

    http://www.i-magical.com/2010/04/feizhuliu-kill-virus-mshta-exe/ 非主流杀毒 – mshta.exe Vincent | Apr 23, ...

  5. [读书笔记]了不起的node.js(二)

    这周做项目做得比较散(应该说一直都是这样),总结就依据不同情境双开吧-这篇记录的是关于node的学习总结,而下一篇是做项目学到的web前端的知识. 1.HTTP篇 node的HTTP模块在第一篇时接触 ...

  6. ubuntukylin提取root权限及mongoDB部署

    UbuntuKylin下安装Mongodb (参照UbuntuKylin下安装Mongodb一文安装成功后的心得) 1.官网下载安装包  http://www.mongodb.org/dr/fastd ...

  7. POJ 3678--Katu Puzzle(2-SAT)

    感觉这题比较裸,表现出了2-sat的本质. 不过构图我想的还是太简单了,a&b=1我只连了 a1->b1,b1->a1,但其实是不对的.这样连,a0和b0可以同时选到.应该连a0- ...

  8. JS 计算日期天数差

    function dayDiffer(startDate,endDate){ console.info((endDate.getTime - startDate.getTime())/(24*60*6 ...

  9. hdoj 2045 不容易系列之(3)—— LELE的RPG难题

    不容易系列之(3)—— LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  10. oscgit

    Gitlab PaaS项目演示 git config --global user.name "你的名字或昵称" git config --global user.email &qu ...