懸浮球跟隨手指移動


/**
 * 項目名稱:mobile_android
 * 創(chuàng)建者:  鄭偉
 * 創(chuàng)建時間:2019/1/18 上午10:44
 * 功能簡介:跟隨手指移動的小球
 */

public class MoveBallView extends View {
    private float currentX, currentY;//起始坐標(biāo)
    private boolean isDraw;//是否繪制
    private float radius = 100;//半徑
    private Paint mPaint;//畫筆
    private float height, width;//屏幕寬高
    private float downX, downY;//按下時的坐標(biāo)

    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() {
        //創(chuàng)建畫筆
        mPaint = new Paint();
        //設(shè)置消除鋸齒
        mPaint.setAntiAlias(true);
        //拿屏幕的寬高
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        height = metrics.heightPixels;
        width = metrics.widthPixels;
        //設(shè)置球的起始位置
        currentX = width - radius - 100;
        currentY = height - radius - 300;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //設(shè)置畫筆的顏色
        mPaint.setColor(Color.RED);
        //繪制個球
        canvas.drawCircle(currentX, currentY, radius, mPaint);
        //設(shè)置文字大小
        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();
                //為了解決阻礙頁面其他控件消費(fèi)事件的問題來返回true或false  判斷主要是為了確定我點(diǎn)的范圍是球
                if ((downX > currentX - radius && downX < currentX + radius) && (downY > currentY
                        - radius && downY < currentY + radius)) {
                    isDraw = true;
                } else {
                    return false;
                }

            case MotionEvent.ACTION_MOVE:
                if (isDraw) {
                    //修改當(dāng)前的坐標(biāo)
                    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:
                //為了區(qū)分是在點(diǎn)擊球還是移動球
                if (Math.abs(event.getX() - downX) < 10 || Math.abs(event.getY() - downY) < 10)
                    performClick();
                isDraw = false;
                return false;
        }
        return true;
    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容