package com.test;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryMainActivity extends Activity implements
OnItemSelectedListener, ViewFactory {
/** Called when the activity is first created. */
private ImageSwitcher imageSwitcher;
private Context mContext;
// 图片数组
private Integer[] mImageIds = {
R.drawable.g1, R.drawable.g2,
R.drawable.g3, R.drawable.g4,
R.drawable.g5, R.drawable.g6,
R.drawable.g7, R.drawable.g8,
R.drawable.g9 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView iv = (ImageView) findViewById(R.id.ImageView01);
Gallery g = (Gallery) findViewById(R.id.Gallery01);
ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// 设置图片匹适配器
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// 显示点击的是第几张图片
Toast.makeText(GalleryMainActivity.this, "" + position%mImageIds.length,
Toast.LENGTH_LONG).show();
// 设置背景部分的ImageView显示当前Item的图片
iv.setImageResource(((ImageView) view).getId());
}
});
imageSwitcher.setFactory(this);
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
// 上下文对象
// 构造方法
public ImageAdapter(Context c) {
mContext = c;
// 读取styleable资源
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
@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 position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(mContext);
iv.setImageResource(mImageIds[position % mImageIds.length]);
// 给生成的ImageView设置Id,不设置的话Id都是-1
iv.setId(mImageIds[position % mImageIds.length]);
iv.setLayoutParams(new Gallery.LayoutParams(120, 160));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setBackgroundResource(mGalleryItemBackground);
return iv;
}
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(mImageIds[position % mImageIds.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Gallery.rar (45个子文件)
Gallery
AndroidManifest.xml 732B
res
layout
main.xml 984B
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
g1.png 24KB
g2.png 27KB
g6.jpg 27KB
g7.png 6KB
g4.png 56KB
g3.png 19KB
g8.png 13KB
g5.png 25KB
ic_launcher.png 3KB
g9.jpg 49KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 188B
attrs.xml 213B
proguard.cfg 1KB
src
com
test
GalleryMainActivity.java 4KB
bin
classes
com
test
GalleryMainActivity$ImageAdapter.class 2KB
GalleryMainActivity.class 4KB
R$styleable.class 535B
GalleryMainActivity$1.class 2KB
R.class 507B
R$layout.class 364B
R$attr.class 310B
R$id.class 466B
R$string.class 397B
R$drawable.class 611B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
g1.png 20KB
g2.png 23KB
g7.png 6KB
g4.png 39KB
g3.png 16KB
g8.png 12KB
g5.png 20KB
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
classes.dex 7KB
Gallery.apk 230KB
resources.ap_ 224KB
.classpath 364B
assets
project.properties 361B
.project 843B
gen
com
test
R.java 2KB
共 45 条
- 1
一品侍寝侍卫
- 粉丝: 2
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页