我为我的ImageView“car”创建了一个onTouchListener,并希望为另一个ImageView做同样的事情,但我无法弄清楚如何。所以我的问题是:
如何使用一个onTouchListener检测两个独立ImageView的MotionEvents并相应地发生一些事情?
@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;
}
答案 0 :(得分:1)
如果您的活动正在实施onTouchListener事件(我假设基于覆盖),则在您的其他ImageView上执行以下操作
imageView.setOnTouchListener(this);
这是当前的活动。
编辑:根据您的评论
public class ImageView1Listener implements OnTouchListener
{
**** OVERRIDES****
}
public class ImageView2Listener implements OnTouchListener{
**** OVERRIDS FOR THIS IMAGE****
}
然后在你的主要删除工具OnTouchListener并使用以编程方式绑定它
imageView1.setOnClickListener(new ImageViewListener1());
imageView2.setOnClickListener(new ImageViewListener2());
答案 1 :(得分:1)
将侦听器附加到它们的父级。
比,使用此处所述的gesturesDetector
:
https://developer.android.com/training/gestures/detector.html
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
....
}
答案 2 :(得分:1)
您应该实现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;
}