I have a SurfaceView in my activity xml and I want to get some informations when the SurfaceView is clicked. For that I have a class like below
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;
}
}
my activity is
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);
}
}
and xml file
<?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 does not work with this way but if I set it by test.setOnTouchListener(...)
it works. I think I should relate SurfaceView test with ImageTable but I don't know how to do. I also some drawing codes in ImageTable and they work properly, everything is drawn in SurfaceView test
If I don't declare any SurfaceView on xml file and do setContenView(new ImageTable(...))
and set surfaceHolder as SurfaceHolder surfaceHolder = getHolder()
the event works.
CodePudding user response:
First of all implement View.OnTouchListener
interface on view class. After that connect view and listener. Also don't forget about 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);
}
}
So If your want to implement it in ImageTable class, then try to do like this
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....