这document可能有助于你可以得到MyView鼠标坐标这样:
public class MyView extends View implements View.OnTouchListener{
public MyView(Context context) {
super(context);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return true;
}
}
编辑:
阅读文件Layout和TouchEvent会有所帮助。你应该把你的MyView成布局,这里是我的代码,它工作正常:
HomeActivity:
setContentView(R.layout.activity_main);
activity_main:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myView"
android:background="@color/colorPrimary"
/>
MyView的:
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
Log.e("TAG","================== TouchEvent");
break;
case MotionEvent.ACTION_UP:
return true;
}
return super.onTouchEvent(event);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//I have a bunch of shapes and text here
}