两层,但无法在android中显示底层

时间:2012-06-11 05:02:17

标签: android android-layout surfaceview android-canvas

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

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

如果我想划痕并擦除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;
    }

1 个答案:

答案 0 :(得分:2)

您不应该在SurfaceView后面使用imageView,因为SurfaceView不是像常规视图那样绘制的。表面是Z有序的,因此它位于窗口后面,保持着SurfaceView; SurfaceView在其窗口中打孔以允许其表面显示。

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

或者你可以使用myImageView.bringToFront();将它对齐在SurfaceView之上。