如何创建带圆角的表面视图 [英] How to create surfaceview with rounded corners
问题描述
我很难绕过surfaceview的角落.我正在使用 MjpegView(从surfaceview继承的自定义视图.我已经尝试过这个解决方案:1) 设置带有圆角的自定义可绘制背景2)阅读这篇文章后http://androidtutorialsandtips.blogspot.co.il/2012/01/overriding-ondraw-method-in-surfaceview.html我不确定如何实现圆角,因为我已经实现了一个锁定画布的线程.
I am having a difficulty to round the corners of surfaceview. I am using MjpegView (custom view that inherit from surfaceview. I tried already this solutions: 1) set background with custom drawable with round corners 2) After reading this post http://androidtutorialsandtips.blogspot.co.il/2012/01/overriding-ondraw-method-in-surfaceview.html I am not sure how to implement the round corners as I have already implemented a thread that locks the canvas.
while (mRun)
{
if (surfaceDone)
{
try
{
c = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder)
{
try
{
if (mIn != null)
{
Bitmap bm = mIn.readMjpegFrame();
destRect = destRect(bm.getWidth(), bm.getHeight());
if (streamHeight == -1 && streamWidth == -1)
{
streamWidth = bm.getWidth();
streamHeight = bm.getHeight();
}
c.drawColor(Color.BLACK);
c.drawBitmap(bm, null, destRect, p);
if (showFps)
{
p.setXfermode(mode);
if (ovl != null)
{
height = ((ovlPos & 1) == 1) ? destRect.top : destRect.bottom - ovl.getHeight();
width = ((ovlPos & 8) == 8) ? destRect.left : destRect.right - ovl.getWidth();
c.drawBitmap(ovl, width, height, null);
}
p.setXfermode(null);
frameCounter++;
if ((System.currentTimeMillis() - start) >= 1000)
{
fps = String.valueOf(frameCounter) + "fps";
frameCounter = 0;
start = System.currentTimeMillis();
ovl = makeFpsOverlay(overlayPaint, fps);
}
}
}
}
catch (IOException e)
{
}
}
}
finally
{
if (c != null) mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
推荐答案
如果我正确理解您的问题:
If I understand your question correcly:
创建一个
<FrameLayout ...>
有 2 个孩子:表面视图和它上面的部分透明的覆盖层.(FrameLayout
将其子项绘制在另一个之上.)
create a
<FrameLayout ...>
with 2 children: the surface view and a partially transparent cover layer above it. (FrameLayout
draws its children one on top of another.)
在覆盖层上绘制圆角.
也就是说,您将在 SurfaceView
上方有一个不透明层,并且该不透明层将在其中心有一个带圆角的透明孔.
That is, you will have an opaque layer above the SurfaceView
and that opaque layer will have a transparent hole-with-rounded-corners in its center.
这里有一个例子,我用 50% 透明的黑色层(默认情况下visibility="invisible")和一个带有按钮的 RelativeLayout 覆盖 SurfaceView.
Here's an example where I cover a SurfaceView with a 50%-transparent black layer (visibility="invisible" by default) and a RelativeLayout with buttons inside.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
<View
android:id="@+id/transparency"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#80000000"
android:visibility="invisible"
/>
<RelativeLayout
android:id="@+id/xxxxx"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
...
这篇关于如何创建带圆角的表面视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!