안드로이드 with 자바/자잘한 문제들

BitmapDrawable을 통한 스크롤 뷰 구현

백_곰 2022. 5. 11. 16:54

1. 구현해야 할 것

 

 

 

 

2. 제약조건

(1) imageView 하나를 써서 BitmapDrawable로 구현할 것.

(2) 수평으로 스크롤 뷰를 만들 것.

(3) 버튼을 누를 경우, 위와 같은 이미지가 나오며 수평으로 스크롤이 가능해야 함.

(4) 다시 버튼을 누를 경우, 다른 이미지가 등장하게 됨.

 

( 또 버튼을 누르면 다시 원래 첫 이미지로 돌아가게 되면서 무한 반복함. )

 

 

 

3. java 소스코드

 

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

package com.example.exercise001;

import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    ImageView img1;
    Button btn1;
    BitmapDrawable bitmap;
    int i=0;

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

        img1 = findViewById(R.id.iv1);

        btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i==0){
                    i=1;
                    Resources res = getResources();
                    bitmap = (BitmapDrawable) res.getDrawable(R.drawable.image01);
                    int width = bitmap.getIntrinsicWidth();
                    int height = bitmap.getIntrinsicHeight();
                    img1.setImageDrawable(bitmap);
                    img1.getLayoutParams().width = width;
                    img1.getLayoutParams().height = height;
                }else if(i==1){
                    i=0;
                    Resources res = getResources();
                    bitmap = (BitmapDrawable) res.getDrawable(R.drawable.image02);
                    int width = bitmap.getIntrinsicWidth();
                    int height = bitmap.getIntrinsicHeight();
                    img1.setImageDrawable(bitmap);
                    img1.getLayoutParams().width = width;
                    img1.getLayoutParams().height = height;
                }

            }
        });
    }
}

 

 

 

 

4. xml 소스코드

 

( 아래는 activity_main.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">


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

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

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

            <ImageView
                android:id="@+id/iv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

 

 

 

 

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

image.zip
0.34MB