畫廊視圖Gallery組件學習筆記

GallerySpinner組件有共同的父類:AbsSpinner,表明GallerySpinner都是一個列表框。區別:Spinner顯示的是一個垂直的列表框,而Gallery顯示的是一個水平的列表框。

示例java代碼:

public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gallery = (Gallery) findViewById(R.id.gallery);
		// 獲取顯示圖片的ImageView對象
		imageView = (ImageView) findViewById(R.id.imageView);
		// 創建一個BaseAdapter對象,該對象負責提供Gallery所顯示的列表項
		BaseAdapter adapter = new BaseAdapter()
		{
			@Override
			public int getCount()
			{
				return imageIds.length;
			}

			@Override
			public Object getItem(int position)
			{
				return position;
			}

			@Override
			public long getItemId(int position)
			{
				return position;
			}

			// 該方法的返回的View就是代表了每個列表項
			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{
				// 創建一個ImageView
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setImageResource(imageIds[position]);
				// 設置ImageView的縮放類型
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				// 爲imageView設置佈局參數
				imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
				TypedArray typedArray = obtainStyledAttributes(
						R.styleable.Gallery);
				imageView.setBackgroundResource(typedArray.getResourceId(
						R.styleable.Gallery_android_galleryItemBackground, 0));
				return imageView;
			}
		};
		gallery.setAdapter(adapter);
		gallery.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			// 當Gallery選中項發生改變時觸發該方法
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id)
			{
				imageView.setImageResource(imageIds[position]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent)
			{
			}
		});
	}

說明:

<>imageView.setImageResource(imageIds[position]); 

功能:Sets a drawable as the content of this ImageView.

參數:resId - the resource identifier of the the drawable

<>imageView.setScaleType(ImageView.ScaleType.FIT_XY);

功能:Controls how the image should be resized or moved to match the sizeof this ImageView.

參數:scaleType - The desired scaling mode.

<>imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));

功能:Set the layout parameters associated with this view. These supplyparameters to the parent of this view specifying how it should bearranged.

 There are many subclasses of ViewGroup.LayoutParams, and thesecorrespond to the different subclasses of ViewGroup that are responsiblefor arrangin

g their children.

參數:params - the layout parameters for this view

<><>Gallery.LayoutParams(75, 100)

利用LayoutParams(int w, int h)的構造方法進行初始化。

功能:Gallery extends LayoutParams to provide a place to hold currentTransformation information along with previous position/transformationinfo.

<> obtainStyledAttributes(int[] attrs)

功能:Retrieve styled attribute information in this Context's theme. 

<>TypedArray

功能:Container for an array of values that were retrieved obtainStyledAttributes().該類定義容器存儲數據。

<>setBackgroundResource(int resid)

功能:Set the background to a given resource. The resource should refer to a Drawable object.

參數:resid - The identifier of the resource.

顯示效果:



發佈了33 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章