您的位置:首页 > 移动开发 > Android开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: