Creating a Snapchat Button for an Android Application

Hi, I am creating a camera app and my first activity is like snapchat.

I followed the instructions for installing the camera for Android ( https://developer.android.com/guide/topics/media/camera.html ).

But, unfortunately, it did not cover the specific type of button that I want (snapchat as a button).

I want to have buttons like snapchat.

Here is my current xml view:

<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>

And my mainactivity 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);
        }
    });

}

I believe that the problem arises because after rendering the xml-layout, the tutorial adds

preview.addView(mPreview);

Above the layout, which, in my opinion, a camera preview is drawn above the button.

I was only thinking of two solutions to this.

.addView(mPreview); . - z-, xml- ?

snapchat - ? , , , snapchat.

.

+4
1

captureButton FrameLayout , mPreview .

// 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, z- . Z- . , FrameLayout, (mPreview). , .

, , -

Drawable cameraButton = getResources().getDrawable(R.drawable.drawableName);
cameraButton.setAlpha(170);
captureButton.setBackground(cameraButton);
+1

Source: https://habr.com/ru/post/1525400/


All Articles