Android 工作室 SurfaceView onTouchEvent
Android studio SurfaceView onTouchEvent
我的 activity xml 中有一个 SurfaceView,我想在单击 SurfaceView 时获取一些信息。为此,我有一个 class 如下所示
class ImageTable extends SurfaceView implements SurfaceHolder.Callback {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG,String.valueOf( event.getAction())+"asd");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
我的activity是
public class SubmitActivity extends AppCompatActivity {
SurfaceView test;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);
test = findViewById(R.id.cylindersImage);
context = this;
imageTable = new ImageTable(this,test);
}
}
和xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/cylindersImage"
android:layout_width="358dp"
android:layout_height="219dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05">
</SurfaceView>
</androidx.constraintlayout.widget.ConstraintLayout>
onTouchEvent 不适用于这种方式,但如果我通过 test.setOnTouchListener(...)
设置它,它会起作用。我想我应该将 SurfaceView 测试与 ImageTable 联系起来,但我不知道该怎么做。我还在 ImageTable 中绘制了一些代码,它们工作正常,所有内容都在 SurfaceView 测试中绘制
如果我不在 xml 文件上声明任何 SurfaceView 并执行 setContenView(new ImageTable(...))
并将 surfaceHolder 设置为 SurfaceHolder surfaceHolder = getHolder()
事件有效。
首先在视图 class 上实现 View.OnTouchListener
界面。之后连接视图和侦听器。也不要忘记 ACTION_CANCEL case
public class SubmitActivity extends AppCompatActivity implements View.OnTouchListener // implement interface {
SurfaceView test;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);
test = findViewById(R.id.cylindersImage);
context = this;
imageTable = new ImageTable(this,test);
imageTable.setOnTouchListener(this) // connect interface and view
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG,String.valueOf( event.getAction())+"asd");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
// usually handles when you make fast moves with finger
// don't forget about it, it handles often
break;
}
return true;
}
}
class ImageTable extends SurfaceView implements SurfaceHolder.Callback {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
}
}
因此,如果您想在 ImageTable class 中实现它,请尝试这样做
class ImageTable extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
surfaceView.setOnTouchListener(this);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
...implement event....
我的 activity xml 中有一个 SurfaceView,我想在单击 SurfaceView 时获取一些信息。为此,我有一个 class 如下所示
class ImageTable extends SurfaceView implements SurfaceHolder.Callback {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG,String.valueOf( event.getAction())+"asd");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
我的activity是
public class SubmitActivity extends AppCompatActivity {
SurfaceView test;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);
test = findViewById(R.id.cylindersImage);
context = this;
imageTable = new ImageTable(this,test);
}
}
和xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/cylindersImage"
android:layout_width="358dp"
android:layout_height="219dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05">
</SurfaceView>
</androidx.constraintlayout.widget.ConstraintLayout>
onTouchEvent 不适用于这种方式,但如果我通过 test.setOnTouchListener(...)
设置它,它会起作用。我想我应该将 SurfaceView 测试与 ImageTable 联系起来,但我不知道该怎么做。我还在 ImageTable 中绘制了一些代码,它们工作正常,所有内容都在 SurfaceView 测试中绘制
如果我不在 xml 文件上声明任何 SurfaceView 并执行 setContenView(new ImageTable(...))
并将 surfaceHolder 设置为 SurfaceHolder surfaceHolder = getHolder()
事件有效。
首先在视图 class 上实现 View.OnTouchListener
界面。之后连接视图和侦听器。也不要忘记 ACTION_CANCEL case
public class SubmitActivity extends AppCompatActivity implements View.OnTouchListener // implement interface {
SurfaceView test;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);
test = findViewById(R.id.cylindersImage);
context = this;
imageTable = new ImageTable(this,test);
imageTable.setOnTouchListener(this) // connect interface and view
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG,String.valueOf( event.getAction())+"asd");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
// usually handles when you make fast moves with finger
// don't forget about it, it handles often
break;
}
return true;
}
}
class ImageTable extends SurfaceView implements SurfaceHolder.Callback {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
}
}
因此,如果您想在 ImageTable class 中实现它,请尝试这样做
class ImageTable extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
public ImageTable(Context context, SurfaceView surfaceView) {
super(context);
surfaceHolder = surfaceView.getHolder();//getHolder();//
surfaceHolder.addCallback(this);
this.context = context;
setFocusable(true);
surfaceView.setOnTouchListener(this);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
...implement event....