在 Android 中绘制到 SurfaceView

Posted

技术标签:

【中文标题】在 Android 中绘制到 SurfaceView【英文标题】:Drawing to a SurfaceView in Android 【发布时间】:2011-07-14 09:10:01 【问题描述】:

我正在尝试对SurfaceView 进行一些非常简单的绘图,但我无法使其正常工作。没有例外,但我也没有看到任何结果。

更准确地说,我正在尝试创建一个SurfaceView 并用一种颜色填充它。

这是我的代码:

public class SvetlinSurfaceViewTestActivity extends Activity 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        SurfaceRenderer renderer = new SurfaceRenderer(this);
        setContentView(renderer);
    


class SurfaceRenderer extends ViewGroup implements SurfaceHolder.Callback 
    private final String TAG = "Svetlin Surface Renderer";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;

    public SurfaceRenderer(Context context) 
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
    

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) 

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
        tryDrawing(holder);
    

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)  
        tryDrawing(holder);
    

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 

    private void tryDrawing(SurfaceHolder holder) 
        Log.i(TAG, "Trying to draw...");

        Canvas canvas = holder.lockCanvas();
        if (canvas == null) 
            Log.e(TAG, "Cannot draw onto the canvas as it's null");
         else 
            drawMyStuff(canvas);
            holder.unlockCanvasAndPost(canvas);
        
    

    private void drawMyStuff(final Canvas canvas) 
        Log.i(TAG, "Drawing...");
        canvas.drawRGB(255, 128, 128);
    

正如预期的那样,我得到 Drawing... 输出了两次。

谁能建议我为什么不能在SurfaceView 上画画?

【问题讨论】:

【参考方案1】:

我删除了代理类 SurfaceRenderer 并将其全部移到我的活动中。它现在正在工作。这是固定代码:

public class SvetlinSurfaceViewTestActivity
    extends Activity
    implements SurfaceHolder.Callback 

    private static final String TAG = "Svetlin SurfaceView";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        SurfaceView view = new SurfaceView(this);
        setContentView(view);
        view.getHolder().addCallback(this);
    

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
        tryDrawing(holder);
    

    @Override
    public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h)  
        tryDrawing(holder);
    

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 

    private void tryDrawing(SurfaceHolder holder) 
        Log.i(TAG, "Trying to draw...");

        Canvas canvas = holder.lockCanvas();
        if (canvas == null) 
            Log.e(TAG, "Cannot draw onto the canvas as it's null");
         else 
            drawMyStuff(canvas);
            holder.unlockCanvasAndPost(canvas);
        
    

    private void drawMyStuff(final Canvas canvas) 
        Random random = new Random();
        Log.i(TAG, "Drawing...");
        canvas.drawRGB(255, 128, 128);
    

【讨论】:

以上是关于在 Android 中绘制到 SurfaceView的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在 FrameLayout 元素中绘制?

如何在 Firebase ML kit Android 中正确绘制检测到的人脸边界框?

Android 重学系列 View的绘制流程(六) 硬件渲染(上)

android中,如何用canvas绘制透明?

Android绘制动画线

Android canvas绘制的可滑动刻度尺