Canvas Android,当触摸位图而不是全屏时,如何执行 OnTouchEvent?
Canvas Android, how can i do an OnTouchEvent when a bitmap is touched instead of the full screen?
我有这个功能:
@Override
public boolean onTouchEvent(MotionEvent event1)
{
if(event1.getAction()==MotionEvent.ACTION_DOWN){
if(!player.getPlaying() && newGameCreated && reset)
{
player.setPlaying(true);
player.setUp(true);
}
if(player.getPlaying())
{
if(!started)started = true;
reset = false;
player.setUp(true);
}
return true;
}
if(event1.getAction()==MotionEvent.ACTION_UP)
{
player.setUp(false);
return true;
}
return super.onTouchEvent(event1);
}
此函数将全屏显示。
但是我如何让它只在我的位图被触摸时才显示?
float x = event1.getX();
float y = event1.getY();
if(rect.contains((int)x, (int)y))
{
// touch in image
}
//where rect is rectangle of bitmap
您可以实现父布局触摸并将其作为子布局传递给您的视图(您绑定 canvas 的视图)。您可以按照以下方式进行操作:
1) 实现父布局触摸事件
yourparentlayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// this method will pass your parent touch to your canvas parent view
canvasparent.dispatchTouchEvent(event);
return true;
}
});
2) 为您的 canvasparent
实施 onTouch 侦听器
canvasparent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect viewRect = new Rect();
yourimageview.getGlobalVisibleRect(viewRect);
if (!viewRect.contains((int) event.getRawX(), (int) event.getRawY())) {
Log.d("Touch : "," Touching Outside Of your canvasparent");
}else{
Log.d("Touch : ","Touching Inside Of your canvasparent");
}
return false;
}
});
我有这个功能:
@Override
public boolean onTouchEvent(MotionEvent event1)
{
if(event1.getAction()==MotionEvent.ACTION_DOWN){
if(!player.getPlaying() && newGameCreated && reset)
{
player.setPlaying(true);
player.setUp(true);
}
if(player.getPlaying())
{
if(!started)started = true;
reset = false;
player.setUp(true);
}
return true;
}
if(event1.getAction()==MotionEvent.ACTION_UP)
{
player.setUp(false);
return true;
}
return super.onTouchEvent(event1);
}
此函数将全屏显示。
但是我如何让它只在我的位图被触摸时才显示?
float x = event1.getX();
float y = event1.getY();
if(rect.contains((int)x, (int)y))
{
// touch in image
}
//where rect is rectangle of bitmap
您可以实现父布局触摸并将其作为子布局传递给您的视图(您绑定 canvas 的视图)。您可以按照以下方式进行操作:
1) 实现父布局触摸事件
yourparentlayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// this method will pass your parent touch to your canvas parent view
canvasparent.dispatchTouchEvent(event);
return true;
}
});
2) 为您的 canvasparent
实施 onTouch 侦听器canvasparent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect viewRect = new Rect();
yourimageview.getGlobalVisibleRect(viewRect);
if (!viewRect.contains((int) event.getRawX(), (int) event.getRawY())) {
Log.d("Touch : "," Touching Outside Of your canvasparent");
}else{
Log.d("Touch : ","Touching Inside Of your canvasparent");
}
return false;
}
});