andorid-비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는 다른 방법이 있다. 후속!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception in thread main java.lang. NoClassDefFoundError 오류 해결 방법즉,/home/hadoop/jarfile) 시스템은 Hello World 패키지 아래의class라는 클래스 파일을 실행하고 있다고 오인하여 시스템의 CLASSPATH 아래 (일반적으로 현재 디렉터리를 포함) Hell...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.