Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

기록장

[안드로이드] 리소스(Resource) 본문

안드로이드

[안드로이드] 리소스(Resource)

edit0 2021. 1. 20. 17:36
모든 내용은 "깡샘의 안드로이드 프로그래밍" 을 참고하고 있습니다.

 

저 보려고 만든거라 글 읽기 힘드실 수 있습니다.

 

 

리소스에 관한 내용 정리

 

특징

 

- 앱 리소스들은 모두 res 폴더 하위에 있어야 하며, 개발자가 임의로 폴더를 정의하는 것이 아니라 리소스별 폴더명이 - 지정되어 있음

- 리소스 폴더의 하위에도 폴더를 생성 불가

- 리소스 파일명에는 대문자 사용 금지

- 추가한 리소스를 식별하기 위한 int형 변수가 R.java 파일에 추가

 

drawable: 이미지, 이미지와 관련된 xml, 그림을 표현한 xml

layout: 화면 UI를 정의한 레이아웃 xml

values: 문자열, 색상, 크기 등 여러가지 값

menu: 액티비티의 메뉴를 구성하기 위한 xml

anim: 애니메이션을 위한 xml

raw: 바이트 단위로 직접 이용되는 이진 파일

mipmap: 앱 아이콘 이미지

 

 

Animation

 

모든 애니메이션은 그래픽 프로그램으로 작성하는게 기본이지만, 때에 따라 고정 애니메이션의 경우 항사 동일하게 움직이므로 사용할 수 있음

 

태그 종류

 

- scale: 크기 변경 애니메이션, 크기 확대/축소

- rotate: 회전 애니메이션

- alpha: 투명도 조정 애니메이션

- translate: 이동 애니메이션

 

공통 속성(필수 속성)

 

- duration: 지정한 애니메이션을 얼마 동안 지속하는지에 대한 설정. "2000"으로 설정하면 2초 동안 애니메이션 효과 지속

- startOffset: 애니메이션을 시작한 후 얼마 후부터 애니메이션 효과를 적용할 것인지에 대한 설정. "0"으로 지정하면 시작하자마자 적용. "500"으로 지정하면 0.5초 후부터 애니메이션 효과 적용

- repeatCount: 애니메이션 반복 횟수. "infinite"로 지정하면 무한 반복. "2"로 지정하면 2번 더 반복된 후 멈춤

- repeatMode: 애니메이션 반복 방향으로 "restart"로 지정하면 지정된 애니메이션 효과가 그대로 다시 적용. "reverse"로 지정하면 반대 방향으로 반복

 

 

animation1.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:duration="2000"/>

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:startOffset="0"
        android:duration="2000"/>
</set>

 

animation2.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0.0"
        android:toXDelta="0.0"
        android:fromYDelta="0.0"
        android:toYDelta="1000.0"
        android:startOffset="0"
        android:duration="2000"/>
</set>

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="버튼"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        android:id="@+id/imageview1"/>

</LinearLayout>

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button b1;
    ImageView iv1;

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

        b1 = findViewById(R.id.button1);

        final Animation anim = AnimationUtils.loadAnimation(this,R.anim.animation1);
        iv1 = findViewById(R.id.imageview1);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv1.startAnimation(anim);
            }
        });

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //애니메이션 시작되는 순간
                Log.i("TAG","Animation start");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //애니메이션 끝나는 순간
                Log.i("TAG","Animation end");
                Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation2);
                iv1.startAnimation(anim);
                anim.setFillAfter(true); //마지막에 멈춤
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //애니메이션 반복되는 순간
                Log.i("TAG","Animation repeat");
            }
        });


    }
}

 

 

values 크기, 색상 리소스

 

문자열, 배열, 색상, 크기 등 흔히 값이라고 표현되는 리소스는 values 폴더 하위에 위치한다. 다른 리소스들과 다르게 파일명으로 직접 식별되는 것이 아니라, 각 xml 파일의 태그 이름값으로 식별되어 사용된다.

 

문자열, 색상, 스타일, 배열, 정보, 크기 정보

(권장 파일명은 다음과 같음)

 

- strings.xml: 문자열 리소스 여러 개를 담는 파일. 파일 내에 <string> 태그로 각 리소스 등록

- colors.xml: 색상 리소스 여러 개를 담는 파일. 파일 내에 <color> 태그로 각 리소스 등록

- styles.xml: 스타일을 여러 개 담는 파일. 파일 내에 <style> 태그로 각 리소스 등록

- arrays.xml: 배열 리소스 여러 개를 담는 파일. 파일 내에 <string-array>, <integer-array> 태그로 각 리소스 등록

- dimens.xml: 크기 리소스를 담는 파일. 파일 내의 <dimen> 태그로 각 리소스 등록

 

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="my_blue">#2196F3</color>
    <color name="my_yellow">#FFEB3B</color>
</resources>


strings.xml

<resources>
    <string name="app_name">zzz</string>
    <string name="button">버튼</string>
</resources>


dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_margin">20dp</dimen>
    <dimen name="my_padding">20dp</dimen>
</resources>


적용

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="@string/button"
        android:layout_margin="@dimen/my_margin"
        android:padding="@dimen/my_padding"
        android:background="@color/my_blue"/>

 

style 리소스: 여러 속성을 하나의 스타일로 묶어 필요한 곳에 적용하기 위해 사용

values -> styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="my_style1">
        <item name="android:textColor">#673AB7</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">italic</item>
    </style>

</resources>


<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/my_style1"/>

my_style1을 만들고 적용

 

내가 만든거 상속 받아 재정의

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="my_style1">
        <item name="android:textColor">#673AB7</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">italic</item>
        <item name="android:text">스타일 텍스트</item>
    </style>

    <style name="my_style2" parent="my_style1">
        <item name="android:background">#4CAF50</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>


TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/my_style2"/>

 

테마 리소스(Theme)

방금까지 한건 뷰를 위한 스타일이였고, 이번엔 액티비티 전체 or 액 전체를 위한 스타일

 

기본 테마 스타일

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
</style>

 

기본 테마 스타일을 건드리면 앱 전체에 모두 영향이 간다. 그래서 만약 하나의 액티비티에 적용하고 싶다면 <activity> 태그에 테마를 설정해주면 된다.

<activity android=".MainActivity" android:theme="@style/AppTheme"></activity>

 

windowNoTitle(true or false): 제목 유무

windowActionBar(true or false): 액션바 유무

android:windowFullscreen(true or false): 기기의 맨 윗단 유무

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>

  <item name="windowNoTitle">true</item>
  <item name="windowActionBar">false</item>
  <item name="android:windowFullscreen">true</item>
</style>

+ android:screenOrientation="landscape": 액티비티 가로 방향 고정, android:screenOrientation="portrait": 액티비티 새로 방향 고정

 

 

EditText 기본 선 미출력

android:background="@android:color/transparent"

android:background="@android:color/transparent"

구분선 주기

<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D0D0"

 

zzz참고