为了账号安全,请及时绑定邮箱和手机立即绑定

android的多次点击事件的实现(有源码)

标签:
Android

三次点击事件的原理图:数组的复制(android源码的调用):

5bb8200f0001fb0d04540434.jpg

下面就是第一步:

5bb8200f00019c8207100084.jpg

创建long数组,里面的数字代表点击的次数。

下面是主要代码实现:

5bb82010000176fa10370446.jpg

system.arraycopy();里面的参数描述:

5bb820100001645a05890090.jpg


最后就是在if里面实现你想要的按钮的点击逻辑,不过最后一点很重要,当按钮还有触摸的点击事件的时候,有一点:

5bb820100001e7e309840089.jpg

一定记得返回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;
   }
  });
 }
}

原文链接:http://www.apkbus.com/blog-846519-61565.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消