結(jié)構(gòu)
1:定義一個半透明的Activity
2:使控件可以拖拽,并記錄下拖拽后控件的位置
1:定義一個半透明的Activity
<activity android:name=".Show_Toast_Location"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
直接在清單文件中加屬性,
因為是原來的AC是白色底,
所以會出現(xiàn)全透明的樣子,還需將原來的AC背景顏色換成灰色,
這樣才能顯出半透明的感覺。
android:background="#6a0a0a0a"
2:使控件可以拖拽,并記錄下拖拽后控件的位置
1:找到控件并設(shè)置一個觸摸監(jiān)聽器
iv_show_toast_location.setOnTouchListener(new View.OnTouchListener()
2:實現(xiàn)觸摸監(jiān)聽器接口中的方法 (MotionEvent 這個參數(shù)指手勢動作)
public boolean onTouch(View view, MotionEvent motionEvent)
3:拿到手勢動作與用戶手勢動作進行匹配
motionEvent.getAction()
MotionEvent.ACTION_DOWN://(按下去的參數(shù))
MotionEvent.ACTION_MOVE: //(移動時候的參數(shù))
MotionEvent.ACTION_UP
4:第一次按住屏幕(MotionEvent.ACTION_DOWN)拿到按下去的X 和 Y
case MotionEvent.ACTION_DOWN://(按下去的參數(shù))
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
break;
5:按住控件進行移動的時候進行計算
case MotionEvent.ACTION_MOVE: //(移動時候的參數(shù))
int MoveX = (int) motionEvent.getRawX(); //拿到移動時X的坐標(biāo)
int MoveY = (int) motionEvent.getRawY(); ////拿到移動時X的坐標(biāo)
//拿到偏移量
int desX = MoveX - startX;
int desY = MoveY - startY;
//下面是移動前的位置
//確定一個矩形只需要兩個點
//拿到當(dāng)前控件,確定原來左,上角位置
int left = iv_show_toast_location.getLeft();
int top = iv_show_toast_location.getTop();
//拿到當(dāng)前控件,確定原來右,下角位置
int right = iv_show_toast_location.getRight();
int bottom = iv_show_toast_location.getBottom();
//下面是移動后的位置
//移動后的左 , 上 ,右 , 下 就是原來的加上偏移的
int Mleft = left + desX;
int Mtop = top + desY;
int Mright = right + desX;
int Mbotton = bottom + desY;
//容錯處理,不能讓控件超出屏幕位置
//以原點為坐標(biāo)系 左 和 上 不能小于0
if (Mleft<0){
return true;
}
if(Mtop<0){
return true;
}
//以原點為坐標(biāo)系 右 和 下 不能小于屏幕寬度
if (Mright>WindowsWidth){
return true;
}
if (Mbotton>windowsHeight-22){
return true;
}
//是手機按照移動的過程去做展示
iv_show_toast_location.layout(Mleft,Mtop,Mright,Mbotton);
//重置開始坐標(biāo)
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
break;
6:抬手的時候記錄下當(dāng)前的X和Y值
case MotionEvent.ACTION_UP: //(抬手的參數(shù))
//抬手 就記錄位置 在SP中
SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_LEFT,iv_show_toast_location.getLeft());
SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_TOP,iv_show_toast_location.getTop());
break;
7:這個方法返回值為Boolean值
//這里要改成返回true 才能移動
//如果既要相應(yīng)點擊 又要相應(yīng)拖動 則要改成false
return true;
全部代碼
public class Show_Toast_Location extends Activity {
private ImageView iv_show_toast_location;
private Button bt_show_toast_location_belowp;
private Button bt_show_toast_location_top;
private WindowManager windowManager;
private int windowsHeight;
private int WindowsWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_toast_location);
initUi();
initData();
}
private void initUi() {
iv_show_toast_location = (ImageView) findViewById(R.id.iv_show_Toast_Location);
bt_show_toast_location_top = (Button) findViewById(R.id.bt_show_Toast_Location_top);
bt_show_toast_location_belowp = (Button) findViewById(R.id.bt_show_Toast_Location_below);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//拿到屏幕寬度和高度
WindowsWidth = windowManager.getDefaultDisplay().getWidth();
windowsHeight = windowManager.getDefaultDisplay().getHeight();
//通過下面拿到的左上角坐標(biāo),設(shè)置及到控件上做回顯操作
int MaginLeft = SpUtil.getInt(this, FianlMath.TOAST_LEFT, 0);
int MaginTop = SpUtil.getInt(this, FianlMath.TOAST_TOP, 0);
//設(shè)置控件位置,應(yīng)該拿到控件的布局來提供
//下面的參數(shù)作用是 在指定當(dāng)前布局內(nèi)的控件的大小
//定義一個規(guī)則
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//指定控件位置
//將左上角的坐標(biāo)作用到控件上
layoutParams.leftMargin = MaginLeft;
layoutParams.topMargin = MaginTop;
//將這個規(guī)則作用在想要這樣做的控件上
iv_show_toast_location.setLayoutParams(layoutParams);
//上下兩段文字的顯示
if (MaginTop>windowsHeight/2){
bt_show_toast_location_top.setVisibility(View.VISIBLE);
bt_show_toast_location_belowp.setVisibility(View.INVISIBLE);
}else {
bt_show_toast_location_top.setVisibility(View.INVISIBLE);
bt_show_toast_location_belowp.setVisibility(View.VISIBLE);
}
}
private void initData() {
//設(shè)置觸摸的監(jiān)聽器
iv_show_toast_location.setOnTouchListener(new View.OnTouchListener() {
private int startX;
private int startY;
//(motionEvent)這個參數(shù)是移動的數(shù)據(jù)(行為)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN://(按下去的參數(shù))
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
break;
case MotionEvent.ACTION_MOVE: //(移動時候的參數(shù))
int MoveX = (int) motionEvent.getRawX(); //拿到移動時X的坐標(biāo)
int MoveY = (int) motionEvent.getRawY(); ////拿到移動時X的坐標(biāo)
//拿到偏移量
int desX = MoveX - startX;
int desY = MoveY - startY;
//下面是移動前的位置
//確定一個矩形只需要兩個點
//拿到當(dāng)前控件,確定原來左,上角位置
int left = iv_show_toast_location.getLeft();
int top = iv_show_toast_location.getTop();
//拿到當(dāng)前控件,確定原來右,下角位置
int right = iv_show_toast_location.getRight();
int bottom = iv_show_toast_location.getBottom();
//下面是移動后的位置
//移動后的左 , 上 ,右 , 下 就是原來的加上偏移的
int Mleft = left + desX;
int Mtop = top + desY;
int Mright = right + desX;
int Mbotton = bottom + desY;
//容錯處理,不能讓控件超出屏幕位置
//以原點為坐標(biāo)系 左 和 上 不能小于0
if (Mleft<0){
return true;
}
if(Mtop<0){
return true;
}
//以原點為坐標(biāo)系 右 和 下 不能小于屏幕寬度
if (Mright>WindowsWidth){
return true;
}
if (Mbotton>windowsHeight-22){
return true;
}
//上下兩個文字的顯示
if (Mtop>windowsHeight/2){
bt_show_toast_location_top.setVisibility(View.VISIBLE);
bt_show_toast_location_belowp.setVisibility(View.INVISIBLE);
}else {
bt_show_toast_location_top.setVisibility(View.INVISIBLE);
bt_show_toast_location_belowp.setVisibility(View.VISIBLE);
}
//是手機按照移動的過程去做展示
iv_show_toast_location.layout(Mleft,Mtop,Mright,Mbotton);
//重置開始坐標(biāo)
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
break;
case MotionEvent.ACTION_UP: //(抬手的參數(shù))
//抬手 就記錄位置 在SP中
SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_LEFT,iv_show_toast_location.getLeft());
SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_TOP,iv_show_toast_location.getTop());
break;
}
//這里要改成返回true 才能移動
//如果既要相應(yīng)點擊 又要相應(yīng)拖動 則要改成false
return true;
}
});
}
}
圖片





