Home > Back-end >  Display a transparent view over a camera preview?
Display a transparent view over a camera preview?

Time:09-15

I have a view where I display the preview of the camera (previewView).

I added a button (button_load) to choose a picture on the phone and I would like that once this picture is chosen, it is displayed in transparency over the preview of the camera (in PhotoPreChargee).

Except that I can not display a view over the preview of the camera.

My code :

OnCLick (button_load) :

private void loadImage(){
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    intent.setType("image/*");
    intent.putExtra("return-data", true);
    startActivityForResult(intent, 100); 
}

When the picture is chosen :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==100 && resultCode==RESULT_OK) {
            Uri uri = data.getData();
            photoPreChargee.setImageURI(uri); // set URI
            photoPreChargee.setVisibility(View.VISIBLE); // set Visible
            photoPreChargee.setAlpha(0.5F); // transparency
            previewView.setVisibility(View.VISIBLE);  // already Visible
        }
}

The problem is that the photoPreChargee view is always invisible. It doesn't overlay the previewView in transparency.

Here is the layout of my views:

app

Do you have any idea why? How to do it?

Thx for your help :)


SOLUTION (by snachmsm):

In onCreate()

previewView = findViewById(R.id.previewView);
previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);

and now I can display a View over the previewView

CodePudding user response:

assuming previewView is a PreviewView - it should be configured to use TextureView under the hood, old-fashioned SurfaceView (which is also set by default) just won't allow to be overlapped in many cases

use below before start preview (e.g. in onCreate)

previewView.preferredImplementationMode = ImplementationMode.COMPATIBLE

and please do post XMLs in such cases, screenshot of Android Studio preview says nothing, no one can ensure you have set proper XML attributes and this could lead to wrong answers or leaving question without answering by person, which knows a solution, but isn't 100% sure and just don't want to ask for details, comment, argue, etc.

  • Related