我需要一种方法将按钮添加到此Android Xamarin示例中 [英] Need a way to add a button to this android xamarin sample

查看:0
本文介绍了我需要一种方法将按钮添加到此Android Xamarin示例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

来获取我的项目的摄像画面。在iOS部分,我很容易地在摄像头上添加了一个拍摄按钮。我正试图在Android端做同样的事情,但我找不到一种方法来做到这一点…我尝试的所有内容都呈现为空白页或引发错误。

在此摄像机视频源上添加按钮的最简单方法是什么?提前感谢

推荐答案

这对我很有效。 在CameraPview类中,只需添加一个新的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);
}

也在资源/布局中添加此布局:

<?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中的按钮图标。

这篇关于我需要一种方法将按钮添加到此Android Xamarin示例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