Android可移動的Button

來源:互聯網
上載者:User

標籤: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     @Override21     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         @Override39         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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.