预览自定义视图集成-Android
自定义视图未显示在“我的相机预览”中。无法找出原因。代码运行正常,只是圆圈没有画在上面。谷歌也搜索了它,但还没有帮助 主类 } XML类 请更换预览自定义视图集成-Android,android,android-layout,android-studio,Android,Android Layout,Android Studio,自定义视图未显示在“我的相机预览”中。无法找出原因。代码运行正常,只是圆圈没有画在上面。谷歌也搜索了它,但还没有帮助 主类 } XML类 请更换 private void onMeasure() { w = getWidth()/2; h = getHeight()/2; } 与 图层顺序是我所缺少的。只是使用了相对布局,并在主活动代码中对其进行了更改。结果是尤里卡 <?xml version="1.0" encoding="utf-8"?> <Relat
private void onMeasure()
{
w = getWidth()/2;
h = getHeight()/2;
}
与
图层顺序是我所缺少的。只是使用了相对布局,并在主活动代码中对其进行了更改。结果是尤里卡
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="303dp"
android:layout_height="470dp"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/Drawing">
</FrameLayout>
<Button
android:id="@+id/button_capture"
android:layout_width="393dp"
android:layout_height="92dp"
android:text="Capture/Stop"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.javacodegeeks.androidvideocaptureexample.MyDrawing
android:id="@+id/Drawing"
android:layout_width="500dp"
android:layout_height="507dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
您得到的cx、cy和半径值是否正确?油漆是什么颜色的?你能把代码贴出来吗?这样我们就可以进行进一步的细节补充了。到目前为止,cx和cy是恒定的。油漆是红色的。如果我删除了onMeasure和decomment,onDraw行,它将显示在布局xml文件中。但在运行时,没有效果。没有变化,仍然没有显示红色圆圈。md=MyDrawingfindViewByIdR.id.Drawing;这条线是我的视图类对吗?xml是否需要一些修复?您发布的xml代码是内容activity_main吗?是的,是activity_main.xml,我可以看到上面画的红色圆圈。但当代码调试时,它不会显示出来。请检查详细的XML。我已经编辑过了,现在已经完成了。你是否替换了onMeasureMethod??还有什么是MyDrawing类的软件包??你需要用受保护的访问说明符覆盖onMeasure,它应该接受我上面发布的两个参数
public class MyDrawing extends View {
Canvas canvas;
int w;
int h;
private static final int DEFAULT_CIRCLE_COLOR = Color.RED;
private int circleColor = DEFAULT_CIRCLE_COLOR;
private Paint paint;
public MyDrawing(Context context) {
super(context);
init(context, null);
}
public MyDrawing(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs)
{
paint = new Paint();
paint.setAntiAlias(true);
}
private void onMeasure()
{
w = getWidth()/2;
h = getHeight()/2;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// int w = getWidth()/2;
// int h = getHeight()/2;
int pl = getPaddingLeft();
int pr = getPaddingRight();
int pt = getPaddingTop();
int pb = getPaddingBottom();
int usableWidth = w - (pl + pr);
int usableHeight = h - (pt + pb);
int radius = Math.min(usableWidth, usableHeight) / 2;
int cx = pl + (usableWidth / 2);
int cy = pt + (usableHeight / 2);
paint.setColor(circleColor);
canvas.drawCircle(cx, cy, radius, paint);
}
}
private void onMeasure()
{
w = getWidth()/2;
h = getHeight()/2;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
w = getWidth()/2;
h = getHeight()/2;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="303dp"
android:layout_height="470dp"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/Drawing">
</FrameLayout>
<Button
android:id="@+id/button_capture"
android:layout_width="393dp"
android:layout_height="92dp"
android:text="Capture/Stop"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.javacodegeeks.androidvideocaptureexample.MyDrawing
android:id="@+id/Drawing"
android:layout_width="500dp"
android:layout_height="507dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
/>
</RelativeLayout>