본문 바로가기

애니리뷰

직접풀어보기 6-2)

package com.example.helloandroid;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
Button btnPrev,btnNext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("뷰플리퍼를 이용한 자동 사진보기 앱");

final ViewFlipper vf;
btnPrev=(Button)findViewById(R.id.btnPrev);
btnNext=(Button)findViewById(R.id.btnNext);
vf=(ViewFlipper)findViewById(R.id.viewFil1);
ImageView a1=(ImageView)findViewById(R.id.chinhan);
ImageView a2=(ImageView)findViewById(R.id.chiwawa);
ImageView a3=(ImageView)findViewById(R.id.mamang);

//사진 보기가 눌렸다면, 100단위로 flip
btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vf.setFlipInterval(1000); //1000밀리초(1)간격으로 flip set(넘김)
vf.startFlipping(); //위의 1밀리초를 인터버로, 지나면 넘김
}
});

btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vf.stopFlipping();
}
});
}
}

///////////////

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

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="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnPrev"
android:text="사진보기 시작"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnNext"
android:text="사진보기 정지"/>
</LinearLayout>

<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewFil1">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/chinhan"
android:id="@+id/chinhan"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/chiwawa"
android:id="@+id/chiwawa"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/mamang"
android:id="@+id/mamang"/>
</ViewFlipper>

</LinearLayout>