Programming2011. 10. 15. 03:04
you do not have to use thread,

However, if you want to use "Double-buffering" mechanism. at least you should know how to use Thread.
This code is really messed up though Sorry =)


package com.Circle;

import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.content.*;

public class CircleActivity extends Activity {
    /** Called when the activity is first created. */

    MyView vw;                    // Circle을 만들기 위한 객체 선언
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vw = new MyView(this);    // 객체 할당
        setContentView(vw);        // 이 객체를 보여주도록 한다.
       
    }
    public class MyView extends View{    // view클레스를 상속하는 MyView class
        int num = 0;                // 이 변수는 0부터 360까지 증가되는 변수이다
        Paint pnt = new Paint();    // paint 변수 선언
        RectF rf = new RectF(50,50,150,150);    // 타원을 넣기 위한 RectF 클레스 선언
        Thread th = new Thread(new ThreadForCircle());    // 실시간 작동을 위하여 thread를 생성하였다.
                                                // ThreadForCircle은 Runnable interface를 가진다.
        int color;                    // color를 저장하기 위한 변수 선언
       
       
        public MyView(Context context){
            super(context);        //생성자 부분
            color = Color.argb(0xFF, (int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
            // 랜덤한 색깔 생성
            pnt.setAntiAlias(true);    // AntiAlias모드를 on, 더욱 자연스럽다.
            th.start();                // thread를 시작한다.
        }
       
        public void onDraw(Canvas canvas){    // 실질적으로 원을 그리는 함수
            if(num > 360){            // 만약 원하나가 이미 그려졌다면
                color = Color.argb(0xFF, (int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
                // 색깔을 바꾸고
                num=0;
                // 처음부터 다시 그릴 준비를 한하.
            }
            else{
                pnt.setColor(color);    // paint 객체에 색깔을 지정한후
                canvas.drawArc(rf, 0, num, true, pnt);    // 부채꼴을 그린다. num값은 thread에 의해 바뀌며,
                                                        // thread는 onDraw를 호출하게 된다.
                Toast.makeText(CircleActivity.this, new String("hello"), Toast.LENGTH_SHORT).show();
            }
        }
        public int getNum(){            // 부채꼴의 현재 크기를 얻기 위한 함수
            return num;           
        }
       
        public void setNum(int _num){    // 부채꼴의 크기를 변경할 함수
            num = _num;
        }
    }


    public class ThreadForCircle implements Runnable{    // Runnable interface를 가지며
        int returnValue;                                // 변경될 크기가 저장될 변수
        public void run(){                                // thread생성 후에 무조건 run이 실행된다.
            while(true){                                // 반복문을 계속 돌린다.
                try {                                    // sleep을 위한 예외처리
                    Thread.sleep(50);                    // 50/1,000초 쉰다.
                    returnValue = vw.getNum() + 6;        // 6도씩 부채꼴이 증가하게 된ㄷ.
                    vw.setNum(returnValue);                // 이 값을 view의 객체에 넣어준다.
                    vw.postInvalidate();                // invalidate가 아닌데 그 이유는
                                                        // 현재 thread는 onDraw 함수가 없다.
                                                        // thread를 호출한 곳에서 invalidate하게 한다.
                } catch (InterruptedException e) {        // 예외 선언
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }         
            }
        }
    }
   
}

'Programming' 카테고리의 다른 글

Spring3.0 ajax를 이용한 json 주고밖기(@RequestBody)  (0) 2012.11.18
Open SSH Server  (0) 2012.10.08
File copy by using windows API  (0) 2011.08.31
Win32 API tutorial  (0) 2011.08.26
Posted by 박세범