동방프로젝트

갤러리

두원공대88학번뚜뚜 2020. 8. 19. 23:05

package com.example.forseveralactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.nio.channels.GatheringByteChannel;
import java.util.ArrayList;

//그리드뷰를 이용, 여러 사진을 격자모양으로 배치
//하나를 클릭시, 확대된 사진이 대화상자로 나옴

//그리드뷰의 작은 사진 클릭시, 큰 사진이 나오는 대화상자용 xml파일이 필요
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("갤러리 영화 포스터");

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

public class MyGalleryAdapter extends BaseAdapter {
Context context;
Integer[] posterID= {
R.drawable.chinhan, R.drawable.chiwawa, R.drawable.chunjae,
R.drawable.chutul, R.drawable.hanbun, R.drawable.hyumhan,
R.drawable.jinaga, R.drawable.insung, R.drawable.tenmonan,
R.drawable.chinhan, R.drawable.chiwawa, R.drawable.chunjae,
R.drawable.chutul, R.drawable.hanbun, R.drawable.hyumhan,
R.drawable.jinaga, R.drawable.insung, R.drawable.tenmonan,
R.drawable.chinhan, R.drawable.chiwawa, R.drawable.chunjae,
R.drawable.chutul, R.drawable.hanbun, R.drawable.hyumhan,
R.drawable.jinaga, R.drawable.insung, R.drawable.tenmonan,
R.drawable.chinhan, R.drawable.chiwawa, R.drawable.chunjae,
R.drawable.chutul, R.drawable.hanbun, R.drawable.hyumhan,
R.drawable.jinaga, R.drawable.insung, R.drawable.tenmonan
};
public MyGalleryAdapter(Context c) {
context=c;
}
public int getCount() {
return posterID.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}

public View getView(int position, View convertView,
ViewGroup parent) {
ImageView imageview = new ImageView(context);
imageview.setLayoutParams(new Gallery.LayoutParams(200, 300));
//이미지뷰를 각 그리드칸의 중앙에 배치
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
//공백의 설정
imageview.setPadding(5, 5, 5, 5);

//이미지뷰에, 파라미터로 넘어온 arg0 포지션의 위치를 적용, return
imageview.setImageResource(posterID[position]);

final int pos = position;
imageview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
ImageView ivPoster = (ImageView) findViewById(R.id.ivPoster);
ivPoster.setScaleType(ImageView.ScaleType.FIT_CENTER);
ivPoster.setImageResource(posterID[pos]);

return false;
}
});
return imageview;
}
}
}

////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery1"
android:spacing="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivPoster"
android:padding="20dp"/>

</LinearLayout>