我刚开始学习如何制作自定义视图。我在屏幕底部创建了一个可绘制对象,并在其中创建了一个小圆圈。布局工作得很好,但有一个小问题,不同分辨率的可绘制对象被裁剪。它在 S3 中运行良好,但在其他设备中情况有所不同。
这是我的代码:
@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);
}
这种观点在所有分辨率上都相同的解决方案应该是什么
这是我的布局:
<?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>