扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

android鎬庝箞鑾峰彇灞忓箷鐐瑰嚮浜嬩欢

扬州沐宇科技
2024-03-16 13:25:31
android

鍦ˋndroid涓幏鍙栧睆骞曠偣鍑讳簨浠跺彲浠ラ€氳繃浠ヤ笅姝ラ瀹炵幇锛?/p>

  1. 鍒涘缓涓€涓户鎵胯嚜View鐨勮嚜瀹氫箟View锛岄噸鍐欏叾onTouchEvent鏂规硶鏉ュ鐞嗗睆骞曠偣鍑讳簨浠躲€?/li>
public class CustomView extends View {

    public CustomView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 鑾峰彇鐐瑰嚮浜嬩欢鐨勫潗鏍?/span>
        float x = event.getX();
        float y = event.getY();

        // 澶勭悊鐐瑰嚮浜嬩欢
        // TODO: 鍦ㄨ繖閲屽鐞嗙偣鍑讳簨浠剁殑閫昏緫

        // 杩斿洖true琛ㄧず宸茬粡澶勭悊浜嗚浜嬩欢
        return true;
    }
}
  1. 鍦ˋctivity涓娇鐢ㄨ鑷畾涔塚iew锛屽苟璁剧疆鐐瑰嚮浜嬩欢鐩戝惉鍣ㄣ€?/li>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new CustomView(this));
    }
}

閫氳繃浠ヤ笂姝ラ锛屽氨鍙互鍦ˋndroid搴旂敤涓幏鍙栧睆骞曠偣鍑讳簨浠跺苟杩涜鐩稿簲鐨勫鐞嗐€?/p>

扫码添加客服微信