본문 바로가기
App/Android Studio

Android Studio - ImageView Shuffle 실습

by 코젼 2022. 8. 4.
728x90
반응형

2022-08-04(33일차)


💁‍♀️오늘의 배운 점

◾ ImageView의 인스턴스 주소를 저장한다.

ImageView iv = findViewById(R.id.iv);

◾ 리소스 id를 저장한다.

int img = R.drawable.oo1;

📃Shuffle버튼을 누르면 사진을 섞도록 하시오.


📂이미지 파일 및 xml, java파일

oo1.jpg
0.01MB
oo2.jpg
0.01MB
oo3.jpg
0.01MB
oo4.jpg
0.01MB
oo5.jpg
0.01MB
oo6.jpg
0.01MB


MainActivity.java
0.00MB


layout.xml
0.00MB


📝layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <Button
        android:id="@+id/btn_shuffle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:backgroundTint="#ff9999"
        android:text="Shuffle"
        android:textSize="40dp" />

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

        <ImageView
            android:id="@+id/iv_01"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/iv_02"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/iv_03"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

    </LinearLayout>

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

        <ImageView
            android:id="@+id/iv_04"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/iv_05"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/iv_06"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_margin="5dp"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

📝MainActivity.java

package com.example.shuffleimage;

import androidx.appcompat.app.AppCompatActivity;

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

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ImageView img_arr[];

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

        int Rid_iv[] = {R.id.iv_01, R.id.iv_02, R.id.iv_03,
                R.id.iv_04, R.id.iv_05, R.id.iv_06};

        img_arr = new ImageView[Rid_iv.length];
        for (int i = 0; i < img_arr.length; i++) {
            img_arr[i] = (ImageView) findViewById(Rid_iv[i]);
        }

        int img[] = {R.drawable.oo1, R.drawable.oo2, R.drawable.oo3,
                R.drawable.oo4, R.drawable.oo5, R.drawable.oo6};

        for (int i = 0; i < img_arr.length; i++) {
            img_arr[i].setBackgroundResource(img[i]);
        }

        findViewById(R.id.btn_shuffle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int temp1, temp2;
                for (int i = 0; i < img_arr.length*3; i++) {
                        temp1 = (int)(Math.random()*6);
                        temp2 = (int)(Math.random()*6);

                        int temp = img[temp1];
                        img[temp1] = img[temp2];
                        img[temp2] = temp;
                }

                for(int i=0; i< img_arr.length; i++) {
                    img_arr[i].setBackgroundResource(img[i]);
                }
            }
        });
    }
}

 

728x90
반응형

댓글