Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- JSP
- AWT
- 예외처리
- 배열
- 이클립스
- struts2
- 에러페이지
- Menu
- OGNL
- 메소드
- 메서드
- Android
- 클래스
- 어노테이션
- JavaScript
- 기본
- Java
- 생성자
- paint
- Eclips
- Spring
- layout
- mybatis
- HTML
- 국제화
- oracle
- 전화걸기
- 오버로딩
- Graphic
- 안드로이드
Archives
- Today
- Total
note
Android surface(마우스에 이미지 따라다니기) 본문
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
MainAct
package aa.surface_ex1;
import android.app.Activity;
import android.os.Bundle;
public class MainAct extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MySurfaceView(this));
}
}
MySurfaceView
package aa.surface_ex1;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.Canvas;
import android.view.SurfaceHolder.Callback;
/*
View 에 동영상 또는 카메라 프리뷰와 같이 빠른 화면 변화 또는
그러지는 양이 많을 경우 SurfaceView를 사용해 처리
Surface 는 그래픽 버퍼
Surfaceview 에서 화면 제어를 하기 위해 SurfaceHolder 생성
=-=-=-=--=-=-=-=-=-=-==-=-=-=
Surface View
=-=-=-=--=-=-=-=-=-=-==-=-=-=
Surface
=-=-=-=--=-=-=-=-=-=-==-=-=-=
SurfaceHolder
SurfaceHolder를 이용해서 surface(버퍼)에 그림을 그리면 SurfaceView에 반영
*/
public class MySurfaceView extends SurfaceView{
Bitmap imgMove;
int moveX,moveY,imgW,imgH;
Thread thread;
Point pImage;
boolean bMove = true;
SurfaceHolder holder; //canvas의 주체
public MySurfaceView(Context context) {
super(context);
/*SurfaceHolder 주요 메소드
addCallBack(); 구현한 Callback 객체 등록
lookccanvas() : Surface를 잠그고 그리기 위한
Canvas를 반환
unlockCanvasAndPost() : Canvas lock을 풀고 현재 이미지를 렌더링
*/
holder = getHolder(); //SurfaceHolder 객체 반환
holder.addCallback(callback);
setFocusable(true);
}
/*Surface에 변동사항을 전달할 객체
Callback Interface는 SurfaceHolder를 통해 작성한
Surface와 SurfaceView를 연결하기 위해서 Surfcae의 생성,변경 종료에 대한 이벤트 처리
*/
private SurfaceHolder.Callback callback = new Callback(){
//Surface가 파괴되었을때
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("MySurfaceView", "표면 파괴");
bMove = false;
thread = null;
}
public void surfaceCreated(SurfaceHolder holder){
Log.i("MySurfaceView", "표면 생성");
pImage = new Point(0,0);
Resources resources = getResources();
Bitmap temp_bitmap = BitmapFactory.decodeResource(resources, R.drawable.popeye);
imgW = 150;
imgH = 180;
moveX = 200;
moveY = 200;
imgMove = Bitmap.createScaledBitmap(temp_bitmap,imgW,imgW,false);
setClickable(true);
thread = new Thread(runnable);
thread.start();
}
//Surface가 변경되었을때
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
Log.i("MySurfaceView", "표면 변경");
}
};
private Runnable runnable = new Runnable() {
public void run() {
while(bMove){
Canvas c = null;
try {
c = holder.lockCanvas();
synchronized (holder) {
doDraw(c);
}
} catch (Exception e) {
Log.i("disp", "err : " + e.getMessage());
} finally{
if(c != null) holder.unlockCanvasAndPost(c);
}
}
}
};
private void doDraw(Canvas canvas){
pImage.x = moveX;
pImage.y = moveY;
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(imgMove, pImage.x,pImage.y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
//touch 지점의 좌표
int x = (int)event.getX();
int y = (int)event.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
moveX = x;
moveY = y;
break;
}
return super.onTouchEvent(event);
}
}
'Android > 기본' 카테고리의 다른 글
| 안드로이드 - 자료관리 (0) | 2012.02.17 |
|---|---|
| Android 메인로딩 페이지 만들기 (2) | 2012.02.01 |
| Android Video View (동영상 재생) (0) | 2012.02.01 |
| Android Audio사용 (음악 재생) (0) | 2012.02.01 |
| Android Constants Provider (0) | 2012.02.01 |