码迷,mamicode.com
首页 > 移动开发 > 详细

Android可移动的Button

时间:2017-03-09 13:21:52      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:port   date   eth   roi   bottom   otto   位置   listen   src   

关键

技术分享

 1 package com.example.administrator.mystudent.ButtonMove;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.util.DisplayMetrics;
 6 import android.util.Log;
 7 import android.view.MotionEvent;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.Toast;
11 
12 import com.example.administrator.mystudent.R;
13 
14 public class ButtonMoveActivity extends Activity {
15 
16     private Button btn1;
17     private int screenWidth;
18     private int screenHeight;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_button_move);
24 
25         //DisplayMetrics取屏幕大小分辨率
26         DisplayMetrics dm=getResources().getDisplayMetrics();
27         screenWidth=dm.widthPixels;
28         screenHeight=dm.heightPixels-50;
29 
30         btn1 = (Button) findViewById(R.id.btn1);
31         btn1.setOnTouchListener(new MyOnTouchListener());
32 
33     }
34 
35     class MyOnTouchListener implements View.OnTouchListener {
36         int lastX,lastY;
37 
38         @Override
39         public boolean onTouch(View v, MotionEvent event) {
40             int ea=event.getAction();
41             Log.i("TAG", "Touch:"+ea);
42 
43             switch(ea){
44                 case MotionEvent.ACTION_DOWN:
45 
46                     lastX=(int)event.getRawX();//获取触摸事件触摸位置的原始X坐标
47                     lastY=(int)event.getRawY();
48                     break;
49 
50                 case MotionEvent.ACTION_MOVE:
51                     int dx=(int)event.getRawX()-lastX;
52                     int dy=(int)event.getRawY()-lastY;
53 
54                     int l=v.getLeft()+dx;
55                     int b=v.getBottom()+dy;
56                     int r=v.getRight()+dx;
57                     int t=v.getTop()+dy;
58 
59                     //下面判断移动是否超出屏幕
60                     if(l<0){
61                         l=0;
62                         r=l+v.getWidth();
63                     }
64 
65                     if(t<0){
66                         t=0;
67                         b=t+v.getHeight();
68                     }
69 
70                     if(r>screenWidth){
71                         r=screenWidth;
72                         l=r-v.getWidth();
73                     }
74 
75                     if(b>screenHeight){
76                         b=screenHeight;
77                         t=b-v.getHeight();
78                     }
79                     v.layout(l, t, r, b);
80 
81                     lastX=(int)event.getRawX();
82                     lastY=(int)event.getRawY();
83                     Toast.makeText(getApplicationContext(),
84                             "当前位置:"+l+","+t+","+r+","+b,
85                             Toast.LENGTH_SHORT).show();
86 
87                     //利用invalidate()刷新界面(加post多线程)
88                     v.postInvalidate();
89                     break;
90                 case MotionEvent.ACTION_UP:
91                     break;
92             }
93             return false;
94         }
95     }
96 
97 }

 

Android可移动的Button

标签:port   date   eth   roi   bottom   otto   位置   listen   src   

原文地址:http://www.cnblogs.com/yoyohong/p/6524921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!