본문 바로가기
코리아 IT아카데미/android

9일차 | glide 사용해보기

by Sharon kim 2022. 2. 28.

 

MainActivity.java

package com.example.myaddview;

import static com.example.myaddview.Fruit.getFruits;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        ArrayList<Fruit> sampleData = getFruits();

        //준비물
        //1. 뷰를 위치 시킬 컨테이너 가져오기
        LinearLayout container = findViewById(R.id.fruitContainer);
        //2. xml 파일을 메모리에 올리는 방법
        LayoutInflater inflater = LayoutInflater.from(this);

        sampleData.forEach(fruit -> {
            View itemView = inflater.inflate(R.layout.item_food, container, false);

            ImageView fruitImageView = itemView.findViewById(R.id.fruitImageView);
            TextView fruitName = itemView.findViewById(R.id.fruitName);
            TextView fruitCount = itemView.findViewById(R.id.fruitCount);

            TextView fruitPrice = itemView.findViewById(R.id.fruitPrice);

//            fruitImageView.setBackgroundColor(Color.BLACK);
            Log.d("TAG","fruit.imageUrl : "+ fruit.imageUrl);
            Glide.with(this)
                    .load(fruit.imageUrl)
                    .centerCrop()
                    .into(fruitImageView);
            Glide.with(this)
                    .load(fruit.imageUrl)
                    .apply(new RequestOptions().circleCrop())
                    .into(fruitImageView);

            fruitName.setText(fruit.name);
            fruitName.setText(fruit.count);
            fruitName.setText(fruit.price);

            container.addView(itemView);
            //glide에 round 처리 하기 과제, packed ?

        });
    }
}

 

Fruit.java

package com.example.myaddview;
//        https://cdn.pixabay.com/photo/2010/12/13/10/05/berries-2277__340.jpg
//        https://cdn.pixabay.com/photo/2017/05/11/19/44/fresh-fruits-2305192__340.jpg
//        https://cdn.pixabay.com/photo/2021/07/30/04/17/orange-6508617__340.jpg
//        https://cdn.pixabay.com/photo/2015/12/30/11/57/fruits-1114060__340.jpg
//        https://cdn.pixabay.com/photo/2016/01/02/01/59/oranges-1117628__340.jpg
//        https://cdn.pixabay.com/photo/2021/11/11/16/05/fruits-6786607__340.jpg
//        https://cdn.pixabay.com/photo/2011/03/16/16/01/tomatoes-5356__340.jpg
//        https://cdn.pixabay.com/photo/2018/05/01/18/21/eclair-3366430__340.jpg
import java.util.ArrayList;

public class Fruit {

    String imageUrl;
    String name;
    String count;
    String price;

    public Fruit(String imageUrl, String name, String count, String price) {
        this.imageUrl = imageUrl;
        this.name = name;
        this.count = count;
        this.price = price;
    }

    //샘플 데이터
    public static ArrayList<Fruit> getFruits(){
        ArrayList<Fruit> fruits = new ArrayList<>();
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2010/12/13/10/05/berries-2277__340.jpg",
                "레몬","10","5000원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2017/05/11/19/44/fresh-fruits-2305192__340.jpg",
                "베리","35","3800원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2021/07/30/04/17/orange-6508617__340.jpg",
                "사과","18","5500원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2015/12/30/11/57/fruits-1114060__340.jpg",
                "오렌지","31","3000원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2016/01/02/01/59/oranges-1117628__340.jpg",
                "두리안","12","8000원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2021/11/11/16/05/fruits-6786607__340.jpg",
                "망고", "50","7000원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2011/03/16/16/01/tomatoes-5356__340.jpg",
                "수박","19","18000원"));
        fruits.add(new Fruit("https://cdn.pixabay.com/photo/2018/05/01/18/21/eclair-3366430__340.jpg",
                "멜론","17","15000원"));

        return fruits;
    }
}

 

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">

    <TextView
        android:id="@+id/pageTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="과일 목록"
        android:textSize="24dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/scrollView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/pageTitle">

        <LinearLayout
            android:id="@+id/fruitContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

 

Item_food.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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <ImageView
        android:id="@+id/fruitImageView"
        android:layout_width="120dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/fruitName"
        app:layout_constraintHorizontal_bias="0.1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/fruitName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="Lemons"
        android:textSize="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/fruitImageView"
        app:layout_constraintTop_toTopOf="@id/fruitImageView" />

    <TextView
        android:id="@+id/fruitCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="수량10개"
        app:layout_constraintEnd_toEndOf="@id/fruitName"
        app:layout_constraintStart_toStartOf="@id/fruitName"
        app:layout_constraintTop_toBottomOf="@id/fruitName" />

    <TextView
        android:id="@+id/fruitPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="가격2_000원"
        app:layout_constraintEnd_toEndOf="@id/fruitName"
        app:layout_constraintStart_toStartOf="@id/fruitName"
        app:layout_constraintTop_toBottomOf="@id/fruitCount" />


</androidx.constraintlayout.widget.ConstraintLayout>