Activity_2_ImageView

소정·2023년 2월 10일
0

Android_with_Java

목록 보기
3/33

ImageView

  • 이미지 사이즈가 자동 스케일링 됨 (이미지가 구겨지지 않음)
  • 짧은면을 기준으로 맞춘다
  • scaleType="centerCrop" : 썸네일 이미지 보여줄 때 많이 씀
  • 이미지는 리소스의 번호를 가져오는 방법이 없다 => Tag 기능을 활용해 번호 달아주기 방법 활용!
<?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:padding="16dp">

    <ImageView
        android:id="@+id/imgV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/flag_korea"
        android:scaleType="centerInside"
        android:background="@drawable/ic_launcher_background"/>

    <Button
        android:id="@+id/btnAus"
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="australia"/>

    <Button
        android:id="@+id/btnBgi"
        android:layout_marginTop="3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="belgium"/>

    <Button
        android:id="@+id/btnKor"
        android:layout_marginTop="3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="korea"/>

    <Button
        android:id="@+id/btnIty"
        android:layout_marginTop="3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="italy"/>

    <!-- 이미지뷰 사용할 때 뷰크기에 wrap_content를 저장할 때 주의점
    wrap_content 은 원본 이미지의 사이즈에 맞춤 스케일링된 이미지와는 상관없다
    -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:src="@drawable/flag_france"
        android:layout_marginRight="50dp"/>

        <!-- wrap_content로 썼을 때 !!
        이때 실제 보이는 이미지 사이즈만큼 이미지뷰 크기를 조절해 주는 속성 -->
        <ImageView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:src="@drawable/flag_france"
            android:adjustViewBounds="true"/>

    </LinearLayout>


</LinearLayout>

package com.bsj0420.ex04imageview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView imgV;
    Button btnAus;
    Button btnBgi;
    Button btnKor;
    Button btnIty;

    int num =0;

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

        imgV = findViewById(R.id.imgV);
        btnAus = findViewById(R.id.btnAus);
        btnBgi = findViewById(R.id.btnBgi);
        btnKor = findViewById(R.id.btnKor);
        btnIty = findViewById(R.id.btnIty);

        btnAus.setOnClickListener(listener);
        btnBgi.setOnClickListener(listener);
        btnKor.setOnClickListener(listener);
        btnIty.setOnClickListener(listener);

        //이미지뷰  클릭하면 그림을 차례대로 변경
        imgV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //이미지를 차례대로 변경
                imgV.setImageResource(R.drawable.flag_australia + num);
                num++;
                //다시 num을 0번으로
                if(num > 12) num=0;
            }
        });

    }

    //리스너를 class 영역(멤버영역)에 써서 메소드 안에서 공유할 수 있도록 한다
    //멤버변수가 돼야 메소드를 만들수 있으니까
    //메소드가 밑에 있어도 순서는 class멤버부터 읽음
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) { 
            //파라미터 view : 클릭을 한 버튼을 참조하는 참조변수이다!
            //파라미터 이름이 view인 이유
            //OnClickListener는 버튼만 눌릴 수 있는 게 아니라
            //최상위 view를 불러놓음

            //어떤 버튼을 클릭했는지 알아내기
            int id = view.getId();

            //이미지뷰에 이미지를 변경해보기
            if(id == R.id.btnAus) imgV.setImageResource(R.drawable.flag_australia);
            else if (id == R.id.btnBgi) imgV.setImageResource(R.drawable.flag_belgium);
            else if (id == R.id.btnKor) imgV.setImageResource(R.drawable.flag_korea);
            else imgV.setImageResource(R.drawable.flag_italy);

        }
    };


}

토이예제

  • 버튼 눌러서 사라지는 게임 만들어보기

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:weightSum="10"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="1 to 25"
        android:textSize="38sp"
        android:textColor="@color/black"
        android:gravity="center"
        android:textStyle="bold"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b01"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b02"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b03"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b04"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b05"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b06"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b07"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b08"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b09"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b10"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b11"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b12"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b13"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b14"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b15"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b16"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b17"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b18"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b19"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b20"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b21"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b22"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b23"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b24"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />

        <Button
            android:id="@+id/b25"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            />
    </LinearLayout>

    <TextView
        android:id="@+id/crrNum"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_gravity="center_horizontal"
        android:textStyle="italic"
        android:textColor="#AC291F"
        android:textSize="30sp"
        />

    <Button
        android:id="@+id/btn_retry"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="RETRY"
        android:backgroundTint="#BFBFBF"
        android:enabled="false"/>


</LinearLayout>

package com.bsj0420.tp03oneto25;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    //25칸짜리 배열
    Button[] btnArr = new Button[25];
    TextView crrNum;
    Button btnRetry;

    //현재 눌러야 할 번호
    int num = 1;

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

        crrNum = findViewById(R.id.crrNum);
        btnRetry = findViewById(R.id.btn_retry);

        //suffle 기능을 쓰기위해 list에 넣음
        ArrayList<Integer> listNum = new ArrayList<>();
        for(int i=1; i<=btnArr.length; i++) listNum.add(i);
        Collections.shuffle(listNum);

        //찾아오고 번호 넣고 리스너 달고
        for (int i=0; i<btnArr.length; i++) {
            btnArr[i] = findViewById(R.id.b01 + (i));

            btnArr[i].setText(listNum.get(i) + "");

            btnArr[i].setOnClickListener(listener);

            btnArr[i].setTag(listNum.get(i));
        }


        btnRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) { //view 클릭된 버튼 참조 변수 - 업캐스팅
            
            //클릭되 버튼에 있는 글씨가 넘버와 같은 지 비교
            
            //1. view로 자식 버튼글씨 가져오기
            //부모는 자식책체에 접근할 수 있지만 자식의 고유기능은 못씀 
            //getText 못함
//            Button btn = (Button)view; //다운캐스팅 -> 버튼의 고유기능 getText쓰기위해
//            String s = btn.getText().toString(); //btn은
//            int n = Integer.parseInt(s);

            //2. 버튼 뷰에 저장된 태그 글씨를 읽어와서 num과 같은지 비교
            String s =view.getTag().toString(); //getTag()는 객체로 오기 때문에 toString();
            int n = Integer.parseInt(s);

            //1.버튼 글씨로 가져오기
//            if(n == num) {
//                btn.setText("ok");
//                btn.setTextColor(0xFFFF0000); // ARGB : A 투명도 , 자바는 기본 00(완전 투명 FF 색)
//                btn.setBackground(null); //버튼은 배경이 그림임(그림을 없앤것임)
//
//                num++; //다음 번째로 가라
//
//                crrNum.setText(num + "");
//
//                if(num > 25 ) {
//                    crrNum.setText("Clear~~");
//                    btnRetry.setEnabled(true);
//                }
//            }

            //2. 태그로 가져오기
            if(n == num) {
                view.setVisibility(View.INVISIBLE);

                num++; //다음 번째로 가라

                crrNum.setText(num + "");

                if(num > 25 ) {
                    crrNum.setText("Clear~~");
                    btnRetry.setEnabled(true);
                }
            }

        }
    };

}

//모든 뷰는 별도의 숨져진 주머니가 있다
// Tag!!
// 이 태그에는 자료형, 객체 상관없이 아무거나 넣을 수 있다
profile
보조기억장치

0개의 댓글