Как сделать фон Flutter androidview прозрачным или установить фон androidview?

Я хочу показать виджет из родного и сделать фон представления Android прозрачным, но фон всегда белый, я хочу знать, как сделать фон androidviewtransparent.

Код флаттера:

Container(
            width: 300.0,
            height: 300.0,
            alignment: Alignment.center,
            color: Colors.green,
            child: SizedBox(
              width: 250.0,
              height: 250.0,
              child: AndroidView(
                viewType: 'AndroidViewDemo',
                onPlatformViewCreated: (id) {
                  print("onPlatformViewCreated:$id");
                },
              ),
            ),
          )

Код Android

public PlatformView create(Context context, int i, Object o) {
    final ImageView imageView = new ImageView(context);
    imageView.setLayoutParams(new ViewGroup.LayoutParams(300, 300));
    imageView.setImageDrawable(context.getDrawable(R.mipmap.ic_launcher));
    imageView.setBackgroundColor(Color.YELLOW);
    PlatformView view = new PlatformView() {
        @Override
        public View getView() {
            return imageView;
        }
        @Override
        public void dispose() {
        }
    };
    return view;
}  
0
0
652
2

Ответы 2

Перед return view; в вашем коде Android просто установите view.setBackgroundColor(Color.TRANSPARENT)

Я знаю, что этот вопрос довольно старый, но я столкнулся с той же проблемой, и мне потребовалось некоторое время, чтобы найти решение в комментарии к существующей проблеме Fluter:

https://github.com/flutter/flutter/issues/26505#issuecomment-473823972

Вот версия на Java, адаптированная к вашему коду:



    public PlatformView create(Context context, int i, Object o) {
        final ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(300, 300));
        imageView.setImageDrawable(context.getDrawable(R.mipmap.ic_launcher));
        imageView.setBackgroundColor(Color.YELLOW);
        PlatformView view = new PlatformView() {
            @Override
            public View getView() {
                makeWindowTransparent();
                return imageView;
            }

            @Override
            public void dispose() {
            }

            private void makeWindowTransparent() {
                imageView.post(() -> {
                    try {
                        ViewParent parent = imageView.getParent();

                        if (parent == null) return;

                        while(parent.getParent() != null) {
                            parent = parent.getParent();
                        }

                        Object decorView = parent.getClass().getDeclaredMethod("getView").invoke(parent);
                        final Field windowField = decorView.getClass().getDeclaredField("mWindow");
                        windowField.setAccessible(true);

                        final Window window = (Window)windowField.get(decorView);

                        windowField.setAccessible(false);

                        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    } catch(Exception e) {
                        // log the exception
                    }
                });
            }
        };

        return view;
    }

Другие вопросы по теме