Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android普通和循環Gallery

Android普通和循環Gallery

編輯:關於Android編程

效果圖:

\

代碼

Main.java

package com.example.gallerydemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class Main extends Activity {

	private Gallery gallery = null;
	private Gallery galleryLoop = null;
	private TextView tvNum = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		tvNum = (TextView) findViewById(R.id.num);

		gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setSpacing(5);

		gallery.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				int num = arg2 + 1;
				// Toast.makeText(Main.this, "點擊了" + num, Toast.LENGTH_SHORT)
				// .show();
				tvNum.setText("常規Gallery:點擊了" + num);
			}
		});

		galleryLoop = (Gallery) findViewById(R.id.galleryLoop);
		galleryLoop.setAdapter(new LoopImageAdapter(this));
		galleryLoop.setSpacing(10);
		galleryLoop.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				int num = (arg2 + 1) % 9;
				if (num == 0) {
					num = 9;
				}
				// Toast.makeText(Main.this, "點擊了" + num, Toast.LENGTH_SHORT)
				// .show();
				tvNum.setText("循環Gallery:點擊了" + num);
			}

		});

	}

	// 普通Gallery的Adapter
	class ImageAdapter extends BaseAdapter {
		private Context context;

		public ImageAdapter(Context context) {
			this.context = context;
		}

		private Integer[] imageInteger = { R.drawable.pic_1, R.drawable.pic_2,
				R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
				R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
				R.drawable.pic_9 };

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

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

		public int getCount() {
			return imageInteger.length;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(imageInteger[position]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
			return imageView;

		}

	}

	// 循環Gallery的Adapter
	class LoopImageAdapter extends BaseAdapter {
		private Context context;

		public LoopImageAdapter(Context context) {
			this.context = context;
		}

		private Integer[] imageInteger = { R.drawable.pic_1, R.drawable.pic_2,
				R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
				R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
				R.drawable.pic_9 };

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

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

		public int getCount() {
			return Integer.MAX_VALUE;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(imageInteger[position
					% imageInteger.length]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
			return imageView;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

main.xml



    

    

    

    

    



轉載請注明出處:周木水的CSDN博客 http://blog.csdn.net/zhoumushui

我的GitHub:周木水的GitHub https://github.com/zhoumushui


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved