안드로이드

 

 

class MainActivity

package kr.co.dothome.braverokmc.mythreadanimation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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


    }
}

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.dothome.braverokmc.mythreadanimation.MainActivity">

    <kr.co.dothome.braverokmc.mythreadanimation.ThreadAnimationView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/imageView"
        android:layout_marginBottom="117dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

 

class ThreadAnimationView 

package kr.co.dothome.braverokmc.mythreadanimation;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by choi on 2017-04-13.
 */

public class ThreadAnimationView extends ImageView {

    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);
        }
    };


    public ThreadAnimationView(Context context) {
        super(context);
        init(context);
    }

    public ThreadAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    private void init(Context context){

        ImageThread thread =new ImageThread();
        thread.start();

    }



    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() {
                        setImageResource(imageArray[index]);
                        invalidate();
                    }
                });

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

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


            }

        }



    }





}

 

 

 

 

 

 

87강

 

 

88강

 

android

 

about author

PHRASE

Level 60  머나먼나라

학문을 그만두면 안 된다. 쪽에서 나온 푸른 물감이 쪽보다 푸르고, 물에서 나온 얼음은 물보다도 차갑다. -순자

댓글 ( 4)

댓글 남기기

작성