简体   繁体   English

在相机预览上显示透明视图?

[英]Display a transparent view over a camera preview?

I have a view where I display the preview of the camera ( previewView ).我有一个显示相机预览的视图( 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 ).我添加了一个按钮 ( button_load ) 来选择手机上的一张图片,我希望一旦选择了这张图片,它就会以透明的方式显示在相机的预览中(在PhotoPreChargee中)。

Except that I can not display a view over the preview of the camera.除了我无法显示相机预览的视图。

My code:我的代码:

OnCLick (button_load): OnCLick(按钮加载):

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.问题是photoPreChargee视图总是不可见的。 It doesn't overlay the previewView in transparency.它不会以透明度覆盖previewView

Here is the layout of my views:这是我的视图的布局:

应用程序

Do you have any idea why?你知道为什么吗? How to do it?怎么做?

Thx for your help:)谢谢你的帮助:)


SOLUTION (by snachmsm ):解决方案(通过snachmsm ):

In onCreate()onCreate()

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

and now I can display a View over the previewView现在我可以在 previewView 上显示一个视图

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假设previewViewPreviewView - 它应该被配置为使用TextureView在引擎盖下,老式的SurfaceView (这也是默认设置的)在许多情况下不允许重叠

use below before start preview (eg in onCreate )在开始预览之前使用下面(例如在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.并且请在这种情况下发布 XML,Android Studio 预览的屏幕截图什么也没说,没有人可以确保您设置了正确的 XML 属性,这可能导致错误的答案或在没有人回答的情况下留下问题,这知道解决方案,但不是t 100% 确定,只是不想询问细节、评论、争论等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM