▶▶ Sensor를 이용해 도형을 움직여보자
카메라 기울기에 따라 도형이 움직이는 모습. 가속도 센서를 사용하였다.
| |||
public class MyView extends View {
private Drawable image;
private int viewWidth;
private int viewHeight;
private int imageWidth;
private int imageHeight;
private float x;
private float y;
public MyView(Context context, AttributeSet attrs) { //이미지 불러오기
super(context, attrs);
image = this.getResources().getDrawable(R.drawable.one);
}
public void move(float mx, float my) { //화면의 가
this.x -= (mx * 4f);
this.y += (my * 4f);
Log.i("viewHeight =", viewHeight + "");
if (this.x < 0) {
this.x = 0;
} else if ((this.x + imageWidth) > this.viewWidth) {
this.x = this.viewWidth - imageWidth;
}
if (this.y < 0) {
this.y = 0;
} else if ((this.y + imageHeight) > this.viewHeight) {
this.y = this.viewHeight - imageHeight;
}
Log.i("SENSOR", "movie-[this.x]" + x + "[this.y]" + y + "[mx]" + mx
+ "[my]" + my);
this.invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { //화면을 보여줄 view의 너비를 구해줌.
this.viewWidth = this.getWidth();
this.viewHeight = this.getHeight();
imageWidth = image.getIntrinsicWidth();
imageHeight = image.getIntrinsicHeight();
super.onSizeChanged(w, h, oldw, oldh); //부모클래스에 있는 메서드를 불러 줌.
}
@Override
protected void onDraw(Canvas canvas) { //이미지를 그려줌.
| |||
public class MainActivity extends Activity {
| |||
'정보기술, IT > IT source' 카테고리의 다른 글
Oracle 설치와 기본 개념정리 (0) | 2013.04.08 |
---|---|
Java 피라미드 형식 출력하기. (0) | 2013.04.06 |
Java 실행주기와 파일실행 흐름을 알아보자. (0) | 2013.04.06 |
구글이 지원하는 페이스북 폰의 운명은? (0) | 2013.04.06 |
Ajax를 위한 Tomcat 설치. (0) | 2013.04.05 |