본문 바로가기

안드로이드 Android

[모바일프로그래밍] 12-3 갤러리

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_marginStart="65dp"
        android:layout_marginTop="72dp"
        android:layout_marginEnd="65dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/gallery1"
        app:srcCompat="@android:drawable/screen_background_dark" />

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:spacing="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    Integer[] poster = {R.drawable.cars, R.drawable.charlie_and_the_chocolate_factory,
            R.drawable.chungking_express, R.drawable.coco, R.drawable.dead_poets_society,
            R.drawable.joker, R.drawable.memories_of_matsuko, R.drawable.night_at_the_museum,
            R.drawable.midnight_in_paris, R.drawable.whiplash};

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

        Gallery gal = (Gallery) findViewById(R.id.gallery1);
        MyGalleryAdapter galAdapter = new MyGalleryAdapter(this);
        gal.setAdapter(galAdapter);

        gal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView ivPoster = (ImageView) findViewById(R.id.imageView);
                ivPoster.setScaleType(ImageView.ScaleType.FIT_XY);
                ivPoster.setImageResource(poster[i]);
            }
        });
    }

    public class MyGalleryAdapter extends BaseAdapter {
        Context context;
        public MyGalleryAdapter(Context con) {
            context = con;
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(context);
            imgView.setLayoutParams(new Gallery.LayoutParams(200, 300));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setPadding(5, 5, 5, 5);
            imgView.setImageResource(poster[position]);
            return imgView;
        }
    }
}