1. 程式人生 > >滑動螢幕來獲取當前的座標位置

滑動螢幕來獲取當前的座標位置

package com.bawei.myapplication.gesture;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**

  • 這裡是手勢拖動

  • @author hasee
    */
    public class CustomGestureView extends View{
    Paint mPaint;
    float mTouchX = 300, mTouchY = 300, mCircleRadius = 50;

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

    public CustomGestureView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
    }
    private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.RED);
    mPaint.setStrokeWidth(10);
    mPaint.setStyle(Paint.Style.FILL);

     //監聽touch事件
     setOnTouchListener(new OnTouchListener() {
         @Override
         public boolean onTouch(View v, MotionEvent event) {
    
             //獲取點選位置
             mTouchX = event.getX();
             mTouchY = event.getY();
    
             //重新整理view
             invalidate();
    
             return true;
         }
     });
    

    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //畫圓
    //第一個引數,圓心x軸位置
    //第二個引數,圓心y軸位置
    //第三個引數,圓形半徑
    //第四個引數,畫筆
    canvas.drawCircle(mTouchX, mTouchY, mCircleRadius, mPaint);
    }
    }