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. 使用 Apache MINA2 实现 Web 系统的消息中间件

    本文将介绍如何使用 Apache MINA2(以下简称 MINA2)解决复杂 Web 系统内各子系统之间同步消息中间件的问题.MINA2 为开发高性能和高可用性的网络应用程序提供了非常便利的框架.从本 ...

  2. Initializing nested object properties z

    public class Employee { public Employee() { this.Insurance = new Insurance(); } // Perhaps another c ...

  3. VirtualBox故障一例

    早上的测试环境,估计是任务太重了吧,在点击VirtualBox的快速休眠后,就没有响应了,查看日志,内容都是: aComponent={Console} aText={The virtual mach ...

  4. Bzoj 2006: [NOI2010]超级钢琴 堆,ST表

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2222  Solved: 1082[Submit][Statu ...

  5. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  6. Apache Hadoop 源码阅读

    总之一句话,这些都是hadoop-2.2.0的源代码里有的.也就是不光只是懂理论,编程最重要,还是基本功要扎实啊.... 在hadoop-2.2.0的源码里,按Ctrl + Shift + T . 跳 ...

  7. A Tour of Go Range continued

    You can skip the index or value by assigning to _. If you only want the index, drop the ", valu ...

  8. PHOTOSHOP 中画笔工具和铅笔工具的一个小小差别

    今天在作图的时候偶然发现的一个以前并没有在意画笔和铅笔的小小区别,情况是这样的,我在做图像处理,需要一个单像素的闭合曲线灰度图来做实验,然后用画笔工具把直径调到1之后去作图,放大之后发现,跟预想的结果 ...

  9. 两个简单方法加速DataGridView

    两个简单方法加速DataGridView (2009-03-24 16:57:13) 转载▼ 标签: 杂谈 分类: .NET DataGridView虽然好用,但是如果数据量比较大的话就会出现性能的问 ...

  10. 【转】Netty那点事(二)Netty中的buffer

    [原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch2-buffer.md 上一篇文章我们概要介绍了Netty的原 ...