• Android可移动的Button


    关键

     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 }
  • 相关阅读:
    关于数据库索引,必须掌握的知识点
    Java基础知识面试题(最详细版)
    基于WinForm制作的用户名密码存储器
    DataGridView点击列名自动排序
    WebRequest.Create(url)无效的URI:无效端口指定的URL时
    knockout 数据绑定,同一个页面table位置加载两个不同的表格数据
    pipeline管道初体验
    Socket,长连接,消息推送,消息提醒,未读消息提醒,消息通知,未读消息通知
    搭建SVN服务器
    C#解决jsonp跨域问题jsonp跨域配置
  • 原文地址:https://www.cnblogs.com/yoyohong/p/6524921.html
Copyright © 2020-2023  润新知