自制小吐司,可以在activity上拖拽与双击

package com.example.mobilesafe;

import javax.crypto.spec.IvParameterSpec;

import com.example.mobilesafe.bean.donotChangeValue;
import com.example.mobilesafe.utils.SPUtil;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class GuiShuDiToastWeiZhiActivity extends Activity {
	private Button mBt_GuiShuDiToastWeiZhi_Top;
	private Button mBt_GuiShuDiToastWeiZhi_Buttom;
	private ImageView mIv_move_toast;
	private WindowManager mWM;
	private int width;
	private int height;
	private long[] click=new long[2];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gui_shu_di_toasht_wei_zhi);
		initButton();
		initDrag();
	}

	/**
	 * 初始化拖拽
	 */
	private void initDrag() {
		// 得到控件
		mIv_move_toast = (ImageView) findViewById(R.id.iv_move_toast);

		// 得到屏幕管理者,获得屏幕宽高
		mWM = (WindowManager) getSystemService(WINDOW_SERVICE);
		width = mWM.getDefaultDisplay().getWidth();
		height = mWM.getDefaultDisplay().getHeight();

		// 获得保存的控件位置
		int locationX = SPUtil.getSpInt(getApplicationContext(),
				donotChangeValue.TOASTLOCATIONX);
		int locationY = SPUtil.getSpInt(getApplicationContext(),
				donotChangeValue.TOASTLOCATIONY);
		Log.d("位置", "locationX="+locationX+"\nlocationY="+locationY);
		// 设置一个params,并指定位置
		LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.leftMargin = locationX;
		params.topMargin = locationY;
		mIv_move_toast.setLayoutParams(params);

		// 设置隐藏的button
		if (locationY > height / 2) {
			mBt_GuiShuDiToastWeiZhi_Top.setVisibility(View.VISIBLE);
			mBt_GuiShuDiToastWeiZhi_Buttom.setVisibility(View.INVISIBLE);
		} else {
			mBt_GuiShuDiToastWeiZhi_Top.setVisibility(View.INVISIBLE);
			mBt_GuiShuDiToastWeiZhi_Buttom.setVisibility(View.VISIBLE);
		}
		mIv_move_toast.setOnClickListener(new OnClickListener() {
			
			/* (non-Javadoc)
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 * 双击土司进行居中
			 */
			@Override
			
			public void onClick(View v) {
				long time=System.currentTimeMillis();
				System.arraycopy(click, 1, click, 0, click.length-1);
				click[click.length-1]=time;
				if(click[click.length-1]-click[0]<500){
					int left=width/2-mIv_move_toast.getWidth()/2;
					int top=height/2-mIv_move_toast.getHeight()/2;
					int right=width/2+mIv_move_toast.getWidth()/2;
					int bottom=height/2+mIv_move_toast.getHeight()/2;
					mIv_move_toast.layout(left, top, right, bottom);
					SPUtil.putSpInt(getApplicationContext(),
							donotChangeValue.TOASTLOCATIONX, mIv_move_toast.getLeft());
					SPUtil.putSpInt(getApplicationContext(),
							donotChangeValue.TOASTLOCATIONY, mIv_move_toast.getTop());
				}
			}
		});
		// 设置拖拽事件
		mIv_move_toast.setOnTouchListener(new OnTouchListener() {
			private float startX;
			private float startY;

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					//得到初始位置
					startX = event.getRawX();
					startY = event.getRawY();
					break;
				case MotionEvent.ACTION_MOVE:
					float moveX = event.getRawX();
					float moveY = event.getRawY();

					float changeX = moveX - startX;
					float changeY = moveY - startY;

					//得到四周的位置坐标
					int left = (int) (mIv_move_toast.getLeft() + changeX);
					int top = (int) (mIv_move_toast.getTop() + changeY);
					int right = (int) (mIv_move_toast.getRight() + changeX);
					int bottom = (int) (mIv_move_toast.getBottom() + changeY);

					if(left<0||top<22||right>width||bottom>height-22)
						return true;
					mIv_move_toast.layout(left, top, right, bottom);
					//设置按钮的隐藏与显示
					if (moveY > height / 2) {
						mBt_GuiShuDiToastWeiZhi_Top.setVisibility(View.VISIBLE);
						mBt_GuiShuDiToastWeiZhi_Buttom.setVisibility(View.INVISIBLE);
					} else {
						mBt_GuiShuDiToastWeiZhi_Top.setVisibility(View.INVISIBLE);
						mBt_GuiShuDiToastWeiZhi_Buttom.setVisibility(View.VISIBLE);
					}
					//重新设置初始位置
					startX = moveX;
					startY = moveY;

					break;
				case MotionEvent.ACTION_UP:
					//保存当前的土司位置
					SPUtil.putSpInt(getApplicationContext(),
							donotChangeValue.TOASTLOCATIONX, mIv_move_toast.getLeft());
					SPUtil.putSpInt(getApplicationContext(),
							donotChangeValue.TOASTLOCATIONY, mIv_move_toast.getTop());
					Log.d("保存坐标", "left"+mIv_move_toast.getLeft()+"\ntop"+ mIv_move_toast.getTop());
					break;

				}
				return false;//因为设置了点击事件和触摸事件同时成立,这里返回false
			}
		});
	}

	/**
	 * 初始化按钮
	 */
	private void initButton() {
		mBt_GuiShuDiToastWeiZhi_Top = (Button) findViewById(R.id.bt_GuiShuDiToastWeiZhi_Top);
		mBt_GuiShuDiToastWeiZhi_Buttom = (Button) findViewById(R.id.bt_GuiShuDiToastWeiZhi_Buttom);
	}
}
权限    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

猜你喜欢

转载自blog.csdn.net/sinat_40387150/article/details/80842401