Android_Gallery
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的更多相关文章
- Android_Gallery(画廊)
转:http://blog.csdn.net/tianjf0514/article/details/7521398 Gallery是画廊的意思,可以实现图片的浏览功能. 主要内容 Gallery控件的 ...
- android 基本控件使用
http://tech.it168.com/a2012/0321/1327/000001327704.shtml Android_ListView_用代码控制ListView的位置 有三种方法 mli ...
随机推荐
- javascript设计模式7
链式调用 (function(){ function _$(els){ //... } _$.prototype={ each:function(fn){ for(var i=0,len=this.e ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- 浅谈 html- table换行
这么久都没有来发表点总结了,看了园里的盆友发表的文章中,我发现自己也长进了不少. 但是,最近两天遇见了一个比较棘手的问题,就是在做web页面时,我用了一个table,这个页面是要供手机端调用的,所以在 ...
- stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】
关于堆和栈已经是程序员的一个月经话题,大部分有是基于os层来聊的. 那么,在赤裸裸的单片机下的堆和栈是什么样的分布呢?以下是网摘: 刚接手STM32时,你只编写一个 int main() ...
- 射频识别技术漫谈(9)——动物标签HDX【worldsing笔记】
半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...
- (莱昂氏unix源代码分析导读-50)LP11行式打印机
by cszhao1980 LP11有两个设备寄存器:状态寄存器(lpsr)和数据缓冲寄存器(lpbuf),可通过以下结构进行访问: 8812: #define LPADDR 0177514 8823 ...
- jquery完成带单选按钮的表格行高亮显示
jquery完成带单选按钮的表格行高亮显示 上篇博客写的是复选框的,这次写的是单选框的,有时查询的时候,只能选择一条记录,如果将选中的这条记录的行高亮显示,同时该行的单选按钮也被选中了,这样会提高用户 ...
- 关于七牛云存储,HTTPS资源上传不成功问题
关于七牛云存储,HTTPS资源上传不成功问题 官方给出了一个解决方案,亲测可用.特此记录一下. 找到QNConfiguration.m文件.然后重写两个方法,直接上代码. + (instancetyp ...
- 【Away3D代码解读】(一):主要类及说明
在深入解读Away3D的代码之前,需要对其有个大概的认识.本节主要列出Away3D中常用的类,并附上说明: View3D: Away3D的入口类,即创建该类就会初始化一个可以使用GPU呈现3D的对象, ...
- Stage3D学习笔记(四):正交矩阵
我们上一章节显示图片的时候,会发现我们制定的顶点在Stage3D中其实是存在一个区间的: x轴(从左到右):[-1.0-1.0] y轴(从下到上):[-1.0-1.0] z轴(从近到远):[0-1.0 ...