简体   繁体   中英

How to dynamically create a SurfaceView with rounded corners

I'm working on a Xamarin.Android project that involves dynamically creating view hierarchies that represent controls. One such control uses the AndroidGameView class, which inherits from SurfaceView and allows drawing with OpenGL ES. I'm trying to figure how to give the view rounded corners. I've tinkered with overriding the OnDraw method or applying a drawable background to clip, but haven't had any success. This question seems similar to my goal, but I could not figure out how to do this dynamically instead of using axml. Is it possible to dynamically give a SurfaceView rounded corners?

There is no direct support for that on Android.

I think the best approach would be to create the rounded rectangle shape you want with a large triangle fan that defines the rounded corners and position it behind the scene and make it opaque. The format for the SurfaceView must be Bitmap.Config.ARGB_8888 and use setOpaque(0) and setAlpha(1.0). The glClearColor() must be transparent black to blend with the Android canvas:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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