美文网首页
悬浮球跟随手指移动

悬浮球跟随手指移动

作者: 郑伟1 | 来源:发表于2019-01-18 14:58 被阅读0次

/**
 * 项目名称:mobile_android
 * 创建者:  郑伟
 * 创建时间:2019/1/18 上午10:44
 * 功能简介:跟随手指移动的小球
 */

public class MoveBallView extends View {
    private float currentX, currentY;//起始坐标
    private boolean isDraw;//是否绘制
    private float radius = 100;//半径
    private Paint mPaint;//画笔
    private float height, width;//屏幕宽高
    private float downX, downY;//按下时的坐标

    public MoveBallView(Context context) {
        super(context);
        init();
    }

    public MoveBallView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MoveBallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //创建画笔
        mPaint = new Paint();
        //设置消除锯齿
        mPaint.setAntiAlias(true);
        //拿屏幕的宽高
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        height = metrics.heightPixels;
        width = metrics.widthPixels;
        //设置球的起始位置
        currentX = width - radius - 100;
        currentY = height - radius - 300;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔的颜色
        mPaint.setColor(Color.RED);
        //绘制个球
        canvas.drawCircle(currentX, currentY, radius, mPaint);
        //设置文字大小
        mPaint.setTextSize(DisplayUtil.dip2px(getContext(), 14));
        mPaint.setColor(Color.WHITE);
        //找到文字的绘制中心
        float textX = currentX - mPaint.measureText("抢尾单") / 2;
        float textY = currentY + (mPaint.getFontMetrics().bottom - mPaint.getFontMetrics()
                .top) / 2 - mPaint.getFontMetrics().bottom;
        //绘制个字
        canvas.drawText("抢尾单", textX, textY, mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                //为了解决阻碍页面其他控件消费事件的问题来返回true或false  判断主要是为了确定我点的范围是球
                if ((downX > currentX - radius && downX < currentX + radius) && (downY > currentY
                        - radius && downY < currentY + radius)) {
                    isDraw = true;
                } else {
                    return false;
                }

            case MotionEvent.ACTION_MOVE:
                if (isDraw) {
                    //修改当前的坐标
                    currentX = event.getX();
                    currentY = event.getY();
                    //防止出去四个边界
                    if (currentX < radius) currentX = radius;
                    if (currentX > width - radius) currentX = width - radius;
                    if (currentY < radius) currentY = radius;
                    if (currentY > height - radius) currentY = height - radius;
                    //重绘小球
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                //为了区分是在点击球还是移动球
                if (Math.abs(event.getX() - downX) < 10 || Math.abs(event.getY() - downY) < 10)
                    performClick();
                isDraw = false;
                return false;
        }
        return true;
    }

}

相关文章

网友评论

      本文标题:悬浮球跟随手指移动

      本文链接:https://www.haomeiwen.com/subject/xlendqtx.html