图集
舉個栗子
xml
java
public class xx extends Activity { // 顯示圖片的資源 private static int[] images = {R.mipmap.wallpaper_x, xx}; Gallery gallery; ImageSwitcher is; @Override protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.xx);gallery = (Gallery) findViewById(R.id.gallery);is = (ImageSwitcher) findViewById(R.id.imageSwitcher);gallery.setAdapter(new ImageAdapter(this));// 讓選定的圖片在中心顯示gallery.setSelection(images.length / 2);// 為Gallery綁定監聽器;gallery.setOnItemSelectedListener(new OnItemSelectedListener() {public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {// 當在Gallery中選定一張圖片是 ImageSwitcher同步顯示同一張// position%images.length 為了讓圖片循環顯示is.setImageResource(images[position % images.length]);}public void onNothingSelected(AdapterView<?> parent) {}});is.setFactory(new ImageFactory(this));}private class ImageAdapter extends BaseAdapter {private Context context;public ImageAdapter(Context context) {this.context = context;}// 可以return images.lenght(),在這里返回Integer.MAX_VALUE// 是為了使圖片循環顯示public int getCount() {return images.length;}public Object getItem(int position) {return null;}public long getItemId(int position) {return 0;}public View getView(int position, View convertView, ViewGroup parent) {ImageView iv = new ImageView(context);iv.setScaleType(ImageView.ScaleType.CENTER_CROP);iv.setImageResource(images[position % images.length]);iv.setLayoutParams(new Gallery.LayoutParams(250, 350));iv.setAdjustViewBounds(true);return iv;} }private class ImageFactory implements ViewFactory {private Context context;public ImageFactory(Context context) {this.context = context;}public View makeView() {ImageView iv = new ImageView(context);iv.setLayoutParams(new ImageSwitcher.LayoutParams(-1, -1));return iv;} }復制代碼}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: struct 类型指针技巧
- 下一篇: fitnesse - 框架介绍