Java 如何为多个图像视图设置触控式侦听器?
我为我的ImageView“car”创建了一个onTouchListener,并希望对另一个ImageView执行相同的操作,但是我不知道如何执行。所以我的问题是: 如何使用一个onTouchListener来检测两个单独的ImageView的运动事件,并相应地发生一些事情Java 如何为多个图像视图设置触控式侦听器?,java,android,imageview,ontouchlistener,Java,Android,Imageview,Ontouchlistener,我为我的ImageView“car”创建了一个onTouchListener,并希望对另一个ImageView执行相同的操作,但是我不知道如何执行。所以我的问题是: 如何使用一个onTouchListener来检测两个单独的ImageView的运动事件,并相应地发生一些事情 @Override public boolean onTouch(View view, MotionEvent event) { //int action = event.getAction();
@Override
public boolean onTouch(View view, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
carcolor.setBackgroundColor(this.getResources().getColor(R.color.colorPrimary));
car.startAnimation(pressdown);
pressdown.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
carback.setScaleX((float) 0.9);
carback.setScaleY((float) 0.9);
}
@Override
public void onAnimationEnd(Animation animation) {
carback.setVisibility(View.VISIBLE);
car.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
car.startAnimation(release);
carcolor.setBackgroundColor(this.getResources().getColor(R.color.unpressed));
release.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
carback.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
car.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
如果您的活动正在实现onTouchListener事件(我假设基于覆盖),那么在其他ImageView上只需执行以下操作
imageView.setOnTouchListener(this);
这是当前的活动
根据您的评论编辑:
public class ImageView1Listener implements OnTouchListener
{
**** OVERRIDES****
}
public class ImageView2Listener implements OnTouchListener{
**** OVERRIDS FOR THIS IMAGE****
}
然后在主应用程序中,删除OnTouchListener的实现,并以编程方式使用bind
imageView1.setOnClickListener(new ImageViewListener1());
imageView2.setOnClickListener(new ImageViewListener2());
如果您的活动正在实现onTouchListener事件(我假设基于覆盖),那么在其他ImageView上只需执行以下操作
imageView.setOnTouchListener(this);
这是当前的活动
根据您的评论编辑:
public class ImageView1Listener implements OnTouchListener
{
**** OVERRIDES****
}
public class ImageView2Listener implements OnTouchListener{
**** OVERRIDS FOR THIS IMAGE****
}
然后在主应用程序中,删除OnTouchListener的实现,并以编程方式使用bind
imageView1.setOnClickListener(new ImageViewListener1());
imageView2.setOnClickListener(new ImageViewListener2());
将侦听器附加到它们的父对象。 然后,使用
手势检测器
,如下所述:
将侦听器附加到它们的父对象。 然后,使用
手势检测器
,如下所述:
您应该实现onTouchListener,如下所示:
imageView.setOnTouchListener(this);
它就像:
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (view.getId()){
case R.id.car1: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
case R.id.car2: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
}
return true;
}
您应该实现onTouchListener,如下所示:
imageView.setOnTouchListener(this);
它就像:
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (view.getId()){
case R.id.car1: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
case R.id.car2: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
}
return true;
}
这只回答了一半的问题。你如何给两个图像视图赋予不同的功能并分别检测它们的运动?这一点你的问题还不清楚-你可以创建一个实现OnTouchListener的类,每个类都是唯一的,并将其从类实现中移除。我更新了它,以更好地反映我上一次的评论,这只回答了问题的一半。你如何给两个图像视图赋予不同的功能并分别检测它们的运动?这一点你的问题还不清楚-你可以创建一个类来实现OnTouchListener,每个都是唯一的,并将其从类实现中删除我更新了它,以更好地反映我上一次的评论ment@LincolnWhite ,在本例中,我只粘贴了
onSingleTapConfigure
事件。打开url。您可以阅读链接中的更多示例。否则,您需要什么?@LincolnWhite,在本例中,我只粘贴了onSingleTapConfigure
事件。打开url。您可以阅读链接中的更多示例。如果没有,你需要什么?