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 ...
随机推荐
- 【转】ubuntu 编码 UTF-8 GBK GB18030
添加编码支持 sudo locale-gen zh_CN.GBK sudo locale-gen zh_CN.GB2312 sudo locale-gen zh_CN.GB18030 2.更新一下lo ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- 设计模式_Interpreter_解释器模式
形象例子: 俺有一个<泡MM真经>,上面有各种泡MM的攻略,比如说去吃西餐的步骤.去看电影的方法等等,跟MM约会时,只要做一个Interpreter,照着上面的脚本执行就可以了.解释器模式 ...
- CCF 认证
题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...
- 3.1 全局存储带宽与合并访问 -- Global Memory(DRAM) bandwidth and memory coalesce
全局存储带宽(DRAM) 全局内存是动态随机访问的方式访问内存.我们希望访问DRAM的时候非常快,实际情况是DRAM中出来的数据非常非常慢,这就好比,理想状态是泄洪,水倾巢而出,气势宏伟,实际取水却像 ...
- linux下ssh使用rsa验证登陆MACOX
由于项目的需求,我这边ubuntu下常常需要SSH访问另外一台MACOS. 每次输入密码有点烦,就想到RSA公钥和密钥验证的方法. 像所有教程上讲的一样,本机执行 gong@hzsx:~$ ssh-k ...
- Codeforces Round #105 (Div. 2) ABCDE
A. Insomnia cure 哎 只能说英语太差,一眼题我看了三分钟. 题意:给5个数k, l, m, n 和 d,求1~d中能被k, l, m, n 至少一个整除的数的个数. 题解:…… 代码: ...
- iOS: ARC和非ARC下使用Block属性的问题
1. Block的声明和线程安全 Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非AR ...
- C#模仿360安全卫士玻璃按钮,不闪烁,背景切换效率快
首先先上效果图: 1.准备两张透明的png图片(尺寸74 x 82),一张用于鼠标进入控件时显示,一张用于鼠标单击控件时显示 2.拖一个GlassButton按钮 3.设置按钮属性 this.btnE ...
- ng-cookie 的基本使用
2.angular-cookie - 配置$cookiesProvider ```angular.module("Demo",[]).config(["$cookiesP ...