android Gallery与ImageSwitcher结合实例应用总结
2012-08-10 10:42
295 查看
Activity类:
public class Main extends Activity implements ViewFactory, OnItemSelectedListener {
private Display display;
int width;
int height;
private Gallery gallery;
private ImageSwitcher imswitcherLayout;
private ImageAdapter imageAdapter;
//动态获取屏幕大小
private void getWindows() {
display=getWindowManager().getDefaultDisplay();
width=display.getWidth();
height=display.getHeight();
}
@SuppressLint("ParserError")
private int [] imageList=new int []{
R.drawable.cn505100back,R.drawable.cn505100positive,
R.drawable.cntypebg,R.drawable.gatbg,
R.drawable.hkbg,R.drawable.indexbt1,
R.drawable.waibg
};
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mcontext;
public ImageAdapter(Context context) {
this.mcontext = context;
//
获得Gallery组件的属性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
public int getCount() {
//改进1返回一个很大的值,如:inter.Max_VALUES
//return imageList.length;
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//显示gallery图片方法
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imv=new ImageView(mcontext);
//
第2点改进,通过取余来循环取得resIds数组中的图像资源ID
//imv.setImageResource(imageList[position]);
imv.setImageResource(imageList[position%imageList.length]);
imv.setLayoutParams(new Gallery.LayoutParams((int)(width*0.1),(int)(height*0.1)));
imv.setBackgroundResource(mGalleryItemBackground);
return imv;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindows();
//Gallery
gallery=new Gallery(this);
ImageAdapter imageAdapter=new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener( this);
//ImageSwitcher
imswitcherLayout=new ImageSwitcher(this);
imswitcherLayout.setFactory(this);
LinearLayout.LayoutParams
imageSwitcherlp=new LinearLayout.LayoutParams((int)(width*1),(int)(height*0.6));
imswitcherLayout.setLayoutParams(imageSwitcherlp);
//设置LinearLayout
LinearLayout galleryLayout=new LinearLayout(this);
//设置gallerylayout属性
LinearLayout.LayoutParams gallerylp=new LinearLayout.LayoutParams((int)(width*0.1),(int)(height*0.1));
galleryLayout.setOrientation(LinearLayout.VERTICAL);
galleryLayout.setLayoutParams(gallerylp);
//添加imswitcher、galleryLayout到布局文件
galleryLayout.addView(imswitcherLayout);
galleryLayout.addView(gallery);
setContentView(galleryLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//以上部分为Gallery方法
//一下为ImageSwitcher方法
public void onItemSelected(AdapterView<?> arg0, View view, int positon,
long arg3) {
imswitcherLayout.setImageResource(imageList[positon%imageList.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
//显示ImageSwitcher中的图片
public View makeView() {
ImageView imv=new ImageView(this);
imv.setLayoutParams(new ImageSwitcher.LayoutParams((int)(width*1),(int)(height*0.6)));
imv.setBackgroundColor(0xFF000000);
return imv;
}
}
Xml:
Values/attrs.xml
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<declare-styleable
name="Gallery">
<attr
name="android:galleryItemBackground"
/>
</declare-styleable>
</resources>
public class Main extends Activity implements ViewFactory, OnItemSelectedListener {
private Display display;
int width;
int height;
private Gallery gallery;
private ImageSwitcher imswitcherLayout;
private ImageAdapter imageAdapter;
//动态获取屏幕大小
private void getWindows() {
display=getWindowManager().getDefaultDisplay();
width=display.getWidth();
height=display.getHeight();
}
@SuppressLint("ParserError")
private int [] imageList=new int []{
R.drawable.cn505100back,R.drawable.cn505100positive,
R.drawable.cntypebg,R.drawable.gatbg,
R.drawable.hkbg,R.drawable.indexbt1,
R.drawable.waibg
};
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mcontext;
public ImageAdapter(Context context) {
this.mcontext = context;
//
获得Gallery组件的属性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
public int getCount() {
//改进1返回一个很大的值,如:inter.Max_VALUES
//return imageList.length;
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//显示gallery图片方法
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imv=new ImageView(mcontext);
//
第2点改进,通过取余来循环取得resIds数组中的图像资源ID
//imv.setImageResource(imageList[position]);
imv.setImageResource(imageList[position%imageList.length]);
imv.setLayoutParams(new Gallery.LayoutParams((int)(width*0.1),(int)(height*0.1)));
imv.setBackgroundResource(mGalleryItemBackground);
return imv;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindows();
//Gallery
gallery=new Gallery(this);
ImageAdapter imageAdapter=new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener( this);
//ImageSwitcher
imswitcherLayout=new ImageSwitcher(this);
imswitcherLayout.setFactory(this);
LinearLayout.LayoutParams
imageSwitcherlp=new LinearLayout.LayoutParams((int)(width*1),(int)(height*0.6));
imswitcherLayout.setLayoutParams(imageSwitcherlp);
//设置LinearLayout
LinearLayout galleryLayout=new LinearLayout(this);
//设置gallerylayout属性
LinearLayout.LayoutParams gallerylp=new LinearLayout.LayoutParams((int)(width*0.1),(int)(height*0.1));
galleryLayout.setOrientation(LinearLayout.VERTICAL);
galleryLayout.setLayoutParams(gallerylp);
//添加imswitcher、galleryLayout到布局文件
galleryLayout.addView(imswitcherLayout);
galleryLayout.addView(gallery);
setContentView(galleryLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//以上部分为Gallery方法
//一下为ImageSwitcher方法
public void onItemSelected(AdapterView<?> arg0, View view, int positon,
long arg3) {
imswitcherLayout.setImageResource(imageList[positon%imageList.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
//显示ImageSwitcher中的图片
public View makeView() {
ImageView imv=new ImageView(this);
imv.setLayoutParams(new ImageSwitcher.LayoutParams((int)(width*1),(int)(height*0.6)));
imv.setBackgroundColor(0xFF000000);
return imv;
}
}
Xml:
Values/attrs.xml
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<declare-styleable
name="Gallery">
<attr
name="android:galleryItemBackground"
/>
</declare-styleable>
</resources>
相关文章推荐
- Spring + mybatis整合方案总结 结合实例应用
- Android:图像切换器imageSwitcher的实例应用
- (简单图片浏览器)imageSwitcher与gallery结合应用
- Spring + mybatis整合方案总结 结合实例应用
- Spring + mybatis整合方案总结 结合实例应用
- Spring + mybatis整合方案总结 结合实例应用
- Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- Gallery 与ImageSwitcher的结合使用
- C#反射之基础应用实例总结
- jstl标签集合及应用实例总结
- ImageSwitcher结合Gallery展示SD卡中的资源图片
- 轻量级验证码生成插件webutil-licenseImage源码与实例应用
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- Java集合框架的知识总结(1) 说明:先从整体介绍了Java集合框架包含的接口和类,然后总结了集合框架中的一些基本知识和关键点,并结合实例进行简单分析。 1、综述 所有集合类
- C# Windows 桌面应用只运行一个实例(总结)
- 一个tomcat部署多个应用实例总结
- tuple list 结构结合record的应用实例
- Gallery 与ImageSwitcher的结合使用
- FIELD MODULE结合CHAIN在SCREEN中的应用实例