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

4일차 | 다수의 activity 사용, 애니메이션, 이벤트 리스너, drawable 디자인 만들기, values로 중복 관리

by Sharon kim 2022. 2. 17.

정정 : layout > b와 c바뀜

화면 구현 & 애니메이션 (쌤ver)

a_constraint_layout_03.xml

AndroidManifest.xml

rotate_anim.xml

SpaceActivity.java

 

 

결과

구글 android rotate 검색 복붙

res 오른쪽마우스 new -> 안드로이드 리소스 파일로 rotate_anim.xml만들기

rotate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:fromDegrees="0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toDegrees="3600">

</rotate><!--시간 : 4000(ms) 1000-> 1초-->

AndroidManifest.xml

intent-filter가 있어야 앱화면 재생시 첫화면으로 나옴&amp;nbsp;

SpaceActivity.java

package com.example.myapp3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class SpaceActivity extends AppCompatActivity {
    private static String TAG = "SpaceActivity";
    private Button button1;
    private ImageView imageViewRocketIcon;


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

        button1 = findViewById(R.id.button1);
        imageViewRocketIcon = findViewById(R.id.imageViewRocketIcon);

        // 이벤트 리스너 등록
        button1.setOnClickListener(v->{
            Log.d(TAG, "버튼이 클릭되었습니다.");
            Animation anim = AnimationUtils.loadAnimation(
                    getApplicationContext(),
                    R.anim.rotate_anim
            );
            imageViewRocketIcon.startAnimation(anim);
        });
    }
}
/*
* 1. 새로운 액티비티 만들기
* 2. setContentView 메서드 안에 R.id.layout.constraint_02
* 3. 매니페스트 파일에 첫 화면을 세팅(여러분이 만든 화면)
* 4. 이벤트리스너
* 5. 애니메이션을 연결해서 동작 시켜 보기
* */

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


    <ImageView
        android:id="@+id/icon1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/space_station_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/icon2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.05" />

    <ImageView
        android:id="@+id/icon2"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/rocket_icon"
        app:layout_constraintEnd_toStartOf="@id/icon3"
        app:layout_constraintStart_toEndOf="@id/icon1"
        app:layout_constraintTop_toTopOf="@id/icon1" />

    <ImageView
        android:id="@+id/icon3"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/rover_icon"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/icon2"
        app:layout_constraintTop_toTopOf="@id/icon1" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Space Stations"
        app:layout_constraintEnd_toEndOf="@id/icon1"
        app:layout_constraintStart_toStartOf="@id/icon1"
        app:layout_constraintTop_toBottomOf="@id/icon1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Flights"
        app:layout_constraintEnd_toEndOf="@id/icon2"
        app:layout_constraintStart_toStartOf="@id/icon2"
        app:layout_constraintTop_toBottomOf="@id/icon2" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Rovers"
        app:layout_constraintEnd_toEndOf="@id/icon3"
        app:layout_constraintStart_toStartOf="@id/icon3"
        app:layout_constraintTop_toBottomOf="@id/icon3" />

    <TextView
        android:id="@+id/textViewDCA"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@color/purple_200"
        android:gravity="center"
        android:paddingEnd="10dp"
        android:text="DCA"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/textViewMARS"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/icon1"
        app:layout_constraintVertical_bias="0.1" />

    <TextView
        android:id="@+id/textViewMARS"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@color/purple_200"
        android:gravity="center"
        android:paddingStart="10dp"
        android:text="MARS"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/textViewDCA"
        app:layout_constraintTop_toTopOf="@id/textViewDCA" />


    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/double_arrows"
        app:layout_constraintBottom_toBottomOf="@id/textViewDCA"
        app:layout_constraintEnd_toEndOf="@id/textViewMARS"
        app:layout_constraintStart_toStartOf="@id/textViewDCA"
        app:layout_constraintTop_toTopOf="@id/textViewDCA" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.1" />


    <com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/switch1"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="@color/purple_200"
        android:padding="10dp"
        android:text="One Way"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="@id/guideline1"
        app:layout_constraintTop_toBottomOf="@id/textViewDCA" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/purple_200"
        android:padding="10dp"
        android:text="1 Traveller"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@id/guideline1"
        app:layout_constraintTop_toBottomOf="@id/switch1" />


    <ImageView
        android:id="@+id/imageViewGalaxy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/galaxy"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.7" />

    <ImageView
        android:id="@+id/imageViewRocketIcon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="50dp"
        android:src="@drawable/rocket_icon"
        app:layout_constraintBottom_toBottomOf="@id/imageViewGalaxy"
        app:layout_constraintEnd_toStartOf="@id/imageViewGalaxy"
        app:layout_constraintTop_toTopOf="@id/imageViewGalaxy" />


    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/purple_200"
        android:text="DEPART"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

/*복습
 * 1. 새로운 액티비티 만들기
 * 2. setContentView 메서드 안에 R.id.layout.constraint_02
 * 3. 매니페스트 파일에 첫 화면을 세팅(여러분이 만든 화면)
 * 4. 이벤트리스너
 * 5. 애니메이션을 연결해서 동작 시켜 보기
 * */

b_constraint_layout_02.xml

AndroidManifest.xml

rotate_anim.xml

NewActivity.java

동일하게 구현


AndroidManifest.xml

c_calculator_01.xml

MainActivity.java

버튼 클릭시 로그캣

