歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之ImageSwitcher 圖片查看

Android之ImageSwitcher 圖片查看

日期:2017/3/1 10:56:16   编辑:Linux編程

布局文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <ImageSwitcher android:id="@+id/switcher"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:layout_alignParentLeft="true"
  10. android:layout_alignParentTop="true">
  11. </ImageSwitcher>
  12. <Gallery android:id="@+id/gallery"
  13. android:layout_width="fill_parent"
  14. android:layout_alignParentLeft="true"
  15. android:layout_height="60dp"
  16. android:spacing="15dp"
  17. android:layout_alignParentBottom="true"
  18. android:gravity="center_vertical"
  19. android:background="#aaaaaa">
  20. </Gallery>
  21. </RelativeLayout>
主程序如下:
  1. package com.cloay.imageswitcher;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemSelectedListener;
  11. import android.widget.Gallery.LayoutParams;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Gallery;
  14. import android.widget.ImageSwitcher;
  15. import android.widget.ImageView;
  16. import android.widget.ViewSwitcher.ViewFactory;
  17. /**
  18. * ImageSwitcherActivity.java
  19. * @author cloay
  20. * 2011-7-16
  21. */
  22. public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory{
  23. private ImageSwitcher imageSwitcher;
  24. private Gallery gallery;
  25. private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
  26. R.drawable.f, R.drawable.g};
  27. private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
  28. R.drawable.f, R.drawable.g};
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
  34. imageSwitcher.setFactory(this);
  35. //設置圖片切換時的動畫效果
  36. imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
  37. imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
  38. gallery = (Gallery)findViewById(R.id.gallery);
  39. //自定義ImageAdapter繼承於BaseAdapter,是一個內部類
  40. gallery.setAdapter(new ImageAdapter(this));
  41. gallery.setOnItemSelectedListener(this);
  42. }
  43. @Override
  44. public View makeView() {
  45. ImageView image = new ImageView(this);
  46. image.setScaleType(ImageView.ScaleType.FIT_CENTER);
  47. image.setLayoutParams(new ImageSwitcher.LayoutParams(
  48. LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
  49. return image;
  50. }
  51. @Override
  52. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  53. long arg3) {
  54. imageSwitcher.setImageResource(imagesId[arg2]);
  55. }
  56. @Override
  57. public void onNothingSelected(AdapterView<?> arg0) {
  58. }
  59. public class ImageAdapter extends BaseAdapter{
  60. private Context context;
  61. int galleryItemBackground;
  62. public ImageAdapter (Context c){
  63. context = c;
  64. TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery1);
  65. galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
  66. typeArray.recycle();
  67. }
  68. @Override
  69. public int getCount() {
  70. //返回selectId[]的長度
  71. return selectId.length;
  72. }
  73. @Override
  74. public Object getItem(int position) {
  75. return position;
  76. }
  77. @Override
  78. public long getItemId(int position) {
  79. return position;
  80. }
  81. @Override
  82. public View getView(int position, View convertView, ViewGroup parent) {
  83. ImageView imageView = new ImageView(context);
  84. //設置資源圖片
  85. imageView.setImageResource(selectId[position]);
  86. imageView.setAdjustViewBounds(true); //允許調整邊框
  87. //設定底部畫廊,自適應大小
  88. imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  89. //設置畫廊背景
  90. imageView.setBackgroundResource(galleryItemBackground);
  91. return imageView;
  92. }
  93. }
  94. }
Copyright © Linux教程網 All Rights Reserved