import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
private int x0;
private int y0;
private int x1;
private int y1;
private int width;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取屏幕宽高
WindowManager systemService = (WindowManager)getSystemService(WINDOW_SERVICE);
width = systemService.getDefaultDisplay().getWidth();
height = systemService.getDefaultDisplay().getHeight();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取点击点的个数
int action = event.getAction();
if (pointerCount==1) {
switch (action) {
case MotionEvent.ACTION_DOWN:
x0 = (int)event.getX(0);
y0=(int)event.getY(0);
System.out.println("ACTION_DOWN pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP pointerCount="+pointerCount);
break;
default:
break;
}
}
if (pointerCount==2) {
x0 = (int)event.getX(0);
y0=(int)event.getY(0);
x1 = (int)event.getX(1);
y1 = (int)event.getY(1);
switch (action) {
case MotionEvent.ACTION_DOWN:
System.out.println("ACTION_DOWN pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_POINTER_1_DOWN:
System.out.println("ACTION_POINTER_1_DOWN pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_POINTER_1_UP:
System.out.println("ACTION_POINTER_1_UP pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
if (x0<width*0.5 && y0<height*0.5) {
if (x1>width*0.5 && y1>height*0.5) {
Toast.makeText(getApplicationContext(), "弹出窗口", Toast.LENGTH_SHORT).show();
}
}
System.out.println("ACTION_POINTER_2_DOWN pointerCount="+pointerCount);
break;
case MotionEvent.ACTION_POINTER_2_UP:
System.out.println("ACTION_POINTER_2_UP pointerCount="+pointerCount);
break;
default:
break;
}
}
return super.onTouchEvent(event);
}
}
还有一种相对简单的方法就是在屏幕的对角个写以一个view给view写点击事件
代码如下
package com.example.touch;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private View viewTop;
private View viewBotton;
private boolean isOnclickTop = false;
private boolean isOnclickBottom = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
viewTop = findViewById(R.id.view_top);
viewBotton = findViewById(R.id.view_bottom);
initView();
}
private void initView() {
// TODO Auto-generated method stub
viewTop.setOnClickListener(this);
viewBotton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.view_top:
isOnclickTop=true;
new CountDownTimer(100,100) {
@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
isOnclickTop=false;
}
}.start();
break;
case R.id.view_bottom:
isOnclickBottom=true;
new Thread(new Runnable() {//也可以开启一个新的线程
@Override
public void run() {
// TODO Auto-generated method stub
isOnclickBottom=false;
}
}).start();
// new CountDownTimer(100,100) {
//
// @Override
// public void onTick(long arg0) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onFinish() {
// // TODO Auto-generated method stub
// isOnclickBottom=false;
// }
// }.start();
break;
default:
break;
}
if (isOnclickTop==true&&isOnclickBottom==true) {
Toast.makeText(getApplicationContext(), "弹出窗口", Toast.LENGTH_SHORT).show();
}
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View
android:id="@+id/view_top"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#000000"
/>
<View
android:id="@+id/view_bottom"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#ff0000" />
</RelativeLayout>