用户绘图未显示在画布上

发布于 2024-12-08 18:21:42 字数 2070 浏览 1 评论 0原文

我有这样的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <com.somedomain.drawings.DrawingSurface
        android:layout_width="fill_parent"
        android:layout_height="200dip"
            android:id="@+id/drawingSurface" 
            android:background="@drawable/bg2"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" >

        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="OK"
                android:onClick="onClick"
                android:id="@+id/colorGreenBtn" />

    </LinearLayout>

</RelativeLayout>

当我尝试在 DrawingSurface 上绘图时,绘图未显示。但画布的背景图像正在显示。当我保存它时,绘图将显示在输出上。

当我删除 DrawingSurface 的背景图像并尝试绘制它时,它正在显示。

我想当用户在带有背景图像的画布上绘图时显示绘图。有什么想法吗?非常感谢您的帮助! :)

更新:这是我的绘图代码

@Override
public void run() {
    Canvas canvas = null;

    while (_run){
        if(isDrawing == true){
            try{

                canvas = mSurfaceHolder.lockCanvas(null);
                if(mBitmap == null){
                    mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                }


                final Canvas c = new Canvas (mBitmap);

                c.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0x00000000);

                commandManager.executeAll(c,previewDoneHandler);
                previewPath.draw(c);

                canvas.drawBitmap (mBitmap, 0,  0,null);
            } finally {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }


        }

    }

}

I have an XML layout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <com.somedomain.drawings.DrawingSurface
        android:layout_width="fill_parent"
        android:layout_height="200dip"
            android:id="@+id/drawingSurface" 
            android:background="@drawable/bg2"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" >

        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="OK"
                android:onClick="onClick"
                android:id="@+id/colorGreenBtn" />

    </LinearLayout>

</RelativeLayout>

When I'm trying to draw on the DrawingSurface, the drawing isn't showing. But the background image of the canvas is showing. And when I save it, the drawing is showing on the output.

When I removed the background image of the DrawingSurface and try to draw to it, it is showing.

I want to show the drawing when the user draws on the canvas with the background image. Any ideas? Thanks a lot for any help! :)

Update: Here's my drawing code

@Override
public void run() {
    Canvas canvas = null;

    while (_run){
        if(isDrawing == true){
            try{

                canvas = mSurfaceHolder.lockCanvas(null);
                if(mBitmap == null){
                    mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                }


                final Canvas c = new Canvas (mBitmap);

                c.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0x00000000);

                commandManager.executeAll(c,previewDoneHandler);
                previewPath.draw(c);

                canvas.drawBitmap (mBitmap, 0,  0,null);
            } finally {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }


        }

    }

}

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

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

发布评论

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

评论(1

一刻暧昧 2024-12-15 18:21:42

可以尝试以下几件事:

1)将 ImageView 放在 XML 中的 drawSurface 之前,并将 bg 放入 ImageView 中。

我知道 SurfaceView 与其他视图不同,但你可以尝试一下。

2)也许您可以在绘图线程启动之前将背景绘制到画布上。
canvas.drawBitmap() 可能是您想要使用的。

Couple things to try:

1) put the ImageView before the drawSurface in XML, and put the bg into the ImageView.

I know the surfaceView is different than other view, but you could try out.

2) Maybe you could draw the bg onto the canvas first the drawing thread started.
canvas.drawBitmap() could be the one you wanna use.

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