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?
Asked
Active
Viewed 933 times
0
-
see my answer for this question http://stackoverflow.com/questions/16729169/how-to-maintain-multi-layers-of-imageviews-and-keep-their-aspect-ratio-based-on – pskink Oct 08 '13 at 13:45
-
is exactly what I was looking for, but I need to be able to click on each element added to the main layer. – user1169390 Oct 08 '13 at 14:24
-
see commented out limes – pskink Oct 08 '13 at 14:39
2 Answers
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