子类化 SurfaceView 并重写 onDraw() 来更改 SurfaceView 参数以生成所需尺寸的预览

发布于 2024-09-11 17:02:24 字数 1114 浏览 5 评论 0原文

我对 SurfaceView 进行了子类化,并在 Activity 的 onCreate 中实例化它。预览已生成,但控件从未进入 onDraw(),该方法在 SurfaceView 的子类中被重写。这是为什么?

class ActivityClass extends Activity{

    onCreate(){

        mPreview = new Preview(this);
        setContentView(mPreview);
    }
    public void startPreview(){

        rec = new MediaRecorder();
        rec.setVideoSource();.......
        rec.setPreviewDisplay(mPreview.getSurfaceHolder.getSurface());
    } 
}

class Preview extends SurfaceView implements SurfaceHolder.Callback{

    SurfaceHolder mHolder;

    public Preview(Context context){
        super(context);
        mHolder = getHolder();     
        mHolder.addCallback(this);      
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        bringToFront();//this is not
        invalidate();//making a difference
    }

    SurfaceHolder getSurfaceHolder(){

        return mHolder;
    }
    //Surface callback methods implemented here
}

在Surface上绘制预览之前,如果实现的话,不应该把控制权交给onDraw回调吗?

因为 onDraw 回调对 Android 框架说‘你不绘制视图。既然已经实施了我就画出来。”我说得对吗?

那么为什么控件无法进入onDraw()呢?请帮忙。

I have subclassed the SurfaceView and instantiating it in onCreate of the Activity. The preview is generated but the control never enters onDraw() which is overriden in the subclass of SurfaceView. Why is that?

class ActivityClass extends Activity{

    onCreate(){

        mPreview = new Preview(this);
        setContentView(mPreview);
    }
    public void startPreview(){

        rec = new MediaRecorder();
        rec.setVideoSource();.......
        rec.setPreviewDisplay(mPreview.getSurfaceHolder.getSurface());
    } 
}

class Preview extends SurfaceView implements SurfaceHolder.Callback{

    SurfaceHolder mHolder;

    public Preview(Context context){
        super(context);
        mHolder = getHolder();     
        mHolder.addCallback(this);      
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        bringToFront();//this is not
        invalidate();//making a difference
    }

    SurfaceHolder getSurfaceHolder(){

        return mHolder;
    }
    //Surface callback methods implemented here
}

Before drawing the preview on the Surface, shouldn't the control be given to the onDraw callback if it is implemented?

Because onDraw callback says to the Android framework 'you don't draw the view. I will draw it since I have been implemented'. Am I right?

Why then, is the control failing to enter onDraw()? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送君千里 2024-09-18 17:02:24

您只需添加

setWillNotDraw(false)

到构造函数即可。

完成了..:)

You simply have to add

setWillNotDraw(false)

To the constructor.

And its done..:)

放飞的风筝 2024-09-18 17:02:24
public class SelectedRect extends View {
    public SelectedRect(Context context) {
        super(context);     
        bringToFront();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);
        invalidate();       
        this.bringToFront();    
    }
}

在活动课中:

View rect =  new SelectedRect(this);
rect.bringToFront();
this.addView(rect, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
public class SelectedRect extends View {
    public SelectedRect(Context context) {
        super(context);     
        bringToFront();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);
        invalidate();       
        this.bringToFront();    
    }
}

And in activity class :

View rect =  new SelectedRect(this);
rect.bringToFront();
this.addView(rect, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
掩耳倾听 2024-09-18 17:02:24

事情有些可疑。您似乎正在尝试将 SurfaceView 设置为预览显示。在这种情况下,Activity 应该实现 SurfaceHolder.Callback(而不是 SurfaceView)。这样 Activity 就可以知道 SurfaceView 何时已创建并准备好进行绘制。此外,甚至没有必要扩展 SurfaceView:您应该能够使用 SurfaceView 本身。

假设我误解了,并且您确实想出于某种原因扩展表面视图......
我不明白你在 Preview 类中哪里覆盖了 onDraw 。你没有包含这段代码吗?

当你说 onDraw 回调告诉 android '你不绘制视图。既然已经实施了我就画出来’,我不同意。 onDraw 是一个函数,其中 View 绘制所有视图都有的东西,例如背景。 SurfaceView 扩展了视图。当你扩展 SurfaceView 时,你可以重写 onDraw 来告诉 android 除了 View 中原始 onDraw 绘制的内容之外还绘制其他内容。换句话说,Android 总是负责所有的绘制工作;重写 onDraw 只是告诉它在正确的绘制时间时再绘制一些东西。

Something fishy is going on. It seems that you're trying to set the SurfaceView as a preview display. In that case, the Activity should implement SurfaceHolder.Callback (not the SurfaceView). This is so that the Activity can know when the SurfaceView has been created and is ready for drawing. Furthermore, it shouldn't even be necessary to extend SurfaceView: you should be able to user SurfaceView itself.

Assuming I misunderstood and you really want to extend surfaceview for some reason...
I don't see where you overrode onDraw in your Preview class. Did you not include this code?

When you say that the onDraw callback tells android 'you don't draw the view. I will draw it since I have been implemented', I disagree. onDraw is a function where a View draws stuff that all views have, such as the background. SurfaceView extends view. When you extend SurfaceView, you can override onDraw to tell android to draw stuff in addition to what the original onDraw in the View draws. In other words, Android always does all the drawing; overriding onDraw just tells it to draw a few more things when it's the right time to draw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文