프로그래밍

[Do it! 안드로이드 앱] 자바) 두 개의 이미지뷰에 이미지 번갈아 보여주기 본문

안드로이드/Do it! 안드로이드 앱프로그래밍

[Do it! 안드로이드 앱] 자바) 두 개의 이미지뷰에 이미지 번갈아 보여주기

시케 2022. 5. 16. 16:03
728x90
반응형

도전과제 03

 

결과화면

 

디자인

 

프로젝트 설명

두 개의 이미지뷰룰 한 화면에 보여주고 하나의 이미지를 두 개의 이미지뷰에서 번갈아서 보여준다

뷰 크기보다 이미지가 클시 수직/수평 스크롤 사용이 가능하다

 

 

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:orientation="vertical"
    tools:context=".MainActivity">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            tools:context=".MainActivity">
            <ImageView
                android:id="@+id/ImgV1"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:src="@drawable/rm1"/>
        </ScrollView>
    </HorizontalScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="▲"
            android:id="@+id/BtnUp"
            android:layout_gravity="center"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="▼"
            android:id="@+id/BtnDown"
            android:layout_gravity="center" />
    </LinearLayout>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            tools:context=".MainActivity">
            <ImageView
                android:id="@+id/ImgV2"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:src="@drawable/rm1" />
        </ScrollView>
    </HorizontalScrollView>
</LinearLayout>

 

java

package com.sample.doitmission_03;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    ImageView imgV1, imgV2;
    Button btnUp, btnDown;

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

        imgV1 = findViewById(R.id.ImgV1);
        imgV2 = findViewById(R.id.ImgV2);
        btnUp = findViewById(R.id.BtnUp);
        btnDown = findViewById(R.id.BtnDown);

        imgV1.setVisibility(View.VISIBLE);
        imgV2.setVisibility(View.INVISIBLE);

        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgV1.setVisibility(View.VISIBLE);
                imgV2.setVisibility(View.INVISIBLE);
            }
        });

        btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgV2.setVisibility(View.VISIBLE);
                imgV1.setVisibility(View.INVISIBLE);
            }
        });
    }
}
728x90
반응형
Comments