안드로이드

 

 

class MainActivity

 

package kr.co.braverokmc.myimageanimation;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

    ImageSwitcher imageSwitcher;
    int[] imageArray ={R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    ImageThread imageThread;

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

        imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher1);

        //이미지 바꾸기
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView =new ImageView(getApplicationContext());
                return imageView;
            }
        });


        //안드로이드에 내장된 애니메이션 적용
        Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation out=AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

        imageSwitcher.setInAnimation(in);
        imageSwitcher.setOutAnimation(out);

    }


    public void onClick(View v){

        switch (v.getId()){
            case R.id.button1:
                imageThread=new ImageThread();
                imageThread.start();

                Toast.makeText(getApplicationContext(), "시작", Toast.LENGTH_SHORT).show();
                break;

            case R.id.button2:
                if(imageThread!=null){
                    imageThread.halt();
                    imageThread=null;
                }
                Toast.makeText(getApplicationContext(), "중지", Toast.LENGTH_SHORT).show();
                break;
        }
    }




    class ImageThread extends Thread{

        boolean running =false;
        int index=0;

        @Override
        public void run() {
            running=true;

            while (running){

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        imageSwitcher.setImageResource(imageArray[index]);
                        imageSwitcher.invalidate();
                    }
                });

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                index++;
                if(index >= imageArray.length){
                    index=0;
                }


            }

        }

        public void halt(){
            running=false;
        }

    }



}

 

R.layout.activity_main


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kr.co.braverokmc.myimageanimation.MainActivity">

    <Button
        android:text="시작"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="119dp"
        android:layout_marginStart="119dp"
        android:onClick="onClick"
        android:id="@+id/button1"

        />

    <Button
        android:text="중지"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_alignEnd="@+id/button1"
        android:id="@+id/button2"
        android:onClick="onClick"
        />

    <ImageSwitcher
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2"
        android:id="@+id/imageSwitcher1" />

</RelativeLayout>

android

 

about author

PHRASE

Level 60  머나먼나라

궁극적인 최고의 객관적 가치는 이것을 주장할 수는 있어도 논증할 수는 없다. 독자적 가치란 논증해야 하는 것이지 주장해야 하는 것이 아니다. -게오르크 짐멜

댓글 ( 4)

댓글 남기기

작성