Android½çÃæ¿Ø¼þ ÏÂÔØ±¾ÎÄ

5. this.imageLayout.addView(imageView, new LinearLayout.LayoutParams(LinearLayou

t.LayoutParams.WRAP_CONTENT,

6. LinearLayout.LayoutParams.WRAP_CONTENT)); 7. } catch (IOException e) { 8. e.printStackTrace(); 9. }

5.3.2 ImageButton

ͼ5.3.2ImageButton

android.widget.ImageButtonͼƬ¿Ø¼þ£¬¼Ì³Ð×Ôandroid.widget.ImageView£¬ÔÚandroid.widget°üÖС£

×î¼òµ¥µÄʹÓ÷½·¨¡£srcÉèÖÃͼƬ·¾¶£¬¿ÉÒýÓÃdrawableµÄͼƬ¡£

Xml´úÂë

1.

¶¯Ì¬ÉùÃ÷ImageView£¬ÉèÖÃsrc¡£

Java´úÂë

1. 2. 3. 4. 5. 6. 7. 8. 9.

try {

ImageButton imageButton = new ImageButton(this);

InputStream inputStream = super.getAssets().open(\

imageButton.setImageDrawable(Drawable.createFromStream(inputStream, \

this.imageLayout.addView(imageButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT)); } catch (IOException e) { e.printStackTrace(); }

5.3.3 ImageSwitcherºÍGallery

ͼ5.3.3 ImageSwitcher

android.widget. ImageSwitcherͼƬ¿Ø¼þ£¬¼Ì³Ð×Ô

android.widget.ViewSwitcher£¨ViewGroup£©¡£ÔÚandroid.widget°üÖС£ ImageSwithcerÊÇÓÃÀ´Í¼Æ¬ÏÔʾÄÇ¿éÇøÓòµÄ¿Ø¼þ£¬Ê¹Ó÷½·¨

setInAnimation(Animation)£¬setOutAnimation(Animation)ÉèÖö¯»­¡£ GalleryÊÇÀ´¿ØÖƵ×ÏÂÄǸöͼ±êË÷ÒýÁбíË÷ÒýÓõġ£ImageAdapter¼Ì³Ð×ÔBaseAdapter£¬ÉèÖÃGalleryµÄÊÊÅäÆ÷¡£

ÔÚlayoutÌí¼ÓImageSwitcherºÍGallery¡£¶¨ÒåActivity£¬implements½Ó¿ÚOnItemSelectedListener, ViewFactory¡£onCreateµÄʱºò¶¨ÒåÒªÏÔʾͼƬ·¾¶ÁÐ±í£¬ÉèÖÃGalleryµÄAdapter¡£onItemSelectedʼþ´¥·¢Ê±£¬ÉèÖöÔÓ¦µÄͼƬ¡£

LayoutÎļþ¡£

Xml´úÂë

1.

2. 5.

6. 11.

12.

16. android:layout_alignParentBottom=\ 17. android:layout_alignParentLeft=\ 18. android:gravity=\ 19. android:spacing=\ /> 20.

21.

SwitcherActivityÀà¡£

Java´úÂë

1. public class SwitcherActivity extends Activity implements OnItemSelectedListener, View

Factory { 2.

3. private ImageSwitcher imageSwitcher; 4. private Gallery gallery; 5.

6. private ArrayList imageAssetPathList = new ArrayList(); 7.

8. @Override

9. protected void onCreate(Bundle savedInstanceState) { 10. super.onCreate(savedInstanceState);

11. super.setContentView(R.layout.switcher);

12. this.imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 13. this.gallery = (Gallery) findViewById(R.id.gallery); 14.

15. for (int i = 1; i <= 20; i++) {

16. this.imageAssetPathList.add(\ 17. } 18.

19. this.imageSwitcher.setFactory(this);

20. this.gallery.setAdapter(new ImageAdapter(this, this.imageAssetPathList)); 21. this.gallery.setOnItemSelectedListener(this); 22. 23. } 24.

25. @Override

26. public View makeView() {

27. ImageView imageView = new ImageView(this); 28. imageView.setBackgroundColor(0xFF000000);

29. imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

30. imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FIL

L_PARENT, LayoutParams.FILL_PARENT)); 31. return imageView; 32. } 33.

34. @Override

35. public void onItemSelected(AdapterView parent, View view, int position, long id) {

36. try {

37. InputStream inputStream = super.getAssets().open(this.imageAssetPathList.get(po

sition));

38. imageSwitcher.setImageDrawable(Drawable.createFromStream(inputStream, \

position));

39. } catch (IOException e) { 40. e.printStackTrace(); 41. } 42. } 43.

44. @Override

45. public void onNothingSelected(AdapterView parent) { 46. 47. } 48. 49. }

ImageAdapterÀà

Java´úÂë

1. 2. 3. 4. 5. 6. 7. 8.

public class ImageAdapter extends BaseAdapter {

private Context content;

private ArrayList imageAssetPathList;

public ImageAdapter(Context content, ArrayList imageAssetPathList) { this.content = content;

this.imageAssetPathList = imageAssetPathList;