android gallery Gallery Control Sample

gallery.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个Gallery画廊控件的案例"/>
    <Gallery android:id="@+id/mygallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

GalleryActivity.java:

package com.example.baseexample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity {
	private Gallery mGallery;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		
		mGallery = (Gallery)findViewById(R.id.mygallery);
		mGallery.setAdapter(new ImageAdapter(this));
	}
	
	private class ImageAdapter extends BaseAdapter{
		private Context mContext;
		private Integer[] mImage = {R.drawable.a,
				R.drawable.b,
				R.drawable.c,
				R.drawable.d,
				R.drawable.e,
				R.drawable.f};
		
		
		public ImageAdapter(Context c){
			mContext = c;
		}
		
		public int getCount(){
			return mImage.length;
		}
		
		public Object getItem(int position){
			return position;
		}
		
		public long getItemId(int position){
			return position;
		}
		
		public View getView(int position,View converView,ViewGroup parent){
			ImageView i = new ImageView(mContext);
			i.setImageResource(mImage[position]);
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setLayoutParams(new Gallery.LayoutParams(600, 1000));
			return i;
		}
	}
}


Reproduced in: https: //my.oschina.net/u/2552902/blog/543976

Guess you like

Origin blog.csdn.net/weixin_34008784/article/details/92326949