I just started learning on how to make custom views .I created a drawable at the bottom of the screen and a small circle within it. The layout works perfectly fine but there is a small issue , the drawable in different resolution is cropped . Its works fine in S3 but in other devices the things are different.
Here is my code :
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
setMeasuredDimension(childWidthMeasureSpec,childHeightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable outer_ring = getResources().getDrawable(R.drawable.outer_ring);
System.out.println("Height now is : "+(canvas.getHeight()));
Rect r1 = new Rect();
r1.top = (canvas.getHeight()-outer_ring.getIntrinsicHeight());
r1.bottom = canvas.getHeight();
r1.left = (canvas.getWidth()-outer_ring.getIntrinsicWidth())/2;
r1.right = canvas.getWidth()/2 +outer_ring.getIntrinsicWidth()/2;
outer_ring.setBounds(r1);
outer_ring.draw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10.0f);
canvas.drawCircle(canvas.getWidth()/2, ((canvas.getHeight()-outer_ring.getIntrinsicHeight()/2)), 20, paint);
}
what should be the solution that this view appears same on all resolution
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.testing.CustomMenu
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myView"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>