2011-11-22 14 views

Respuesta

2

se crea un TouchDelegate:  

final View parent = (View) findViewById(R.id.touch_delegate_root); 
parent.post(new Runnable() { 
    // Post in the parent's message queue to make sure the parent 
    // lays out its children before we call getHitRect() 
    public void run() { 
     final Rect rect = new Rect(); 
     Button delegate = YourActivityClass.this.mButton; 
     delegate.getHitRect(rect); 
     rect.top -= 20; 
     rect.bottom += 12; // etc 
     parent.setTouchDelegate(new TouchDelegate(rect , delegate)); 
    } 
}); 

referido de here

+6

Con su método pude aumentar el área táctil, pero no pude reducir el área. – Amit

1

se puede resolver utilizando únicamente XML. Simplemente coloque su imagen en un marco y coloque otra vista transparente que conecte para hacer clic en eventos encima de ella. Ajustar el tamaño y la posición con los parámetros de diseño:

<FrameLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<ImageView android:id="your_view" 
    android:clickable="false" 
    <!-- your other attributes --> 
    <!-- ... --> 
    /> 
    <ImageView android:id="the_clickable_view" 
     android:src="@null" 
    <!-- set desired size of clickable area --> 
    <!-- align it inside a frame using: 
    android:gravity and android:margins --> 
    /> 
</FrameLayout> 
+0

Como el área deseada que no se puede hacer clic es "transparente" (supongo que eso significa que no está dentro del área del dibujo) incluso podría deshacerse de la vista añadida y simplemente al agregar FrameLayout lo resolvió: use los atributos de diseño originales en el FrameLayout en lugar de ImageView y haga que ImageView solo sea tan grande como desee que se pueda hacer clic. –

0

No utilice el OnClickListener, pero el OnTouchListener y manejar el área de clic por sí mismo.

Por ejemplo, al escalar el rectángulo táctil y traducirlo al centro de la vista. También podría usar radios o compensaciones manuales.

imageView.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     final Rect rect = new Rect(); 
     v.getHitRect(rect); 

     float scale = 0.5f; 

     final float x = event.getX(); 
     final float y = event.getY(); 

     final float minX = v.getWidth() * 0.5f * (1.0f - scale); 
     final float maxX = v.getWidth() * 0.5f * (1.0f + scale); 

     final float minY = v.getHeight() * 0.5f * (1.0f - scale); 
     final float maxY = v.getHeight() * 0.5f * (1.0f + scale); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       if (x > minX && x < maxX && y > minY && y < maxY) { 
        Log.d("TOUCH", String.valueOf(x) + " " + String.valueOf(y)); 
      } 
      break; 

     } 
     return true; 
    } 
}); 
Cuestiones relacionadas