기술/beginning games2011. 5. 25. 11:56
download download
public class BitmapTest extends Activity {
class RenderView extends View {
Bitmap aosi;
Bitmap corea;
Rect dst = new Rect();
public RenderView(Context context) {
super(context);
try{
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("christmas.png");
aosi = BitmapFactory.decodeStream(is);
is.close();
Log.d("Aosi Test=","christmas.png 형식 : "+aosi.getConfig());
is = assetManager.open("widget.png");
BitmapFactory.Options options = new BitmapFactory.Options();
corea = BitmapFactory.decodeStream(is,null,options);
is.close();
Log.d("Aosi Test=","widget.png 형식 : "+corea.getConfig());
} catch(IOException e){
} finally {
}
}
protected void onDraw(Canvas c){
dst.set(50,50,350,350);
c.drawBitmap(aosi, null, dst,null);
c.drawBitmap(corea, 100, 100,null);
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   setContentView(new RenderView(this));
}

}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 11:32
download download



- 48*32 경우에 (width – 1, height – 1)을 해줘야 함

public class ShapeTest extends Activity {
class RenderView extends View {
Paint paint;
public RenderView(Context context) {
super(context);
paint = new Paint();
}
protected void onDraw(Canvas c){
c.drawRGB(255, 255, 255);
paint.setColor(Color.RED);
c.drawLine(0, 0, c.getWidth()-1, c.getHeight()-1, paint);
paint.setStyle(Style.STROKE);
paint.setColor(0xff00ff00);
c.drawCircle(c.getWidth()/2, c.getHeight()/2, 40, paint);
paint.setStyle(Style.FILL);
paint.setColor(0x770000ff);
c.drawRect(100, 100,200,200,paint);
invalidate();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   setContentView(new RenderView(this));
}
}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 11:19
download download
public class RenderViewTest extends Activity {
class RenderView extends View{
Random rand = new Random();
public RenderView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
canvas.drawRGB(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   setContentView(new RenderView(this));
}
}

download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 11:05
download download
퍼미션 추가 
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>


public class WakeLockTest extends Activity {
WakeLock wakeLock; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); 
   wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");         
super.onCreate(savedInstanceState);
}
@Override 
public void onResume() {
wakeLock.acquire(); 
super.onResume(); 
}
@Override 
public void onPause() { 
wakeLock.release(); 
super.onPause(); 
}
}

 자세한 설명은 아래 블로그에서 
http://blog.naver.com/heehow/140128105545 
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 10:59
download download
public class FullScreenTest extends SingleTouchTest {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);  //앱의 타이틀 바를 삭제
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 앱을 풀스크린으로 
   super.onCreate(savedInstanceState);
   // TODO Auto-generated method stub
}

}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 10:45
download download
public class MediaPlayerTest extends Activity {
MediaPlayer  mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TextView textView = new TextView(this);
   setContentView(textView);
   
   setVolumeControlStream(AudioManager.STREAM_MUSIC);
   mediaPlayer = new MediaPlayer();
   try{
    AssetManager assetManager = getAssets();
    AssetFileDescriptor des = assetManager.openFd("music.ogg");
    mediaPlayer.setDataSource(des.getFileDescriptor(),des.getStartOffset(),des.getLength());
    mediaPlayer.prepare();
    mediaPlayer.setLooping(true);
   } catch (IOException e){
    textView.setText("음악 파일을 읽을 수 없습니다."+e.getMessage());
    mediaPlayer = null;
   }
}
@Override
protected void onResume(){
super.onResume();
if(mediaPlayer != null){
mediaPlayer.start();
}
}
@Override
protected void onPause(){
super.onPause();
if(mediaPlayer != null){
mediaPlayer.pause();
if(isFinishing()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 10:33
download download
public class SoundPoolTest extends Activity implements OnTouchListener{
SoundPool soundPool;
int explosionId=-1;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TextView textView = new TextView(this);
   textView.setOnTouchListener(this);
   setContentView(textView);
   
   setVolumeControlStream(AudioManager.STREAM_MUSIC);
   soundPool = new SoundPool(20,AudioManager.STREAM_MUSIC,0);
   
   try{
    AssetManager assetManager = getAssets();
    AssetFileDescriptor des = assetManager.openFd("ex.ogg");
    explosionId = soundPool.load(des, 1);
   } catch(IOException e){
    textView.setText("사운드를 읽을 수 없습니다."+e.getMessage());
   }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(explosionId != -1){
soundPool.play(explosionId, 1, 1, 0, 0, 1);
}
}
return true;
}
}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 10:09
download download
외부 저장 장치를 사용하기 위하여 uses-permission을 추가
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

public class ExternalStorageTest extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TextView textView = new TextView(this); 
   setContentView(textView); 
   
   String state = Environment.getExternalStorageState();
   if(!state.equals(Environment.MEDIA_MOUNTED)){
    textView.setText("외부 저장 장치가 없습니다.");
   } else {
    File externalDir = Environment.getExternalStorageDirectory();
    File textFile = new File(externalDir.getAbsolutePath()+File.separator+"text.txt");
    try{
    writeTextFile(textFile,"테스트 입니다.");
    String text = readTextFile(textFile);
    textView.setText(text); 
    if(!textFile.delete()){
    textView.setText("임시파일을 삭제 할 수 없습니다.");
    }
    } catch(IOException e){
    textView.setText("에러:"+e.getMessage());
    }
   }
   
}
private void writeTextFile(File file, String text)throws IOException{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
private String readTextFile(File file)throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder text = new StringBuilder();
String line;
while((line=reader.readLine())!= null){
text.append(line);
text.append("\n");
}
reader.close();
return text.toString();
}

}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 25. 09:48
download download
public class AssetsTest extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TextView textView = new TextView(this);
   setContentView(textView);
   
   AssetManager assetManager = getAssets();
   InputStream is = null;
   
   try{
    is = assetManager.open("texts/mytext.txt");
    String text = loadTextFile(is);
    textView.setText(text);
   }catch(IOException e){
    textView.setText("파일을 가져올 수 없습니다.");
   }finally{
    if(is != null){
    try{
    is.close();
    } catch(IOException e){
    textView.setText("파일을 닫을 수 없습니다.");
    }
   
   }
}
public String loadTextFile(InputStream is)throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int len = 0;
while((len=is.read(bytes))>0)
byteStream.write(bytes,0,len);
return new String(byteStream.toByteArray(),"UTF8");
}

}
download download download
Posted by yachtie_leo
기술/beginning games2011. 5. 24. 18:59
download download
public class AccelerometerTest extends Activity implements SensorEventListener{

StringBuilder b = new StringBuilder();
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   textView = new TextView(this);
   setContentView(textView);
   
   SensorManager manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
   if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()==0){
    textView.setText("가속도센서가 설치 되지 않았습니다.");
   } else {
    Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    if(!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)){
    textView.setText("센서를 등록할 수 없습니다.");
    }
   }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
public void onSensorChanged(SensorEvent event) {
b.setLength(0);
b.append("x: ").append(event.values[0]);
b.append("y: ").append(event.values[1]);
b.append("z: ").append(event.values[2]);
textView.setText(b.toString());
}

}
download download download
Posted by yachtie_leo