[3] 두 개의 이미지 주고 받기.

2021. 12. 28. 15:46·안드로이드 with 자바/자잘한 문제들

1. 구현해야 할 것

 

 

 

 

2. java 소스코드

 

( 아래는 MainActivity.java의 코드이다. )

public class MainActivity extends AppCompatActivity {
    ImageView img1,img2;
    Button btn1, btn2;
    BitmapDrawable bd;

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

        img1 = findViewById(R.id.img1);
        img2 = findViewById(R.id.img2);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.INVISIBLE);
                setImage(img1);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img1.setVisibility(View.INVISIBLE);
                img2.setVisibility(View.VISIBLE);
                setImage(img2);
            }
        });
    }

    public void setImage(ImageView img){

        Resources res = getResources();
        bd = (BitmapDrawable) res.getDrawable(R.drawable.beach);
        int getWidth = bd.getIntrinsicWidth();
        int getHeight = bd.getIntrinsicHeight();

        img.setImageDrawable(bd);
        img.getLayoutParams().width = getWidth;
        img.getLayoutParams().height = getHeight;
    }
}

 

 

 

 

3. xml 소스코드

 

( 아래는 activity_main의 xml 코드이다. )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <HorizontalScrollView
        android:id="@+id/upLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnLayout"
        android:layout_alignParentTop="true">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            tools:ignore="SpeakableTextPresentCheck">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/beach" />
        </ScrollView>
    </HorizontalScrollView>

    <LinearLayout
        android:id="@+id/btnLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="up" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="down" />
    </LinearLayout>

    <HorizontalScrollView
        android:id="@+id/downLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnLayout"
        android:layout_alignParentBottom="true">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            tools:ignore="SpeakableTextPresentCheck">

            <ImageView
                android:id="@+id/img2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                app:srcCompat="@drawable/beach" />
        </ScrollView>
    </HorizontalScrollView>
</RelativeLayout>

 

 

 

 

< 이미지는 아래의 파일을 다운 >

beach.zip
0.30MB

 

'안드로이드 with 자바 > 자잘한 문제들' 카테고리의 다른 글

BitmapDrawable을 통한 스크롤 뷰 구현  (0) 2022.05.11
부분 레이아웃을 전체 레이아웃에 메모리에 객체화 시키기.  (0) 2022.01.11
Toast 직접 만들어 표시하기.  (0) 2022.01.06
[4] SMS 입력 화면 만들고 글자의 수 표시하기  (0) 2021.12.28
java 코드에서 화면 구성  (0) 2021.12.22
'안드로이드 with 자바/자잘한 문제들' 카테고리의 다른 글
  • 부분 레이아웃을 전체 레이아웃에 메모리에 객체화 시키기.
  • Toast 직접 만들어 표시하기.
  • [4] SMS 입력 화면 만들고 글자의 수 표시하기
  • java 코드에서 화면 구성
백_곰
백_곰
  • 백_곰
    친절한 코딩
    백_곰
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 알고리즘 (with JAVA)
        • 기본 알고리즘
        • 완전 탐색
        • 분할 정복 알고리즘
        • 동적 계획법
        • 탐욕법
        • 코딩 테스트 기출 문제
        • 코드트리 조별과제
      • 백준 (with JAVA)
        • 완전 탐색
        • 분할 정복
        • 그 외
      • 자바
        • 개발 환경 구축하기
        • 팁
        • 기본적인 개념
        • 컬렉션 프레임워크
        • 프로세스와 쓰레드
        • 지네릭스
        • 람다식
        • 스트림
        • 입출력 IO
        • 네트워킹
        • 열거형(enums)
        • java.lang 패키지
        • java.time 패키지
        • 유용한 클래스들
        • 형식화 클래스들
      • 안드로이드 with 자바
        • 응용 문제들
        • 자잘한 문제들
        • 오류 보고서
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      java.lang패키지
      안드로이드 스튜디오
      소켓 프로그래밍
      다형성
      TCP 소켓 프로그래밍
      코드트리
      불안정 정렬
      InputStream
      알고스팟
      ServerSocket
      안정 정렬
      역직렬화
      map()
      snail
      유용한 클래스
      문자 기반 스트림
      serializable
      outputstream
      java.time 패키지
      Collections Framework
      코딩트리조별과제
      람다식
      스트림
      선택 정렬
      코딩테스트
      제자리 정렬
      Arrays
      중간연산
      file
      자바 개념
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    백_곰
    [3] 두 개의 이미지 주고 받기.
    상단으로

    티스토리툴바