Gallery 画廊

1. images.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	
	<!-- 按照320*480的分辨率计算, 480 = 390(大图片height) + 60(小图片height) + 30(任务栏height) -->
	
	<ImageSwitcher android:id="@+id/images_imageswitcher"
		android:layout_width="fill_parent"
		android:layout_height="390dp"
		android:layout_alignParentTop="true"
		android:layout_alignParentLeft="true"
	/>
	
	<Gallery android:id="@+id/images_gallery"
		android:layout_width="fill_parent"
		android:layout_height="60dp"
		android:layout_alignParentBottom="true"
		android:layout_alignParentLeft="true"
		android:gravity="center_vertical"
		android:spacing="16dp"
	/>
</RelativeLayout>

 

2.ImagesActivity

public class ImagesActivity extends Activity implements ViewSwitcher.ViewFactory, Gallery.OnItemSelectedListener {
	private ImageSwitcher mSwitcher;
	private int[] mThumbIds = {
			R.drawable.a,
			R.drawable.b,
			R.drawable.c,
			R.drawable.d,
			R.drawable.e,
			R.drawable.f,
			R.drawable.g,
			R.drawable.h,
			R.drawable.i,
			R.drawable.j,
			R.drawable.k,
			R.drawable.l,
	}; // a - l 表示图片

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.images);
		// 设置ImageSwitcher
		mSwitcher = (ImageSwitcher) findViewById(R.id.images_imageswitcher);
		mSwitcher.setFactory(this); // 通过ViewSwitcher.ViewFactory进行资源配置
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
		
		// 设置Gallery
		Gallery mGallery = (Gallery) findViewById(R.id.images_gallery);
		mGallery.setAdapter(new ImageAdapter(this)); // 和ListView一样,Gallery需要一个adapter进行资源配置
		mGallery.setOnItemSelectedListener(this);
	}

	public View makeView() {
		ImageView mImageView = new ImageView(this);
		mImageView.setBackgroundColor(0xFF000000); //设置ImageSwitcher图片(ImageView)背景
		mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); //设置ImageSwitcher图片(ImageView)ScaleType
		mImageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); //设置ImageSwitcher图片(ImageView)大小
		return mImageView;
	}
	
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		mSwitcher.setImageResource(mThumbIds[position]);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
	}
	
	private class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private ImageAdapter(Context c) {
			mContext = c;
		}
		
		public int getCount() {
			return mThumbIds.length;
		}

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

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

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView mImageView;
			if (convertView == null) { // if it's not recycled, initialize some attributes.
				mImageView = new ImageView(mContext);
				mImageView.setLayoutParams(
						new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); //设置Gallery图片(ImageView)大小
				mImageView.setBackgroundResource(R.drawable.icon); //设置Gallery图片(ImageView)背景
				mImageView.setAdjustViewBounds(true); //使Gallery图片自适应屏幕分辨率,以免图片bound超出屏幕范围
				mImageView.setImageResource(mThumbIds[position]); //设置Gallery图片(ImageView)源资源
			} else {
				mImageView = (ImageView) convertView;
			}
			return mImageView;
		}
	}
}

 

3. 获得图片

上面的例子中,ImageSwitcher和ImageView是通过

ImageSwitcher.setImageResource(int resId);

ImageView.setImageResource(int resId);

设置图片的。我们还可以通过下面2个方法设置图片

ImageSwitcher.setImageDrawable(Drawable drawable);

ImageView.setImageDrawable(Drawable drawable);

你可能感兴趣的:(C++,c,xml,android,C#)