D-PAD按键处理以及拖动--onKeyDown、onTouch 事件与Layout坐标交互
just
posted @ 2011年9月10日 13:01
in android
, 2541 阅读
当用户点击D-pad上的上、下、左、右四个方向键或者拖动 test按钮时,会直接更改在Layout上的绝对坐标
效果如下:
public class PadActivity extends Activity { /** Called when the activity is first created. */ private Button mButton; private int intWidth,intHeight,intButtonX,intButtonY; //存储屏幕的分辨率 private int intScreenX,intScreenY; //按钮位移的平移量 private int intShift=2; private int currX,currY;//当前坐标 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton=(Button) findViewById(R.id.mButton); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); intScreenX=dm.widthPixels; intScreenY=dm.heightPixels; //定义按钮的宽高 intWidth=80; intHeight=80; //初始化按钮剧中 RestoreButton(); mButton.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //取得当前坐标 currX = (int) event.getX(); currY = (int) event.getY(); //System.out.println("---"+currX+" "+currY); break; // 按钮跟焦点移动 case MotionEvent.ACTION_MOVE: int dx=((int) event.getX())-currX; int dy=((int) event.getY())-currY; intButtonX+=dx; intButtonY+=dy; if (intButtonX<0) { intButtonX=0; } if (intButtonX>(intScreenX-intWidth)) { intButtonX=intScreenX-intWidth; } if (intButtonY<0) { intButtonY=0; } if (intScreenY<(intButtonY+intHeight)) { intButtonY=intScreenY-intHeight; } //System.out.println("--intButtonY "+intButtonY +" intScreenY "+intScreenY+" intHeight "+intHeight); setButtonPos(intButtonX,intButtonY); break; default: break; } return false; } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: RestoreButton(); break; case KeyEvent.KEYCODE_DPAD_UP: MoveButtonUp(); break; case KeyEvent.KEYCODE_DPAD_DOWN: MoveButtonDown(); break; case KeyEvent.KEYCODE_DPAD_LEFT: MoveButtonLeft(); break; case KeyEvent.KEYCODE_DPAD_RIGHT: MoveButtonRight(); break; } return super.onKeyDown(keyCode, event); } private void MoveButtonRight() { intButtonX+=intShift; if (intButtonX>(intScreenX-intWidth)) { intButtonX=intScreenX-intWidth; } setButtonPos(intButtonX,intButtonY); } private void MoveButtonLeft() { intButtonX-=intShift; if (intButtonX<0) { intButtonX=0; } setButtonPos(intButtonX,intButtonY); } private void MoveButtonDown() { intButtonY=intButtonY+intShift; if (intButtonY>(intScreenY-intHeight)) { intButtonY=intScreenY-intHeight; } setButtonPos(intButtonX,intButtonY); } private void MoveButtonUp() { intButtonY=intButtonY-intShift; if (intButtonY<0) { intButtonY=0; } setButtonPos(intButtonX,intButtonY); } @SuppressWarnings("deprecation") private void setButtonPos(int intButtonX2, int intButtonY2) { mButton.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth, intHeight, intButtonX2, intButtonY2)); System.out.println("("+Integer.toString(intButtonX)+","+Integer.toString(intButtonY)+")"); } private void RestoreButton() { initXY(); Toast.makeText(this, "("+Integer.toString(intButtonX)+","+Integer.toString(intButtonY)+")", Toast.LENGTH_LONG).show(); setButtonPos(intButtonX,intButtonY); } private void initXY() { intButtonX=((intScreenX-intWidth)/2); intButtonY=((intScreenY-intHeight)/2); } }
2019年1月22日 12:05
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
MoveButtonDown();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
MoveButtonLeft();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
MoveButtonRight();
break;
2022年9月24日 02:32
Biology is the study of the life of flora and fauna. Biology talks about organisms and how they react to certain toxins when you mix things up. Biology has a long history which showed impacted people in negative and positive ways. Biology is the study of the life of flora and fauna. NCERT Biology Question Paper Class 9 Biology talks about organisms and how they react to certain toxins when you mix things up. Biology has a long history which showed impacted people in negative and positive ways.Biology is one of the interesting studies with practical experiments which helps the students in understanding better way.
2023年2月08日 16:15
BSNL is installing Bharat Net a country-wide fiber optic cable for internet connectivity in many of the panchayats, and on the other hand, ISP brought the same fibernet technology to your doorstep directly and through TIPs. BSNL FTTH plans Telecom Infrastructure Providers (TIPs) with new BSNL Fiber plans covering many isolated pockets in all BSNL circles of the country for 50Mbps to 300 Mbps internet speed on providing with BSNL Fiber Plans along with FREE ONT as per the possibility.
2023年2月16日 16:17
Board of Secondary Education has deferred the Class 12th board Exam should be held from May 2024. The candidates can check the Class 12th Board Model Question Papers from true site. 12th Question Paper 2024 The 12th Question Paper 2024 will likewise be reported on class 12th students can take note of the total 12th Question Paper 2024. Download the Board Model Paper and Question Paper 2024 as indicated by the 12th Class Model Paper 2024 educational board.