0

I need to create a view in Android that involves placing a variable number of images in a specific location on the screen. My problem is that this position should not vary according to the size of the screen. In addition, its position must be relative to the background image. Eg I have the image of a house and I have to arrange the windows and the door through some fixed coordinates. Suggestions?

user1169390
  • 151
  • 11

2 Answers2

1

in addition to your comment on how to detect MotionEvents, add the following method to a LayeredImageView:

public List<Layer> getLayersAt(float x, float y) {
    Iterator<Layer> iter = mLayers.iterator();
    List<Layer> list = new LinkedList<Layer>();
    RectF rect = new RectF();
    Matrix m = getImageMatrix();

    while (iter.hasNext()) {
        Layer layer = iter.next();
        rect.set(layer.drawable.getBounds());
        layer.matrix.mapRect(rect);
        m.mapRect(rect);
        if (rect.contains(x, y)) {
            list.add(layer);
        }
    }
    return list;
}

and you can call it like this:

public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    List<Layer> list = iv.getLayersAt(x, y);
}
pskink
  • 23,874
  • 6
  • 66
  • 77
0

You can use android:weightSum property for your LinearLayout and then according to the weight number divide your views on LinearLayout with android:layout_weight. Here I have an example:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="70"
    android:gravity="bottom|center_horizontal"
    android:orientation="horizontal"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/ivHamsa"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:scaleType="fitCenter"
        android:src="@drawable/hamsa_icon" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:orientation="horizontal" >
</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="70"
        android:gravity="top|center_horizontal"
        android:orientation="horizontal"
        android:weightSum="100" >

        <ImageView
            android:id="@+id/ivAndroid"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:scaleType="fitCenter"
            android:background="@drawable/rounded_white"
            android:layout_margin="5dp"
            android:src="@drawable/android_icon" />

        <ImageView
            android:id="@+id/ivBook"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:scaleType="fitCenter"
            android:background="@drawable/rounded_white"
            android:layout_margin="5dp"
            android:src="@drawable/book_icon" />

        <ImageView
            android:id="@+id/ivMusic"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:scaleType="fitCenter"
            android:background="@drawable/rounded_white"
            android:layout_margin="5dp"
            android:src="@drawable/music_icon" />

        <ImageView
            android:id="@+id/ivVideo"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:scaleType="fitCenter"
            android:background="@drawable/rounded_white"
            android:layout_margin="5dp"
            android:src="@drawable/video_icon" />

        <ImageView
            android:id="@+id/ivPhoto"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:scaleType="fitCenter"
            android:background="@drawable/rounded_white"
            android:layout_margin="5dp"
            android:src="@drawable/photo_icon" />
    </LinearLayout>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:orientation="horizontal" >
</LinearLayout>

Majid Daeinejad
  • 1,037
  • 8
  • 19