mojo's Blog

안드로이드 프로그래밍 제 10장 연습문제 6번 본문

Android

안드로이드 프로그래밍 제 10장 연습문제 6번

_mojo_ 2021. 9. 4. 17:49

문제 6번 ) 투표한 결과 화면이 1등부터 1초마다 뷰플리퍼에 자동으로 나오도록 프로젝트를 구현하기

 

activity_main.xml Code

 

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="3">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv1"
            android:src="@drawable/apple"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv2"
            android:src="@drawable/bear"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv3"
            android:src="@drawable/cherry"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="3">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv4"
            android:src="@drawable/rabbit"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv5"
            android:src="@drawable/lion"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv6"
            android:src="@drawable/cat"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="3">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv7"
            android:src="@drawable/dog"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv8"
            android:src="@drawable/kitty"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv9"
            android:src="@drawable/oreo"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/btnNext"
            android:text="투표 종료"/>
    </LinearLayout>

</LinearLayout>

 

second.xml Code

 

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="자동보기 시작"
            android:id="@+id/btnStart"
            android:layout_weight="1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="자동보기 정지"
            android:id="@+id/btnEnd"
            android:layout_weight="1"/>
    </LinearLayout>

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewFlipper">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView1"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView2"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView3"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView4"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView5"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView6"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView7"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView8"
                android:scaleType="fitCenter" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="350dp"
                android:layout_height="350dp"
                android:id="@+id/imgView9"
                android:scaleType="fitCenter" />
        </LinearLayout>
    </ViewFlipper>

</LinearLayout>

 

MainActivity.java Code

 

public class MainActivity extends AppCompatActivity {
    Button btn;
    ImageView img[] = new ImageView[9];
    Integer imageId[] = {R.id.iv1, R.id.iv2, R.id.iv3,
            R.id.iv4, R.id.iv5, R.id.iv6,
            R.id.iv7, R.id.iv8, R.id.iv9};
    final String imageName[] = {"사과", "배", "체리",
            "토끼", "라이언", "고양이",
            "개","키티","오레오"};
    final int voteCount[] = {0, 0, 0,
            0, 0, 0,
            0, 0, 0};
    final int imgSource[] = {R.drawable.apple, R.drawable.bear, R.drawable.cherry,
            R.drawable.rabbit, R.drawable.lion, R.drawable.cat,
            R.drawable.dog, R.drawable.kitty, R.drawable.oreo};
    int maxCnt = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("연습하기");

        for(int i=0; i<imageId.length; i++){
            img[i] = (ImageView)findViewById(imageId[i]);
            final int index = i;
            img[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    maxCnt = Math.max(maxCnt , ++voteCount[index]);
                    Toast.makeText(getApplicationContext(), imageName[index]+": 총 "+voteCount[index]+" 표",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }

        btn = (Button)findViewById(R.id.btnNext);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                int cnt = maxCnt, idx = 0;
                int id[] = new int[9];
                while(cnt >= 0) {
                    for(int i=0; i<imageId.length; i++){
                        if(voteCount[i] == cnt){
                            img[i].setImageResource(R.drawable.apple);
                            id[idx++] = imgSource[i];
                        }
                    }
                    cnt--;
                }

                intent.putExtra("ImageId", id);
                startActivity(intent);
            }
        });
    }
}

 

SecondActivitiy.java Code

 

public class SecondActivity extends AppCompatActivity {
    Button btn1, btn2;
    ImageView []imgArray = new ImageView[9];
    int imgId[] = { R.id.imgView1, R.id.imgView2, R.id.imgView3,
                    R.id.imgView4, R.id.imgView5, R.id.imgView6,
                    R.id.imgView7, R.id.imgView8, R.id.imgView9,};

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        setTitle("연습");

        Intent inIntent = getIntent();
        int getId[] = inIntent.getIntArrayExtra("ImageId");

        for(int i=0; i<imgArray.length; i++){
            imgArray[i] = (ImageView)findViewById(imgId[i]);
            imgArray[i].setImageResource(getId[i]);
            android.util.Log.i("액티비티 테스트", Integer.toString(getId[i]));
        }

        final ViewFlipper vFlipper;
        vFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        vFlipper.setFlipInterval(1000);

        btn1 = (Button)findViewById(R.id.btnStart);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.startFlipping();
            }
        });

        btn2 = (Button)findViewById(R.id.btnEnd);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.stopFlipping();
            }
        });
    }
}

 

 

 

'Android' 카테고리의 다른 글

갤러리와 스피너  (0) 2021.09.05
리스트뷰와 그리드뷰  (0) 2021.09.04
액티비티와 인텐트 응용  (0) 2021.09.04
액티비티와 인텐트의 기본  (0) 2021.09.04
안드로이드 프로그래밍 제 9장 연습문제 (5, 6번)  (0) 2021.09.02
Comments