package com.example.myapp3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //1~9까지 선언
    TextView one;
    TextView two;
    TextView three;
    TextView four;
    TextView five;
    TextView six;
    TextView seven;
    TextView eight;
    TextView nine;
    TextView zero;
    TextView ca;
    TextView plus;

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

        //변수의 식별자-> 주소를 초기화(값을 넣다)
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        zero = findViewById(R.id.zero);
        ca = findViewById(R.id.ca);
        plus = findViewById(R.id.plus);



        //이벤트 리스너
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "1번 버튼을 눌렀습니다.");
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "2번 버튼을 눌렀습니다.");
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "3번 버튼을 눌렀습니다.");
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "4번 버튼을 눌렀습니다.");
            }
        });
        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "5번 버튼을 눌렀습니다.");
            }
        });
        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "6번 버튼을 눌렀습니다.");
            }
        });
        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "7번 버튼을 눌렀습니다.");
            }
        });
        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "8번 버튼을 눌렀습니다.");
            }
        });
        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "9번 버튼을 눌렀습니다.");
            }
        });
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "0번 버튼을 눌렀습니다.");
            }
        });
        ca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "0번 버튼을 눌렀습니다.");
            }
        });
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "0번 버튼을 눌렀습니다.");
            }
        });

        //람다 표현식 <-- 어떤 것을 간단하게 표현할 수 있다.
        one.setOnClickListener(v -> {
            Log.d("TAG", "람다 표현식으로 변경함!!!");
        });
        //한 줄일 때는 중괄호 생략
        two.setOnClickListener(v -> Log.d("TAG", "2 버튼 클릭"));

    }
}

AndroidManifest.xml

MainActivity2.java

c_calculator_01.xml

package com.example.myapp3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

    TextView one;
    TextView two;
    TextView three;
    TextView four;
    TextView five;
    TextView six;
    TextView seven;
    TextView eight;
    TextView nine;
    TextView zero;
    TextView ca;
    TextView plus;
    TextView result;

    String newValue = "";
    String oldValue = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c_calculator_01);
        initData();
        addEventListener();
    }

    private void initData() {
        zero = findViewById(R.id.zero);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        ca = findViewById(R.id.ca);
        plus = findViewById(R.id.plus);
        result = findViewById(R.id.result);
    }

    private void addEventListener() {
        zero.setOnClickListener(v -> {
            newValue = newValue + "0";
            result.setText(newValue);
        });
        one.setOnClickListener(v -> {
            newValue = newValue + "1";
            result.setText(newValue);
        });
        two.setOnClickListener(v -> {
            newValue = newValue + "2";
            result.setText(newValue);
        });
        three.setOnClickListener(v -> {
            // 1 + 3 (기존에 사용했던 값을 가지고 오고 
            //+ 여기에서 들어가는 값 (3))
            //"1" + "3" = "13"
            newValue = newValue + "3";
            result.setText(newValue);
        });
        four.setOnClickListener(v -> {
            newValue = newValue + "4";
            result.setText(newValue);
        });
        five.setOnClickListener(v -> {
            newValue = newValue + "5";
            result.setText(newValue);
        });
        six.setOnClickListener(v -> {
            newValue = newValue + "6";
            result.setText(newValue);
        });
        seven.setOnClickListener(v -> {
            newValue = newValue + "7";
            result.setText(newValue);
        });
        eight.setOnClickListener(v -> {
            newValue = newValue + "8";
            result.setText(newValue);
        });
        nine.setOnClickListener(v -> {
            newValue = newValue + "9";
            result.setText(newValue);
        });
        ca.setOnClickListener(v -> {
            newValue = "";
            oldValue = "0";
            result.setText(newValue);
        });
        plus.setOnClickListener(v -> {
            //연산
            int number1 = Integer.parseInt(newValue);
//            if (oldValue == ""){
//                oldValue ="0";
//            }
            int number2 = Integer.parseInt(oldValue);
            int sum = (number1 + number2);

            oldValue = String.valueOf(sum);
            newValue = "";
            result.setText(oldValue);
            //.setText() 텍스트 변경

        });
        result.setOnClickListener(v -> {

        });

    }
}

g_drawable_example_01.xml

drawable폴더에 양식 만들어 사용가능

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/a_gradient_01"
            android:gravity="center"
            android:text="@string/str_android"
            android:textColor="@color/white" />

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/d_stroke_01"
            android:gravity="center"
            android:text="@string/str_android"
            android:textColor="@color/black" />

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/b_solid_01"
            android:gravity="center"
            android:text="@string/str_android"
            android:textColor="@color/white" />

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/c_solid_02"
            android:gravity="center"
            android:text="@string/str_android"
            android:textColor="@color/white" />

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/e_radius_01"
            android:gravity="center"
            android:text="@string/str_android"
            android:textColor="@color/white" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/a_gradient_01"
            android:text="Button" />

        <Button
            android:id="@+id/textButton"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text button" />

        <androidx.appcompat.widget.AppCompatButton
            android:background="@drawable/a_gradient_01"
            android:text="button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

values에 중복되는 스타일, 색, 텍스트 저장가능

style 사용
색 사용
텍스트사용

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="button_style_1" parent="TextAppearance.AppCompat">
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">30dp</item>
    </style>
</resources>
<resources>
    <string name="app_name">MyApp3</string>
    <string name="str_android">android</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>