package com.example.my_touch1;
import ....
public class MainActivity extends Activity {
LinearLayout layout;
TextView msg;
private StringBuilder data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(LinearLayout)findViewById(R.id.rootlayout);
msg=(TextView)findViewById(R.id.textView1);
data=new StringBuilder();
//註冊觸控監聽物件
layout.setOnTouchListener(new MytouchListener());
}
//觸控監聽物件之類別,需實作OnTouchListener
private class MytouchListener implements OnTouchListener{
//實作OnTouchListener所需實作之方法
public boolean onTouch(View view, MotionEvent event) {
// 判斷觸控事件
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
data.delete(0, data.length());
msg.setText(data.append("Action down"+"("+event.getX()+","+event.getY()+")"+"\n"));
break;
case MotionEvent.ACTION_UP:
msg.setText(data.append("Action up"+"("+event.getX()+","+event.getY()+")"+"\n"));
break;
case MotionEvent.ACTION_MOVE:
msg.setText(data.append("Action move"+"("+event.getX()+","+event.getY()+")"+"\n"));
break;
}
//取得多點觸控的點數
int pCount=event.getPointerCount();
data.append("多點觸控點數為:"+pCount+"\n");
for(int i=0;i<pCount;i++){
//(event.getX(0),event.getY(0))代表第一點觸控點之座標
data.append(String.format("觸控點%d:(%.2f,%.2f)%n",
event.getPointerId(i),event.getX(i),event.getY(i)));
}
msg.setText(data);
return true;
}
}
}
沒有留言:
張貼留言