2

I have a very simple layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <ImageView
    android:id="@+id/board"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/boardmask" />

</LinearLayout>

and a very simple activity:

public class BoardActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.board_activity);
    ImageView board = (ImageView) findViewById(R.id.board);
    board.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Log.i("OnTouch", event.getX() + " " + event.getY());
            }
            return true;
        }
    });
  }
}

The ImageView is about half the screen vertically, and is centered. Can someone explain why a click at the very top or bottom of the screen generates and OnTouch event, it should be out of the ImageView, shouldn't it?

Best regards, and thank you very much.

pouzzler
  • 1,800
  • 2
  • 20
  • 32
  • I added v.getX and v.getY to my onTouch, and I get 480x800 wich is my phone's screen. How come a view set to wrap_content in a layout set to wrap_content fills the whole screen? – pouzzler Apr 16 '12 at 17:14
  • Can u do a thing and tell me what u observe? In your imageview, set the background to #ff0000. Tell me do you see the imageview extending throughout the screen? – Shubhayu Apr 16 '12 at 17:24
  • the red background covers the whole screen. However, the foreground still only covers about half the screen. – pouzzler Apr 16 '12 at 18:10

1 Answers1

0

I have seen this happen quite often and I have not found where google has explained this. I'll try and explain how I have understood it.

When you add an ImageView and provide a source to it, or add the image later programatically, the ImageView resizes itself according to the dimensions of the image. But at times when the source image is larger than the screen size, it is scaled according to the scaleType (if provided) or just scales the image so that the largest side fits the screen completely and the other side is scaled keeping the dimension intact.

But after the image is inserted into the ImageView, the Bounds of the ImageView is not adjusted automatically to the newly scaled image. For this there is another parameter in the ImageView called android:adjustViewBounds

So try doing this in your Imageview.

<ImageView
android:id="@+id/board"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/boardmask" 
android:adjustViewBounds="true"/>
Shubhayu
  • 13,402
  • 5
  • 33
  • 30