1

我正在制作一个应用它的图片裁剪。
但 Galaxy nexus 存在一些问题。
Region.Op.DIFFERENCE 不起作用。
Desire(2.3.3) 和 GalaxyNexus(4.1) 模拟器运行良好。
但不仅适用于 GalaxyNexus Real Phone

欲望(2.3.3)运作良好

Galaxy Nexus (4.1.1) 问题

请查看代码...这是一个 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);
}
4

2 回答 2

2

解决了!

在清单中添加此代码

    android:hardwareAccelerated="false"

:)

于 2012-12-19T07:28:31.770 回答
2

朱铉的回答太棒了!但是,就我而言,我不想在所有 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);
            }
        }
    }
}
于 2015-05-20T19:16:39.813 回答