在 canvas 上绘制后透明叠加层失去透明度

Transparent overlay loses transparency after drawing on canvas

我创建了一个带有透明覆盖层的简单应用程序,只要我不在覆盖层上绘制任何东西,透明度就会起作用。当我执行 unlockCanvasAndPost 时,透明度会丢失。

这是清单:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="android.example.rectangleoncamera">

        <uses-feature android:name="android.hardware.camera" />
        <uses-permission android:name="android.permission.CAMERA" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="left|top">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.example.rectangleoncamera.views.CameraView
            android:id="@+id/cameraView"
            android:layout_height="match_parent"
            android:layout_width="match_parent">
        </android.example.rectangleoncamera.views.CameraView>
        <android.example.rectangleoncamera.views.RectView
            android:id="@+id/transparentView"
            android:layout_height="match_parent"
            android:layout_width="match_parent">
        </android.example.rectangleoncamera.views.RectView>
    </RelativeLayout>
</RelativeLayout>

下面是三个 Java 类、MainActivity、CameraView 和 RectView(有问题的叠加层):

package android.example.rectangleoncamera;

import android.content.res.Resources;
import android.hardware.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    SurfaceView mCameraView, mTransparentView;
    SurfaceHolder mCameraSurface, mTransparentSurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCameraView = (SurfaceView) findViewById(R.id.cameraView);
        mCameraSurface = mCameraView.getHolder();
        mCameraView.setSecure(true);

        mTransparentView = (SurfaceView) findViewById(R.id.transparentView);
        mTransparentView.setZOrderMediaOverlay(true);

        mTransparentSurface = mTransparentView.getHolder();
        mTransparentSurface.setFormat(PixelFormat.TRANSPARENT);
    }
}






package android.example.rectangleoncamera.views;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
    Camera mCamera;
    SurfaceHolder mHolder;

    public CameraView(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);

    }

    public CameraView(Context context, AttributeSet attrSet)
    {
        super(context,attrSet);
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open(0);
            mCamera.startPreview();
            //Draw();
        }

        catch (Exception e) {

            Log.i("Exception", e.toString());
            e.printStackTrace();
            return;

        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (holder.getSurface() == null) {
            return;
        }

        try {

            mCamera.stopPreview();

        }

        catch (Exception e) {
            Log.i("Exception", e.toString());

        }

        try {

            mCamera.setPreviewDisplay(holder);

            mCamera.startPreview();

        }

        catch (Exception e) {
            Log.i("Exception", e.toString());

        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.release();
    }
}



package android.example.rectangleoncamera.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class RectView extends SurfaceView implements SurfaceHolder.Callback{
    int deviceHeight;
    int deviceWidth;
    SurfaceHolder mHolder;

    public RectView(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);

        deviceHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
        deviceWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public RectView(Context context, AttributeSet attrSet) {
        super(context,attrSet);
        mHolder = getHolder();
        mHolder.addCallback(this);

        setZOrderMediaOverlay(true);
        mHolder.setFormat(PixelFormat.TRANSPARENT);

        deviceHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
        deviceWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    protected void Draw(Canvas canvas) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(3);
        int RectLeft = 1;
        int RectTop = 200 ;
        int RectRight = RectLeft+ deviceWidth -100;
        int RectBottom =RectTop+ 200;
        Rect rec=new Rect((int) RectLeft,(int)RectTop,(int)RectRight,(int)RectBottom);
        canvas.drawRect(rec,paint);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = holder.lockCanvas();
        //Draw(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Canvas canvas = holder.lockCanvas();
        //Draw(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

非常感谢任何帮助!!!

修改您的 draw 方法如下:

protected void draw(SurfaceHolder holder) {
    Canvas canvas = holder.lockCanvas();
    try {
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
      paint.setStyle(Paint.Style.STROKE);
      paint.setColor(Color.GREEN);
      paint.setStrokeWidth(3);
      int RectLeft = 1;
      int RectTop = 200;
      int RectRight = RectLeft + deviceWidth - 100;
      int RectBottom = RectTop + 200;
      canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
      Rect rec = new Rect(RectLeft, RectTop, RectRight, RectBottom);
      canvas.drawRect(rec, paint);
    } finally {
      holder.unlockCanvasAndPost(canvas);
    }
  }