본문 바로가기
안드로이드/리소스 관리

안드로이드 이미지 리소스

by 김어찐 2022. 6. 23.
728x90

안드로이드는 drawable 폴더에 이미지를 넣어서 이미지를 사용할 수 있다.
이 때 xml 통해 이미지를 새롭게 구성하여 사용할 수 있다.
여러 이미지를 겹쳐 하나의 이미지를 만들 때
패턴 이미지를 만들 때
상태에 따른 이미지를 만들 때
기타 등등

 

drawable vs mipmap

현재 안드로이드는 이미지를 넣는 폴더로 drawable과 mipmap을 제공한다.
mipmap 은 애플리케이션의 아이콘으로 사용할 이미지를 넣는 곳이고 drawable은 애플리케이션 내에서 사용할 이미지를 넣은 곳이다.
두 폴더의 차이는 뒤에서 살펴본다.

 

drawable, bitmap

 

두 클래스 모두 이미지 데이터를 관리하는 클래스이다.
Bitmap은 jpg, png, gif 파일로 부터 읽어온 이미지 데이터를 관리하는 객체이다.
Drawable 은 Bitmap을 포함하여 xml 파일을 통해 만든 이미지, 코드를 통해 만든 이미지 등도 포함한다. Drawable은 안드로이드에서 화면이 그릴 수 있는 요소들을 모두 포함한다.

 

package com.example.imgresource

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageView2.setImageResource(R.drawable.img_android)

        // Bitmap : jpg,png,gif 파일로 부터 읽어온 이미지 데티러를 관리
        val bitmap = BitmapFactory.decodeResource(resources,R.drawable.img_android)
        imageView3.setImageBitmap(bitmap)
        
        // Drawable : Bitmap을 포함한 다양한 타입으로 부터 이미지 데이터를 관리
        val drawable = getDrawable(R.drawable.img_android)
        imageView4.setImageDrawable(drawable)

        // 배경 타이 이미지를 생성한다
        //container.setBackgroundResource(R.drawable.tile)

        val drawable2 = getDrawable(R.drawable.tile)
        container.background = drawable2

        // layer 이미지를 사용한다.
        val drawable3 = getDrawable(R.drawable.layer)
        imageView4.setImageDrawable(drawable3)

        // 상태 이미지를 사용한다.
        val drawable4 = getDrawable(R.drawable.btn_image)
        button.background = drawable4

    }
}

타일

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/p"
    android:tileMode="repeat"
    />

 

레이어 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/img1" android:gravity="center"/>
    </item>
    <item android:top="10dp" android:left="10dp" android:gravity="center">
        <bitmap android:src="@drawable/img2"/>
    </item>
    <item android:top="20dp" android:left="20dp" android:gravity="center">
        <bitmap android:src="@drawable/img3"/>
    </item>
</layer-list>

 

버튼 이미지 drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn1"/>
    <item android:state_pressed="false" android:drawable="@drawable/btn2"/>
</selector>

 

728x90