【问题标题】:Need a way to add a button to this android xamarin sample需要一种向此 android xamarin 示例添加按钮的方法
【发布时间】:2018-06-08 20:00:16
【问题描述】:

我在这里使用了这个示例:https://github.com/xamarin/xamarin-forms-samples/blob/master/CustomRenderers/View/Droid/CameraPreview.cs

获取有关我的项目的摄像头信息。在 IOS 部分,我轻松地在相机源上添加了一个拍照按钮。我试图在 android 端做同样的事情,但我找不到如何做到这一点的方法......我尝试的一切都呈现空白页面或抛出错误。

在此相机源上添加按钮的最简单方法是什么?提前致谢

【问题讨论】:

  • 只需将内容添加到SurfaceView。你试过什么?
  • 我实际上尝试过... SurfaceView 没有 .AddView 方法...我尝试使用几乎所有可能的组合来扩展 .AXML 布局,但这也不起作用(崩溃或空白页).. . 我还尝试在.AddView(surfaceView) 之后直接添加一些按钮与另一个.addView 但这没有明显的效果
  • 也许你能告诉我如何“向 SurfaceView 添加东西”?我比 Android 更喜欢 iOS,这东西对我来说真的很新鲜!

标签: android xamarin button camera surfaceview


【解决方案1】:

您可以在MainPage 中使用RelativeLayout。然后将ButtonCameraPreview 添加到其中。例如:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer"
             x:Class="CustomRenderer.MainPage"
             Padding="0,20,0,0"
             Title="Main Page">
    <ContentPage.Content>
        <RelativeLayout>
            <local:CameraPreview
                Camera="Rear"  x:Name="Camera"                     
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}"/>
            <Button x:Name="button"
                    Text="Button" Clicked="button_Clicked"
                    RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,ElementName=Camera,Property=Height,Factor=.85,Constant=0}"
                    RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,ElementName=Camera,Property=Width,Factor=.05,Constant=0}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=.9,Constant=0}"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=.125,Constant=0}" />
        </RelativeLayout>
    </ContentPage.Content>
</ContentPage>

您可以使用MessagingCenter 触发CameraPreview 的点击事件。

例如在渲染器中:

    protected override void OnElementChanged(ElementChangedEventArgs<CustomRenderer.CameraPreview> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            cameraPreview = new CameraPreview(Context);

            SetNativeControl(cameraPreview);

            MessagingCenter.Subscribe<MainPage>(this, "ButtonClick", (sender) => {
                if (cameraPreview.IsPreviewing)
                {
                    cameraPreview.Preview.StopPreview();
                    cameraPreview.IsPreviewing = false;
                }
                else
                {
                    cameraPreview.Preview.StartPreview();
                    cameraPreview.IsPreviewing = true;
                }
            });
        }

        if (e.OldElement != null)
        {
            // Unsubscribe
            cameraPreview.Click -= OnCameraPreviewClicked;
        }
        if (e.NewElement != null)
        {
            Control.Preview = Camera.Open((int)e.NewElement.Camera);
            // Subscribe
            cameraPreview.Click += OnCameraPreviewClicked;
        }
    }

并且在Button点击事件中:

    private void button_Clicked(object sender, System.EventArgs e)
    {
        MessagingCenter.Send<MainPage>(this, "ButtonClick");
    }

【讨论】:

  • 是的,这就是我认为的解决方案。我本来希望有一个本机按钮,因为我在 iOS 部分实现了它并且它工作得很好。使用这个解决方案,我想我必须在拍摄照片时向 PCL 发送另一条消息......在iOS部分,点击向PCL发送单条消息,更加优雅简洁。谢谢!
【解决方案2】:

这对我有用。 在 CameraPreview 类中只需添加一个新的 View 成员(cameraLayoutView)和一个按钮:

public sealed class CameraPreview : ViewGroup, ISurfaceHolderCallback
{
    View cameraLayoutView;
    global::Android.Widget.Button takePhotoButton;
}

然后在构造函数中实例化这个视图和按钮:

public CameraPreview (Context context)
            : base (context)
{
    surfaceView = new SurfaceView (context);
    AddView (surfaceView);

    Activity activity = this.Context as Activity;
    cameraLayoutView = activity.LayoutInflater.Inflate(Resource.Layout.CameraLayout, this, false);
    AddView(cameraLayoutView);

    takePhotoButton = this.FindViewById<global::Android.Widget.Button>(Resource.Id.takePhotoButton);
    takePhotoButton.Click += TakePhotoButtonTapped;

    windowManager = Context.GetSystemService (Context.WindowService).JavaCast<IWindowManager> ();

    IsPreviewing = false;
    holder = surfaceView.Holder;
    holder.AddCallback (this);
}

async void TakePhotoButtonTapped(object sender, EventArgs e)
{
       //handle the click here    
}

protected override void OnLayout (bool changed, int l, int t, int r, int b)
{
    var msw = MeasureSpec.MakeMeasureSpec (r - l, MeasureSpecMode.Exactly);
    var msh = MeasureSpec.MakeMeasureSpec (b - t, MeasureSpecMode.Exactly);

    surfaceView.Measure (msw, msh);
    surfaceView.Layout (0, 0, r - l, b - t);

    cameraLayoutView.Measure(msw, msh);
    cameraLayoutView.Layout(0, 0, r - l, b - t);
}

Resources/Layout 中添加此布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextureView
        android:id="@+id/textureView"
        android:layout_marginTop="-95dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/toggleFlashButton"
        android:layout_width="37dp"
        android:layout_height="37dp"
        android:layout_gravity="top|left"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/NoFlashButton" />
    <Button
        android:id="@+id/switchCameraButton"
        android:layout_width="35dp"
        android:layout_height="26dp"
        android:layout_gravity="top|right"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/ToggleCameraButton" />
    <Button
        android:id="@+id/takePhotoButton"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_marginBottom="15dp"
        android:layout_gravity="center|bottom"
        android:background="@drawable/TakePhotoButton" />
</FrameLayout>

以及Resources/drawable中的按钮图标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2012-11-19
    • 2018-09-09
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多