본문 바로가기
App Programming/Android Studio

[Android Studio] 버튼 클릭시 이미지 변경

by goatlab 2022. 11. 4.
728x90
반응형
SMALL

버튼 클릭시 이미지 변경

 

2개의 이미지 파일을 drawable에 추가하여 진행한다.

 

image (1)

 

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="104dp"
    android:height="103dp"
    android:viewportWidth="104"
    android:viewportHeight="103">
  <path
      android:pathData="M47.41,39.18L47.41,39.18A3.41,3.41 0,0 1,50.82 42.59L50.82,60.69A3.41,3.41 0,0 1,47.41 64.1L47.41,64.1A3.41,3.41 0,0 1,44 60.69L44,42.59A3.41,3.41 0,0 1,47.41 39.18z"
      android:fillColor="#3F414E"/>
  <path
      android:pathData="M60.57,39.18L60.57,39.18A3.41,3.41 0,0 1,63.98 42.59L63.98,60.69A3.41,3.41 0,0 1,60.57 64.1L60.57,64.1A3.41,3.41 0,0 1,57.16 60.69L57.16,42.59A3.41,3.41 0,0 1,60.57 39.18z"
      android:fillColor="#3F414E"/>
</vector>

 

image (2)

 

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="102dp"
    android:height="102dp"
    android:viewportWidth="27"
    android:viewportHeight="27">
    <path
        android:pathData="M13.5,13.5m-13.5,0a13.5,13.5 0,1 1,27 0a13.5,13.5 0,1 1,-27 0"
        android:fillColor="#E6E7F2"/>
    <path
        android:pathData="M16.588,12.358L11.719,9.591C11.01,9.19 10.125,9.692 10.125,10.495V16.032C10.125,16.839 11.01,17.342 11.719,16.937L16.588,14.17C17.297,13.768 17.297,12.763 16.588,12.358Z"
        android:fillColor="#012600"/>
</vector>

 

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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/stop" />
 
    <Button
        android:id="@+id/bnt_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_weight="0.5"
        android:text="이미지 바꾸기"
        android:textSize="30dp" />
 
</LinearLayout>

 

java

 

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 {
    Button m_btn;
    ImageView imageView;
    boolean i = true;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        m_btn = findViewById(R.id.bnt_image);
        imageView = findViewById(R.id.image_view);
 
        m_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i == false){
                    imageView.setImageResource(R.drawable.play);
                    i = true;
                }else {
                    imageView.setImageResource(R.drawable.stop);
                    i = false;
                }
            }
        });
 
    }
}

728x90
반응형
LIST