andorid-비ui 라인으로 그리는 방법

안드로이드 UI 작업은 라인이 안전한 것이 아니라 UI 라인에서 실행해야 한다. 흔히 사용하는 방법은Handler를 이용하여 UI 라인의 업데이트를 실현하는 것이다. 그 본질은 ui 메인 라인을 이용하여 메시지를 보내고 다른 비 ui 라인에서 구체적인 그리기 작업을 하는 것이다.
사용 방법은 매우 간단하다. 아래의 예는 하나의handler의handle Message 복사 방법을 이용하여 메시지 대상이 보낸 메시지를 받아서 파란색 직선에서 굴러가는 원형을 그리는 것이다.
package com.test.surfaceview;

import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class TestsurfaceviewActivity extends Activity {
	private final static String TAG = "TestsurfaceviewActivity";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		onHanlderTest();
	}

	private final static int MSG_UPDATE_START  = 1;
	private final static int MSG_UPDATE_STOP   = 2;
	private final static int MSG_UPDATE_REGION = 3;
	private DrawViewer dv = null;
	
	private void onHanlderTest(){
		dv = new DrawViewer(this);
		new Thread(new myTestThread()).start(); 
		this.setContentView(dv); //       ,           DrawViewer  
	}
	
	private class DrawViewer extends View {
		private int center = 0;

		public DrawViewer(Context ctx) {
			super(ctx);
			this.center = 30;
		}

		@Override
		protected void onDraw(Canvas canvas){
			Log.i(TAG,"onDraw curr position = "+ center);
			center+= 10;
			if(center >= 1280) center = 0;
			Paint mPaint = new Paint();  
	        mPaint.setAntiAlias(true);  
	        mPaint.setColor(Color.RED); 
	        mPaint.setAlpha(0x80); 
	        canvas.drawCircle(center, 320, 40, mPaint);
	        mPaint.setColor(Color.BLUE);
	        canvas.drawLine(0, 360, 1280, 360, mPaint);
		}
	}
	
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) { 
			switch(msg.what){
			case MSG_UPDATE_START:
				Log.i(TAG,"revice MSG_UPDATE_START msg");
				break;
			case MSG_UPDATE_STOP:
				Log.i(TAG,"revice MSG_UPDATE_STOP msg");
				break;
			case MSG_UPDATE_REGION:
				Log.i(TAG,"revice MSG_UPDATE_REGION msg");
				/**
				 * Invalidate the whole view. If the view is visible, 
				 * onDraw(Canvas) will be called at some point in the future. 
				 * This must be called from a UI thread. To call from a non-UI 
				 * thread, call postInvalidate(). 
				 */
				dv.invalidate(); //        DrawViewer  onDraw()  
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	class myTestThread implements Runnable {
		private void sendMsg(int what) {
			Message message = new Message();
			message.what = what;
			myHandler.sendMessage(message);
		}

		public void run() {
			this.sendMsg(MSG_UPDATE_START);
			while (!Thread.currentThread().isInterrupted()) {
				try {
					this.sendMsg(MSG_UPDATE_REGION);
					Thread.sleep(500);
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				}
			}
			this.sendMsg(MSG_UPDATE_STOP);
		}
	}
}

handler를 사용하면 우리가 윈도우즈 플랫폼에서 코드를 쓰는 메시지 구동을 통한 프로그램 설계를 더욱 잘 사용할 수 있다. 비교적 이해하기 쉽고android는 다른 방법이 있다. 후속!

좋은 웹페이지 즐겨찾기