仅在Samsung S8设备中获取锁定表面异常

时间:2017-08-07 11:31:54

标签: android surfaceview

我正在使用以下课程并获得我在问题底部粘贴的错误。我在谷歌搜索了很多,很多人有同样的问题,但不同之处在于我只在三星S8(Android v 7.0)中获得此问题,而在任何其他任何版本的设备上都没有。我该如何解决这个问题?

类别:

public class ScratchView extends SurfaceView implements ScratchMethods, SurfaceHolder.Callback {
    // default value constants
    private final int DEFAULT_COLOR = 0xff444444; // default color is dark gray
    List<Path> mPathList = new ArrayList<Path>();
    private Context mContext;
    private WScratchViewThread mThread;
    private int mOverlayColor;
    private Paint mOverlayPaint;
    private boolean mIsScratchable = true;
    private boolean mIsAntiAlias = false;
    private Path path;
    private float startX = 0;
    private float startY = 0;
    private float startX_old = 0;
    private float startY_old = 0;
    private boolean mScratchStart = false;
    private boolean firstTouch = false;
    private boolean secondTouch = false;
    private float Y4suit = 0;

    private float X4value = 0;
    private float Y4value = 0;
    private float mScreenHeight = 0;
    private float mScreenWidth = 0;
    private int sizeFINGER;

    public ScratchView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init(ctx, attrs);
    }

    public ScratchView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;

        // default value
        mOverlayColor = DEFAULT_COLOR;


        sizeFINGER = MagicTrickSharedPreference.getInstance(context).getScratchValue() + Constants.DEFAULT_SCRATCH_ADDITION;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.WScratchView, 0, 0);


        final int indexCount = ta.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.WScratchView_overlayColor:
                    mOverlayColor = ta.getColor(attr, DEFAULT_COLOR);
                    break;
                case R.styleable.WScratchView_revealSize:
                    //sizeFINGER = ta.getDimensionPixelSize(attr, 50);
                    break;
                case R.styleable.WScratchView_antiAlias:
                    mIsAntiAlias = ta.getBoolean(attr, false);
                    break;
                case R.styleable.WScratchView_scratchable:
                    mIsScratchable = ta.getBoolean(attr, true);
                    break;
            }
        }

        setZOrderOnTop(true);
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        holder.setFormat(PixelFormat.TRANSPARENT);


        mOverlayPaint = new Paint();
        mOverlayPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        mOverlayPaint.setStyle(Paint.Style.STROKE);
        mOverlayPaint.setStrokeCap(Paint.Cap.ROUND);
        mOverlayPaint.setStrokeJoin(Paint.Join.ROUND);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        mScreenHeight = dm.heightPixels;
        mScreenWidth = dm.widthPixels;

    }


    @Override
    public void onDraw(Canvas canvas) {

        canvas.drawColor(mOverlayColor);

        //Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        //canvas.drawBitmap(icon, 100, 100, new Paint());

        for (Path path : mPathList) {
            mOverlayPaint.setAntiAlias(mIsAntiAlias);
            mOverlayPaint.setStrokeWidth(sizeFINGER);


            canvas.drawPath(path, mOverlayPaint);
        }

        //icon.recycle();
        //System.gc();


    }


    @Override
    public boolean onTouchEvent(MotionEvent me) {
        synchronized (mThread.getSurfaceHolder()) {
            if (!mIsScratchable) {
                return true;
            }


            switch (me.getAction()) {
                case MotionEvent.ACTION_DOWN:


                    path = new Path();
                    path.moveTo(me.getX(), me.getY());
                    startX = me.getX();
                    startY = me.getY();
                    startX_old = me.getX();
                    startY_old = me.getY();
                    mPathList.add(path);


                    break;
                case MotionEvent.ACTION_MOVE:

                    if (mScratchStart) {

                        if (isScratch(startX_old, me.getX(), startY_old, me.getY())) {
                            startX_old = me.getX();
                            startY_old = me.getY();
                            path.lineTo(me.getX(), me.getY());
                        }
                    } else {
                        if (isScratch(startX, me.getX(), startY, me.getY())) {
                            mScratchStart = true;

                            path.lineTo(me.getX(), me.getY());
                        }
                    }

                    break;
                case MotionEvent.ACTION_UP:

                    mScratchStart = false;

                    break;
            }
            return true;
        }
    }

    private boolean isScratch(float oldX, float x, float oldY, float y) {

        float distance = (float) Math.sqrt(Math.pow(oldX - x, 2) + Math.pow(oldY - y, 2));

        if (distance > 20) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // do nothing
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        mThread = new WScratchViewThread(getHolder(), this);
        mThread.setRunning(true);
        mThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        boolean retry = true;
        mThread.setRunning(false);
        while (retry) {
            try {
                mThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // do nothing but keep retry
            }
        }

    }

    @Override
    public void resetView() {
        synchronized (mThread.getSurfaceHolder()) {
            firstTouch = false;
            secondTouch = false;
            mPathList.clear();
        }
    }

    @Override
    public boolean isScratchable() {
        return mIsScratchable;
    }

    @Override
    public void setScratchable(boolean flag) {
        mIsScratchable = flag;
    }

    @Override
    public void setOverlayColor(int ResId) {
        mOverlayColor = ResId;
    }

    @Override
    public void setRevealSize(int size) {
        sizeFINGER = size;
    }

    @Override
    public void setAntiAlias(boolean flag) {
        mIsAntiAlias = flag;
    }

    class WScratchViewThread extends Thread {
        private SurfaceHolder mSurfaceHolder;
        private ScratchView mView;
        private boolean mRun = false;


        public WScratchViewThread(SurfaceHolder surfaceHolder, ScratchView view) {
            mSurfaceHolder = surfaceHolder;
            mView = view;
        }

        public void setRunning(boolean run) {
            mRun = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return mSurfaceHolder;
        }


        public void run() {


            Canvas c;

            while (mRun) {
                c = null;
                try {
                    //c = mSurfaceHolder.lockCanvas(null);
                    c = mSurfaceHolder.lockCanvas();
                    synchronized (mSurfaceHolder) {
                        if (c != null) {

                            mView.draw(c);
                        }
                    }
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }    
            }


        }
    }

}

错误:

  

08-07 12:02:28.875 3251-3259 /? E / BufferQueueProducer:[SurfaceView -   com.test.tmp / main.ScratchIt] dequeueBuffer:BufferQueue已经   弃

     

08-07 12:02:28.877 12681-12714 / com.test.tmp E / Surface:dequeueBuffer   失败(没有这样的设备)

     

08-07 12:02:28.877 12681-12714 / com.test.tmp E / SurfaceHolder:异常   锁定表面

     

java.lang.IllegalArgumentException异常

     

在android.view.Surface.nativeLockCanvas(原生方法)

     

在android.view.Surface.lockCanvas(Surface.java:322)

     

在   android.view.SurfaceView $ 4.internalLockCanvas(SurfaceView.java:1046)

     

在android.view.SurfaceView $ 4.lockCanvas(SurfaceView.java:1034)

     

在   magictrick.trick.ScratchView $ WScratchViewThread.run(ScratchView.java:252)

0 个答案:

没有答案