package com.oppo.examples;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;public class DraftTest extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);DisplayMetrics dm = getResources().getDisplayMetrics();final int screenWidth = dm.widthPixels;final int screenHeight = dm.heightPixels - 50;final Button b=(Button)findViewById(R.id.btn);b.setOnTouchListener(new OnTouchListener(){int lastX, lastY;@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubint ea=event.getAction();Log.i("TAG", "Touch:"+ea);//Toast.makeText(DraftTest.this, "位置:"+x+","+y, Toast.LENGTH_SHORT).show();switch(ea){case MotionEvent.ACTION_DOWN:lastX = (int) event.getRawX();lastY = (int) event.getRawY();break;/** * layout(l,t,r,b) * l  Left position, relative to parent                     t  Top position, relative to parent                     r  Right position, relative to parent                     b  Bottom position, relative to parent   * */case MotionEvent.ACTION_MOVE:int dx =(int)event.getRawX() - lastX;int dy =(int)event.getRawY() - lastY;int left = v.getLeft() + dx;int top = v.getTop() + dy;int right = v.getRight() + dx;int bottom = v.getBottom() + dy;if(left < 0){left = 0;right = left + v.getWidth();}if(right > screenWidth){right = screenWidth;left = right - v.getWidth();}if(top < 0){top = 0;bottom = top + v.getHeight();}if(bottom > screenHeight){bottom = screenHeight;top = bottom - v.getHeight();}v.layout(left, top, right, bottom);Log.i("", "position:" + left +", " + top + ", " + right + ", " + bottom);   lastX = (int) event.getRawX();lastY = (int) event.getRawY();break;case MotionEvent.ACTION_UP:break;        }return false;}});}}

更多相关文章

  1. Android通过PopupMenu定义弹出菜单的位置
  2. android 数据库更改数据库位置【DbFlow示范】
  3. MediaScannerReceiver
  4. Android(安卓)UISegmentedControl Fragment切换标签
  5. android cupcake源码编译问题
  6. Android自定义对话框(Dialog)位置,大小
  7. Build android source code 2 -- 下载源码
  8. Flutter Android启动源码分析(一)
  9. android sdk 更新速度慢的解决办法

随机推荐

  1. Android中实现日期时间选择器(DatePicker
  2. Android(安卓)自绘输入框
  3. LinearLayout按下(pressed)或获取焦点(fo
  4. Android(安卓)TextView在XML中的属性
  5. Android之常用文件路径
  6. Android(安卓)Touch事件
  7. Android软件框架的搭建笔记
  8. 2010.11.16———android Camera 拍照的
  9. android下访问sd卡和网络的权限
  10. Android在开机时自动启动一个应用程序