설.현.아빠
멀티터치 예제 본문
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.OnTouchListener;
import android.widget.*;
public class MultiTouchTest extends Activity implements OnTouchListener
{
StringBuilder builder = new StringBuilder();
TextView textView;
float[] x = new float[10];
float[] y = new float[10];
boolean[] touched = new boolean[10];
private void updateTextView()
{
builder.setLength(0);
for(int i=0; i<10; i++)
{
builder.append(touched[i]);
builder.append(", ");
builder.append(x[i]);
builder.append(", ");
builder.append(y[i]);
builder.append("\n");
}
textView.setText(builder.toString());
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Touch and drag(multiple fingers supported)!");
textView.setOnTouchListener(this);
setContentView(textView);
}
@Override
public boolean onTouch(View view, MotionEvent event)
{
int action = event.getAction() & MotionEvent.ACTION_MASK;
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK);
pointerIndex = pointerIndex >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int pointerId = event.getPointerId(pointerIndex);
switch(action)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
touched[pointerId] = true;
x[pointerId] = (int)event.getX(pointerIndex);
x[pointerId] = (int)event.getY(pointerIndex);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
touched[pointerId] = false;
x[pointerId] = (int)event.getX(pointerIndex);
x[pointerId] = (int)event.getY(pointerIndex);
break;
case MotionEvent.ACTION_MOVE:
int pointerCount = event.getPointerCount();
for(int i=0; i<pointerCount; i++)
{
pointerIndex = i;
pointerId = event.getPointerId(pointerIndex);
x[pointerId] = (int)event.getX(pointerIndex);
y[pointerId] = (int)event.getY(pointerIndex);
}
break;
}
updateTextView();
return true;
}
}
'안드로이드 > Etc.' 카테고리의 다른 글
안드로이드 화면을 노트북 모니터에 출력하기. (0) | 2012.03.06 |
---|---|
안드로이드 개발자를 위한 10대 오픈소스 안드로이드(2D,3D)게임엔진개발관련 (0) | 2012.01.17 |
안드로이드 단말별 비중 통계 (0) | 2012.01.06 |
Back 두번눌러서 종료시키기 (0) | 2011.10.04 |
설치된 어플 목록 PackageInfo를 통해 가져오기 & 다른어플 실행 시키기 (1) | 2011.09.07 |