2011년 1월 14일 금요일

Sensor apply to LiveWallpaper (라이브월페이퍼에 센서 적용하기)

My LWP(Penguin, Pendulum, love sign, illusion window) applied Orientation Sensor. I introduce that way. It will be a low-grade courses because I do not understand and do exactly :)
First, the basic structure of live wallpapers should assume that you know.

저의 라이브월페이퍼 (펭귄, 펜더럼, 러브사인, 일루젼윈도우 )에는 오리엔테이션 센서가 적용되었습니다. 그 방법을 소개하고자 하는데, 저도 정학하게 이해하고 있는 것은 아니므로 허접한 튜토리얼 되겠네요;;
일단, 기본적인 라이브월페이퍼의 구조는 안다고 가정하고 시작합니다.



1. implement SensorEvent Listener & Variable declaration.
먼저 월페이퍼 엔진에 센서를 적용하겠다고 implements 하고 변수를 선언을 합니다.

class TestPatternEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener, SensorEventListener {
private SensorManager sensorManager; private Sensor sensor; private int kkk; private float xm, ym ; ... }

2. Sensor activation(?).
센서를 활성화 시킵니다(?).

TestPatternEngine() {

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

sensorManager.registerListener(this, sensor, kkk); 
...
}
3. Put the orientation sensor value to variables (xm, ym).
변수에 센서값을 담습니다.


values[2] is the horizontal tilting value -90~90
values[1] is the vertical tilting value -180~180


I don't know what doing onAccuracyChanged. But should be.
오리엔테이션 센서의 값을 변수에 담습니다. onAccuracyChanged 는 무엇에 쓰이는지 잘모르겠지만 없으면 에러가 납니다.



http://developer.android.com/reference/android/hardware/SensorEvent.html


public void onSensorChanged(SensorEvent event){
synchronized(this){
switch(event.sensor.getType()){
case Sensor.TYPE_ORIENTATION:


xm = (float)event.values[2];
ym = (float)event.values[1];


break;
}
}
}




@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub


}



4. Now let's apply a value. ( Describes the background portion is.)
이제 값을 적용합시다. (배경부분만..)
























canvas move to the middle of the window.
켄버스 중간으로 기준점을 이동합니다.

c.translate(xs,ys);

sensor value is too big. 1 / 20 size to take effect.
센서값이 너무 크므로 1/20만 적용합니다.

xx = (float) xm/20;
yy = (float) ym/20;

Turn the posted value is too large.  (when user lie down)
세로값 90도이상(뒤집어 졌을때) 값이 매우 크므로 조정해줍니다.

if (ym<=-90){ 
        yy = -(180+ym)/20; 
}else if(ym>=90 ){
        yy = (180-ym)/20;
} 

matrix setPolyToPoly apply.

http://developer.android.com/reference/android/graphics/Matrix.html

float[] src = { 0, 0, 64, 0, 64, 64, 0, 64 };
float[] dst = { 0, 0, 64+yy, xx, 64-yy, 64-xx, 0, 64 };


mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
c.concat(mMatrix);


canvas move to 0,0
켄버스 원위치

c.translate(-xs,-ys);


... background draw ...
이제 배경이미지를 그리면 됩니다~~


Is over how to apply :)
이상으로 적용 방법입니다.

댓글 없음:

댓글 쓰기