Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're trying to solve this problem using a view hierarchy like this:</p> <ul><li>Parent<ul><li>ListView</li><li>InverterView</li></ul></li></ul> <p>Problem is, in this position, <code>InverterView</code> has no control over how <code>ListView</code> is drawn. But you know who does have control over how <code>ListView</code> is drawn? <code>ListView</code>'s parent layout does. In other words, what you really want is a hierarchy like this:</p> <ul><li>Parent<ul><li>InverterLayout<ul><li>ListView</li></ul></li></ul></li></ul> <p>Now <code>InverterLayout</code> is responsible for drawing <code>ListView</code>, and can apply effects to it.</p> <pre><code>class InverterLayout extends FrameLayout { // structure to hold our color filter private Paint paint = new Paint(); // the color filter itself private ColorFilter cf; // the rectangle we want to invert private Rect inversion_rect = new Rect(100, 100, 300, 300); public InverterLayout(Context context) { super(context); // construct the inversion color matrix float[] mat = new float[] { -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0 }; cf = new ColorMatrixColorFilter(new ColorMatrix(mat)); } @Override protected void dispatchDraw(Canvas c) { // create a temporary bitmap to draw the child views Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); Canvas cc = new Canvas(b); // draw them to that temporary bitmap super.dispatchDraw(cc); // copy the temporary bitmap to screen without the inversion filter paint.setColorFilter(null); c.drawBitmap(b, 0, 0, paint); // copy the inverted rectangle paint.setColorFilter(cf); c.drawBitmap(b, inversion_rect, inversion_rect, paint); } } </code></pre> <p>When using this, <strong>ensure your child view has its own background</strong>. If the view is transparent and the window background shows through, that window background will not be inverted, because the <code>InverterLayout</code> has no control over how the window is drawn.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload