我想显示来自本机的小部件并使Android视图背景透明,但是背景始终为白色,我想知道如何制作androidview
背景transparent
。
Flutter代码:
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)
在您的Android代码中return view;
之前,只需设置view.setBackgroundColor(Color.TRANSPARENT)
答案 1 :(得分:0)
我知道这个问题已经很老了,但是我遇到了同样的问题,我花了一些时间在对现有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;
}