Gallery入門

4676 ワード

最近プロジェクトはそんなに忙しくなくて、研究点はふだん接触したことがないコントロールについて、直接コードに行って、効果図の添付ファイル、インタフェースは少し醜いです
/**
 * Created by Y on 2015/7/16.
 */
public class GalleryActivity extends Activity implements ViewSwitcher.ViewFactory{

    private Gallery gallery;
    private ImageSwitcher imageSwitcher;
    // 
    private Integer[] imageInteger={
            R.drawable.ic_launcher,
            R.drawable.logo_twitter,
            R.drawable.logo_vkontakte,
            R.drawable.logo_wechat,
            R.drawable.logo_wechatfavorite,
            R.drawable.logo_wechatmoments,
            R.drawable.logo_whatsapp
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grally);
        initViews();
        gallery.setAdapter(new ImageAdapter(this));
        imageSwitcher.setFactory(this);
        imageSwitcher.setImageResource(imageInteger[0]);
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(GalleryActivity.this, " " + (position + 1) + " ", Toast.LENGTH_LONG).show();
                // onItemSelected() , 
                imageSwitcher.setImageResource(imageInteger[position]);
            }
        });
    }

    private void initViews() {
        gallery = (Gallery) findViewById(R.id.gallery);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
    }

    @Override
    public View makeView() {// makeView() , ImageView 
        ImageView i = new ImageView(this);
        // 
        i.setBackgroundColor(0xFF000000);
        // 
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        // 
        i.setLayoutParams(new ImageSwitcher.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
        return i;
    }

    class ImageAdapter extends BaseAdapter {
        // Context
        private Context context;
        //  ImageAdapter
        public ImageAdapter(Context c){
            context = c;
        }
        @Override
        // 
        public int getCount() {
            return imageInteger.length;
        }
        @Override
        // 
        public Object getItem(int position) { return position; }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            // ImageView 
            imageView.setImageResource(imageInteger[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            // 
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //   128x192 
            imageView.setLayoutParams(new Gallery.LayoutParams(400, 400));
            return imageView;
        }
    }
}

 
<?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="fill_parent"
    >
    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        />
    <Gallery
        android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:spacing="16dp"
        />
</RelativeLayout>