gpt4 book ai didi

java - 如何为多个 ImageView 设置触摸监听器?

转载 作者:行者123 更新时间:2023-11-29 03:00:30 25 4
gpt4 key购买 nike

我为我的 ImageView“汽车”创建了一个 onTouchListener,并想为另一个 ImageView 做同样的事情,但我不知道如何做。所以我的问题是:

如何使用一个 onTouchListener 来检测两个独立 ImageView 的 MotionEvent 并相应地发生某些事情?

@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.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;
}

关于java - 如何为多个 ImageView 设置触摸监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35318585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com