代码:
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by hwk on 2020/5/18.
*/
public class PointView extends View {
private Paint paintPoint;//圆心
private Paint paintWave;//波浪
private float x, y;//画圆中心点坐标
private float radiusPoint;//圆心直径
private int radiusWave;//波浪直径
private int alphaWave;//波浪透明度常量
private int mohufanwei = 8;//移动少于次参数,视为未移动,单位/px
public PointView(Context context) {
super(context);
initPaint();
}
public PointView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
private void initPaint() {
paintPoint = new Paint();
paintPoint.setStyle(Paint.Style.FILL);
paintPoint.setColor(Color.parseColor("#FF0000"));
paintPoint.setAntiAlias(true);
paintPoint.setStrokeWidth(radiusPoint / 2);
paintWave = new Paint();
paintWave.setStyle(Paint.Style.FILL);
paintWave.setColor(Color.parseColor("#BBCFFC"));
paintWave.setAntiAlias(true);
paintWave.setStrokeWidth(radiusWave / 2);
paintWave.setAlpha(alphaWave);
}
private Timer timer;
private TimerTask timerTask;
boolean isCick = true;//判断是否进行点击
private static final int LONGPRESSTIME = 1000;//长按超过1秒,触发长按事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && event.getPointerCount() == 1) {
//长按计时器
x = event.getX();
y = event.getY();
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
//长按逻辑触发,isClick置为false,手指移开后,不触发点击事件
isCick = false;
doLongPress();
}
};
isCick = true;
timer.schedule(timerTask, LONGPRESSTIME, 60 * 60 * 24 * 1000);
}
if (event.getAction() == MotionEvent.ACTION_UP && event.getPointerCount() == 1) {
//没有触发长按逻辑,进行点击事件
if (isCick) {
doClick();
}
timerTask.cancel();
timer.cancel();
}
//出现移动,取消点击和长按事件
if (event.getAction() == MotionEvent.ACTION_MOVE) {
//如果在一定范围内移动,不处理移动事件
if (Math.abs(x - event.getX()) > mohufanwei || Math.abs(y - event.getY()) > mohufanwei) {
isCick = false;
timerTask.cancel();
timer.cancel();
}
}
return true;
}
/**
* 长按事件
*/
private void doLongPress() {
Log.e("hwk", "长按了" + x + " " + y);
}
/**
* 点击事件
*/
private void doClick() {
Log.e("hwk", "点击了" + x + " " + y);
radiusPoint = 25;
radiusWave = 35;
alphaWave = 255;
flushPaint();
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
flushPaint();
}
};
private void flushPaint() {
radiusPoint = radiusPoint - 1.5f;
radiusWave = radiusWave + 3;
alphaWave = alphaWave - 20;
if (alphaWave <= 25) {
alphaWave = 0;
paintWave.setStrokeWidth(radiusWave / 2);
paintWave.setAlpha(alphaWave);
paintPoint.setStrokeWidth(radiusPoint / 2);
paintPoint.setAlpha(0);
invalidate();
} else {
paintWave.setStrokeWidth(radiusWave / 2);
paintWave.setAlpha(alphaWave);
paintPoint.setStrokeWidth(radiusPoint / 2);
paintPoint.setAlpha(255);
invalidate();
handler.sendEmptyMessageDelayed(0, 35);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, radiusWave, paintWave);
canvas.drawCircle(x, y, radiusPoint, paintPoint);
}
}