투명 윈도우

Android 2012. 12. 27. 22:32 |
package co.kr.codein;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.Window;
import android.widget.AbsoluteLayout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;

public class SurfaceViewTest extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSPARENT);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(new MySurfaceView(this));
addContentView(new SampleView(this), new ViewGroup.LayoutParams(-1, -1));
}

public class SampleView extends AbsoluteLayout {
private DisplayObject mKirby;

public SampleView(Context context) {
super(context);

mKirby = new DisplayObject(context);
this.addView(mKirby);

Bitmap bmp = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.kirby_t);
mKirby.setBitmap(bmp);
mKirby.move((320 - mKirby.getImageWidth())/2, (240 - mKirby.getImageHeight())/2);
}

@Override
public void onDraw(Canvas canvas) {
mKirby.draw(canvas);
}

@Override
public boolean onMotionEvent(MotionEvent event) {
switch( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int x = (int)(event.getX() - mKirby.getImageWidth()/2);
int y = (int)(event.getY() - mKirby.getImageHeight()/2);
mKirby.move(x, y);
return true;
}
return true;
}
}

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;

public MySurfaceView(Context context) {
super(context);

mHolder = getHolder();
mHolder.setCallback(this);
}

public boolean surfaceCreated(SurfaceHolder holder) {
return true;
}

public void surfaceDestroyed(SurfaceHolder holder) {
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
}
}
}
 

'Android' 카테고리의 다른 글

이클립스 이유없이 실행이 안될때  (0) 2013.10.31
카메라 제어  (0) 2012.12.27
UI Thread 사용예제  (0) 2012.12.27
WebView 사용예 (loadUrl, loadData)  (0) 2012.12.27
Posted by maysent
:

카메라 제어

Android 2012. 12. 27. 22:32 |

CameraEx.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

//카메라의 제어
public class CameraEx extends Activity {
//애플리케이션 초기화
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CameraView(this));
}
}


CameraView.java

import android.content.Context;
import android.hardware.Camera;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.FileOutputStream;

//카메라의 제어
public class CameraView extends SurfaceView
implements SurfaceHolder.Callback {
private SurfaceHolder holder;//홀더
private Camera camera;//카메라

//생성자
public CameraView(Context context) {
super(context);

// SurfaceHolder 생성
holder=getHolder();
holder.addCallback(this);

// Push Buffer 지정
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

//Surface 생성시 호출
public void surfaceCreated(SurfaceHolder holder) {
//카메라의 초기화
try {
camera=Camera.open();
camera.setPreviewDisplay(holder);
} catch (Exception e) {
}
}

//Surface 변경 이벤트 처리
public void surfaceChanged(SurfaceHolder holder,int format,int w,int h) {
// 카메라의 리뷰 개시
Camera.Parameters parameters=camera.getParameters();
parameters.setPreviewSize(w,h);
camera.setParameters(parameters);
camera.startPreview();
}

//Surface 해제 이벤트 처리
public void surfaceDestroyed(SurfaceHolder holder) {
//카메라 프리뷰의 개시
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera=null;
}
//터치 이벤트 처
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
takePicture();
camera.startPreview();
}
return true;
}

//사진 촬영
public void takePicture() {
//카메라의 스크린샷 취득
camera.takePicture(null,null,new Camera.PictureCallback() {
public void onPictureTaken(byte[] data,Camera camera) {
try {
data2sd(getContext(),data,"test.jpg");
} catch (Exception e) {
android.util.Log.e("",""+e.toString());
}
}
});
}

//바이트 데이터 -> SD 카드
private static void data2sd(Context context,
byte[] w,String fileName) throws Exception {
//SD카드의 데이터 저장
FileOutputStream fos=null;
try {
fos=new FileOutputStream("/sdcard/"+fileName);
fos.write(w);
fos.close();
} catch (Exception e) {
if (fos!=null) fos.close();
throw e;
}
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.npaka.cameraex"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CameraEx"
android:label="@string/app_name" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
</manifest>

'Android' 카테고리의 다른 글

이클립스 이유없이 실행이 안될때  (0) 2013.10.31
투명 윈도우  (0) 2012.12.27
UI Thread 사용예제  (0) 2012.12.27
WebView 사용예 (loadUrl, loadData)  (0) 2012.12.27
Posted by maysent
:

UI Thread 사용예제

Android 2012. 12. 27. 22:31 |

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class ExamThread extends Activity implements Runnable {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Thread thread = new Thread(this);
thread.start();
}

public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
// Update 메시치 보냄
handler.sendMessage(Message.obtain(handler, UPDATE));
}
}
public final static int UPDATE = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case UPDATE:
// 메시지 수신
break;
}
};
};
}

'Android' 카테고리의 다른 글

이클립스 이유없이 실행이 안될때  (0) 2013.10.31
투명 윈도우  (0) 2012.12.27
카메라 제어  (0) 2012.12.27
WebView 사용예 (loadUrl, loadData)  (0) 2012.12.27
Posted by maysent
: