1

我正在开发一个类似于刮刮卡的 Android 项目。我在RelativeLayout 中有两层。底层包含一个ImageView,顶层包含一个SurfaceView。

问题是显示屏保持黑色,即使我设置了 ImageView 也不显示canvas.drawColor(Color.TRANSPARENT).

想刮擦擦掉SurfaceView的一部分,露出下面的ImageView怎么办?

提前致谢。我很抱歉英语不好。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:padding="10dp"
    android:id="@+id/layoutama"
    >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <ImageButton
            android:id="@+id/reset"
            android:src="@drawable/refresh_btn"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/logo"
            android:paddingTop="10dp"
            android:paddingRight="8dp"
        />

        <ImageButton
            android:id="@+id/back"
            android:src="@drawable/back_btn"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/reset"
            android:paddingTop="10dp"
        />

        <ImageView
            android:id="@+id/logo"
            android:src="@drawable/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

    </RelativeLayout>

    <AbsoluteLayout
        android:id="@+id/absolayo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <RelativeLayout
            android:id="@+id/relatlayo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/image1"
                android:scaleType="fitXY"
            />

            <coba.ngegosok.MainGambar
                android:id="@+id/LayoDalam"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
            />

        </RelativeLayout>
    </AbsoluteLayout>
</LinearLayout>

这是 SurfaceView 代码:

        protected void onDraw(Canvas canvas) {
        // fills the canvas with black
        canvas.drawColor(Color.TRANSPARENT);

        canvas.drawBitmap(eraseableBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, p);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        tw = w;
        th = h;
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    private void touch_start(float x, float y) {
        mX = x;
        mY = y;
        mPath.reset();
        mPath.moveTo(x, y);
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            //mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, p);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        static_x = event.getX();
        static_y = event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            touch_start(static_x, static_y);
        } if (event.getAction() == MotionEvent.ACTION_MOVE) {
                touch_move(static_x, static_y);
        } if (event.getAction() == MotionEvent.ACTION_UP) {
                touch_up(); 
            }
        return true;
    }
4

1 回答 1

2

你不应该在 SurfaceView 后面有 imageView,因为 SurfaceView 不像绘制常规视图那样绘制。表面是 Z 顺序的,因此它位于持有其 SurfaceView 的窗口后面;SurfaceView 在其窗口中打了一个孔以显示其表面。

在您的情况下,我会将 ImageView 放在 SurfaceView 前面并使用 setVisibility(View.VISIBLE); 当你想展示它。

或者,您可以使用 myImageView.bringToFront(); 让它在 SurfaceView 的顶部对齐。

于 2012-06-11T05:48:04.423 回答