Android Galaxy Nexus上的画布剪辑功能错误

Android Galaxy Nexus上的画布剪辑功能错误,android,android-canvas,ondraw,Android,Android Canvas,Ondraw,我正在制作一个应用程序,它是图片裁剪。 但是Galaxy nexus有一些问题。 Region.Op.DIFFERENCE不起作用。 Desire2.3.3和GalaxyNexus4.1模拟器工作良好。 但不仅适用于GalaxyNexus Real手机 请看代码。。。 这是一个onDraw重写的方法它是扩展的imageview @override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //all

我正在制作一个应用程序,它是图片裁剪。 但是Galaxy nexus有一些问题。 Region.Op.DIFFERENCE不起作用。 Desire2.3.3和GalaxyNexus4.1模拟器工作良好。 但不仅适用于GalaxyNexus Real手机

请看代码。。。 这是一个onDraw重写的方法它是扩展的imageview

@override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //all rectangle   
    getDrawingRect(viewRect);

    //small rectangle
    getDrawingRect(smallRect);
    smallRect.left += 100;
    smallRect.right -= 100;
    smallRect.bottom -= 200;
    smallRect.top += 200;

    // paint  color setting to transparency black
    mNoFocusPaint.setARGB(150, 50, 50, 50);

    // All Rectangle clipping
    canvas.clipRect(viewRect);
    // Small Rectangle clipping
    canvas.clipRect(smallRect, Region.Op.DIFFERENCE);

    // Draw All Rectangle transparency black color it's except small rectangle
    canvas.drawRect(viewRect, mNoFocusPaint);
}
解决了

在清单中添加此代码

    android:hardwareAccelerated="false"

解决了

在清单中添加此代码

    android:hardwareAccelerated="false"

朱贤的回答很好!但在我的情况下,我不想在所有SDK版本中删除整个应用程序的硬件加速。硬件加速画布剪切的问题似乎仅限于4.1.1,因此我采取了为我执行剪切操作的特定视图禁用硬件加速的方法

自定义视图类在本例中为RecyclerView:

public class ClippableRecyclerView extends RecyclerView {

    private final CanvasClipper clipper = new CanvasClipper();

    public ClippableRecyclerView(Context context) {
        super(context);
        configureHardwareAcceleration();
    }

    public ClippableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        configureHardwareAcceleration();
    }

    public ClippableRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        configureHardwareAcceleration();
    }

    public void configureHardwareAcceleration() {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    /**
     * Remove the region from the current clip using a difference operation
     * @param rect
     */
    public void removeFromClipBounds(Rect rect) {
        clipper.removeFromClipBounds(rect);
        invalidate();
    }

    public void resetClipBounds() {
        clipper.resetClipBounds();
        invalidate();
    }

    @Override
    public void onDraw(Canvas c) {
        super.onDraw(c);
        clipper.clipCanvas(c);
    }
}
画布裁剪器类:

public class CanvasClipper {

    private final ArrayList<Rect> clipRegions = new ArrayList<>();

    /**
     * Remove the region from the current clip using a difference operation
     * @param rect
     */
    public void removeFromClipBounds(Rect rect) {
        clipRegions.add(rect);
    }

    public void resetClipBounds() {
        clipRegions.clear();
    }

    public void clipCanvas(Canvas c) {
        if (!clipRegions.isEmpty()) {
            for (Rect clipRegion : clipRegions) {
                c.clipRect(clipRegion, Region.Op.DIFFERENCE);
            }
        }
    }
}

JuHyun的回答很好!但在我的情况下,我不想在所有SDK版本中删除整个应用程序的硬件加速。硬件加速画布剪切的问题似乎仅限于4.1.1,因此我采取了为我执行剪切操作的特定视图禁用硬件加速的方法

自定义视图类在本例中为RecyclerView:

public class ClippableRecyclerView extends RecyclerView {

    private final CanvasClipper clipper = new CanvasClipper();

    public ClippableRecyclerView(Context context) {
        super(context);
        configureHardwareAcceleration();
    }

    public ClippableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        configureHardwareAcceleration();
    }

    public ClippableRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        configureHardwareAcceleration();
    }

    public void configureHardwareAcceleration() {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    /**
     * Remove the region from the current clip using a difference operation
     * @param rect
     */
    public void removeFromClipBounds(Rect rect) {
        clipper.removeFromClipBounds(rect);
        invalidate();
    }

    public void resetClipBounds() {
        clipper.resetClipBounds();
        invalidate();
    }

    @Override
    public void onDraw(Canvas c) {
        super.onDraw(c);
        clipper.clipCanvas(c);
    }
}
画布裁剪器类:

public class CanvasClipper {

    private final ArrayList<Rect> clipRegions = new ArrayList<>();

    /**
     * Remove the region from the current clip using a difference operation
     * @param rect
     */
    public void removeFromClipBounds(Rect rect) {
        clipRegions.add(rect);
    }

    public void resetClipBounds() {
        clipRegions.clear();
    }

    public void clipCanvas(Canvas c) {
        if (!clipRegions.isEmpty()) {
            for (Rect clipRegion : clipRegions) {
                c.clipRect(clipRegion, Region.Op.DIFFERENCE);
            }
        }
    }
}