三次点击事件的原理图:数组的复制(android源码的调用):
下面就是第一步:
创建long数组,里面的数字代表点击的次数。
下面是主要代码实现:
system.arraycopy();里面的参数描述:
最后就是在if里面实现你想要的按钮的点击逻辑,不过最后一点很重要,当按钮还有触摸的点击事件的时候,有一点:
一定记得返回false;
下面贴出我的源码:tv_drag实现双击居中方法(里面有触摸事件):
package com.example.mobilesafe74.activity;
import com.example.mobilesafe74.R;
import com.example.mobilesafe74.utils.ConstantValue;
import com.example.mobilesafe74.utils.SpUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
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.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class ToastLocationActivity extends Activity {
private ImageView tv_drag;
private Button bt_top;
private Button bt_bottom;
private WindowManager mWm;
private int mScreenwidth;
private int mScreenheight;
private long[] mhits = new long[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_location);
initUI();
}
private void initUI() {
// 当前可以拖拽的图片控件
tv_drag = (ImageView) findViewById(R.id.tv_drag);
bt_top = (Button) findViewById(R.id.bt_top);
bt_bottom = (Button) findViewById(R.id.bt_bottom);
mWm = (WindowManager) getSystemService(WINDOW_SERVICE);
mScreenwidth = mWm.getDefaultDisplay().getWidth();
mScreenheight = mWm.getDefaultDisplay().getHeight();
int locationX = SpUtil.getInt(getApplicationContext(),
ConstantValue.LOCATION_X, 0);
int locationY = SpUtil.getInt(getApplicationContext(),
ConstantValue.LOCATION_Y, 0);
// 左上坐标作用在控件上面
// 指定宽高都是w w
LayoutParams LayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LayoutParams.leftMargin = locationX;
LayoutParams.topMargin = locationY;
if (LayoutParams.topMargin > mScreenheight / 2) {
bt_bottom.setVisibility(View.INVISIBLE);
bt_top.setVisibility(View.VISIBLE);
} else {
bt_bottom.setVisibility(View.VISIBLE);
bt_top.setVisibility(View.INVISIBLE);
}
// 将以上规则作用在控件上面
tv_drag.setLayoutParams(LayoutParams);
tv_drag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.arraycopy(mhits, 1, mhits, 0, mhits.length - 1);
mhits[mhits.length - 1] = SystemClock.uptimeMillis();
if (mhits[mhits.length - 1] - mhits[0] < 500) {
// 满足双击事件后,居中
int left = mScreenwidth / 2 - tv_drag.getWidth() / 2;
int right = mScreenheight / 2 - tv_drag.getHeight() / 2;
int top = mScreenwidth / 2 + tv_drag.getWidth() / 2;
int bottom = mScreenheight / 2 + tv_drag.getHeight() / 2;
// 显示出来
tv_drag.layout(left, top, right, bottom);
}
}
});
// 可以拖拽的点击事件
tv_drag.setOnTouchListener(new OnTouchListener() {
private int startX;
private int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下
// 初始位置的坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:// 移动
int moveX = (int) event.getRawX();
int moveY = (int) event.getRawY();
int disX = moveX - startX;
int disY = moveY - startY;
// 当前控件的上下左右的坐标
int left = tv_drag.getLeft() + disX;
int top = tv_drag.getTop() + disY;
int right = tv_drag.getRight() + disX;
int bottom = tv_drag.getBottom() + disY;
// 容错处理
if (left < 0) {
return true;
}
if (right > mScreenwidth) {
return true;
}
if (top < 0) {
return true;
}
if (bottom > mScreenheight - 22) {
return true;
}
if (top > mScreenheight / 2) {
bt_bottom.setVisibility(View.INVISIBLE);
bt_top.setVisibility(View.VISIBLE);
} else {
bt_bottom.setVisibility(View.VISIBLE);
bt_top.setVisibility(View.INVISIBLE);
}
// 告知控件,按计算的坐标去展示
tv_drag.layout(left, top, right, bottom);
// 重置坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:// 抬起
// 记录位置坐标
SpUtil.putInt(getApplicationContext(),
ConstantValue.LOCATION_X, tv_drag.getLeft());
SpUtil.putInt(getApplicationContext(),
ConstantValue.LOCATION_Y, tv_drag.getTop());
break;
}
// 返回事件,只有触摸操作的时候用true,有点击事件记得加上return false
return false;
}
});
}
}