標籤:
1.Gallery
Gallery控制項一般是用於顯示映像列表,因此也稱為是畫廊控制項, Gallery只能水平顯示一行,而且支援水平滑動效果。也就是說,單擊、選中或者拖動Gallery中的映像, Gallery映像中的列表會根據不同的情況向左向右移動,直到顯示到最後的一個映像為止。
2.ImageSwicther
ImageSwitcher控制項可以用在不同的映像之間切換,其中切換的過程可以採用動畫的方法,如淡入淡出的效果。ImageSwitcher需要一個映像工廠(ViewFactory)來建立用於顯示映像的ImageView對象,因此我們需要一個實現android.widget.ViewSwitcher.ViewFactory介面的類。 使用ImageSwitcher時,基本步驟: 1.必須implements ViewSwitcher.ViewFactory 2.調用setFactory方法:imageSwitcher.setFactory(this) 3.重寫函數 makeView(),做如下處理:(此方法在進入該Activity時就會調用) 4.在需要顯示圖片的地方寫如下代碼 以下具體列子如下: 在main.xml檔案中:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_height="fill_parent" 4 android:layout_width="fill_parent"> 5 <ImageSwitcher 6 android:id="@+id/imageswitcher" 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent"> 9 </ImageSwitcher>10 <Gallery 11 android:id="@+id/gallery"12 android:layout_width="fill_parent"13 android:layout_height="wrap_content"14 android:layout_alignParentBottom="true"/>15 </RelativeLayout>
在java檔案中:
1 public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{ 2 3 private Gallery gallery; 4 private ImageSwitcher is; 5 private int galleryItemBackGroundId; 6 private int imageId[]={R.drawable.gril1,R.drawable.gril2,R.drawable.gril3,R.drawable.gril4,R.drawable.gril5, 7 R.drawable.gril6,R.drawable.gril7,R.drawable.gril8,R.drawable.gril9,R.drawable.gril10, 8 R.drawable.gril11}; 9 @Override10 protected void onCreate(Bundle savedInstanceState) {11 super.onCreate(savedInstanceState);12 requestWindowFeature(Window.FEATURE_NO_TITLE); //去除標題13 setContentView(R.layout.main);14 15 gallery = (Gallery) findViewById(R.id.gallery);16 gallery.setAdapter(new MyGalleryAdapter(MainActivity.this));17 gallery.setOnItemSelectedListener(this); //設定選項監聽用setOnItemSelectedListener18 19 is = (ImageSwitcher) findViewById(R.id.imageswitcher);20 is.setFactory(this);21 22 is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));23 is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));24 25 }26 27 @Override28 public boolean onCreateOptionsMenu(Menu menu) {29 // Inflate the menu; this adds items to the action bar if it is present.30 getMenuInflater().inflate(R.menu.main, menu);31 return true;32 }33 34 class MyGalleryAdapter extends BaseAdapter{35 36 private Context context;37 38 public MyGalleryAdapter(Context context) {39 this.context = context;40 TypedArray typed = obtainStyledAttributes(R.styleable.Gallery); //獲得屬性集合41 galleryItemBackGroundId = typed.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);42 }43 44 @Override45 public int getCount() {46 // TODO 自動產生的方法存根47 return Integer.MAX_VALUE; //為了實現畫廊中圖片迴圈效果,才設定return_Integer.Max_VALUE,其實可以return imageId.length;48 }49 50 @Override51 public Object getItem(int position) {52 // TODO 自動產生的方法存根53 return imageId[position];54 }55 56 @Override57 public long getItemId(int position) {58 // TODO 自動產生的方法存根59 return position;60 }61 62 @Override63 public View getView(int position, View convertView, ViewGroup parent) {64 // TODO 自動產生的方法存根65 ImageView imageView = new ImageView(context); 66 imageView.setImageResource(imageId[position%imageId.length]); 設定圖片到view中67 imageView.setScaleType(ScaleType.FIT_XY);68 imageView.setLayoutParams(new Gallery.LayoutParams(156, 100)); 69 imageView.setBackgroundResource(galleryItemBackGroundId); //設定imageview 背景70 return imageView;71 }72 73 }74 75 @Override76 public View makeView() {77 ImageView imageView = new ImageView(MainActivity.this);78 imageView.setAdjustViewBounds(true); //是否保持寬高比。需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果
79 imageView.setScaleType(ScaleType.FIT_XY);80 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));81 82 return imageView;83 }84 85 @Override86 public void onItemSelected(AdapterView<?> parent, View view, int position, //畫廊(Gallery)選中的項87 long id) {88 // TODO 自動產生的方法存根89 90 is.setImageDrawable(getResources().getDrawable(imageId[position%imageId.length]));91 }92 93 @Override94 public void onNothingSelected(AdapterView<?> parent) {95 // TODO 自動產生的方法存根96 97 }98 }
運行效果:
android中Gallery與ImageSwicther解析