为Android应用程序创建像按钮一样的Snapchat

时间:2014-02-05 11:35:49

标签: android

您好我正在创建一个相机应用程序,我的第一个活动类似于Snapchat。

我已经按照Android相机开发教程(https://developer.android.com/guide/topics/media/camera.html)。

但不幸的是,它没有涵盖我想要的特定类型的按钮(像按钮这样的按钮)。

我想拥有像Snapchat这样的按钮。

这是我当前的视图xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

    <Button
        android:id="@+id/button_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_vertical|center_horizontal"
        android:text="capture" />

    </FrameLayout>

</LinearLayout>

我的主要活动java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    // Create an instance of Camera
    mCamera = getCameraInstance(getApplicationContext());

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    // Add a listener to the Capture button
    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }
    });

}

我认为问题源于渲染xml布局后,教程添加

preview.addView(mPreview);

在布局上,我相信相机预览是在按钮上绘制的。

我只想到两个解决方案。

一个是在preview.addView(mPreview)之后以编程方式添加一个按钮;线。或者,如果有一些z-index我可以在按钮的xml代码上设置?

Snapchat按钮是图像按钮?我想我的整体问题是我如何创建像Snapchat app这样的按钮。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

以编程方式将TMethod = procedure of object; 添加到captureButton,方法与将FrameLayout添加到布局的方式相同。

mPreview

FrameLayout的外观顺序非常重要,因为您无法手动设置视图的z-index。 z-index根据外观顺序自动设置。要添加到// Add mPreview to the layout first // so we can build on top of it FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(mPreview); // Then captureButton will go on top of mPreview Button captureButton = new Button(this); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT ); captureButton.setLayoutParams(params); preview.addView(captureButton); 的第一个视图将是底层(FrameLayout)。连续的图层将构建在它之前的视图之上。

至于使按钮半透明,我建议创建一个drawable并执行类似

的操作
mPreview
相关问题