Position Shape At Exact Spot On Imageview
I can't get the elements to position correctly on my layout in Android Studio. I have an ImageView with a png background containing the number 2. I have two Views each containing a
Solution 1:
Android is used on a very large number of devices with very different screen sizes and density. Because of this it's hard to do what you want via xml layout. But you can use layout with fixed relations of views so it will look similar to what you want but this isn't good approach.
This example can give you a key. This layout shows 4 views in exact places of screen always.
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="2"><FrameLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@android:color/white" /></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@android:color/white" /></FrameLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="2"><FrameLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@android:color/white" /></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@android:color/white" /></FrameLayout></LinearLayout></LinearLayout>
Something like this one:
Better if you will draw both background (with number 2) and circles in custom view. So you'll know exact sizes and coords of all object and can place them correctly.
Just example of drawing in custom view:
publicclassMainActivityextendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(newMyView(this));
}
publicclassMyViewextendsView {
publicMyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
// TODO Auto-generated method stubsuper.onDraw(canvas);
intx= getWidth();
inty= getHeight();
int radius;
radius = 100;
Paintpaint=newPaint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
canvas.drawPaint(paint);
paint.setColor(Color.WHITE);
canvas.drawCircle(x / 2, y / 2, radius, paint);
paint.setColor(Color.WHITE);
canvas.drawCircle(x / 3, y / 3, radius, paint);
}
}
}
Hope this helps.
Post a Comment for "Position Shape At Exact Spot On Imageview"