diff --git a/core/java/android/view/HardwareLayer.java b/core/java/android/view/HardwareLayer.java index a12434c0835e2..41c44f1b9b175 100644 --- a/core/java/android/view/HardwareLayer.java +++ b/core/java/android/view/HardwareLayer.java @@ -16,6 +16,7 @@ package android.view; +import android.annotation.Nullable; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Paint; @@ -51,8 +52,8 @@ final class HardwareLayer { * @param paint The paint used when the layer is drawn into the destination canvas. * @see View#setLayerPaint(android.graphics.Paint) */ - public void setLayerPaint(Paint paint) { - nSetLayerPaint(mFinalizer.get(), paint.getNativeInstance()); + public void setLayerPaint(@Nullable Paint paint) { + nSetLayerPaint(mFinalizer.get(), paint != null ? paint.getNativeInstance() : 0); mRenderer.pushLayerUpdate(this); } diff --git a/core/java/android/view/RenderNode.java b/core/java/android/view/RenderNode.java index a45c18d058cbe..a19254f2a6f66 100644 --- a/core/java/android/view/RenderNode.java +++ b/core/java/android/view/RenderNode.java @@ -259,7 +259,7 @@ public class RenderNode { return nSetLayerType(mNativeRenderNode, layerType); } - public boolean setLayerPaint(Paint paint) { + public boolean setLayerPaint(@Nullable Paint paint) { return nSetLayerPaint(mNativeRenderNode, paint != null ? paint.getNativeInstance() : 0); } diff --git a/core/java/android/view/TextureView.java b/core/java/android/view/TextureView.java index 1be4810779dad..1a712c3916db7 100644 --- a/core/java/android/view/TextureView.java +++ b/core/java/android/view/TextureView.java @@ -16,6 +16,7 @@ package android.view; +import android.annotation.Nullable; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -133,7 +134,6 @@ public class TextureView extends View { */ public TextureView(Context context) { super(context); - init(); } /** @@ -144,7 +144,6 @@ public class TextureView extends View { */ public TextureView(Context context, AttributeSet attrs) { super(context, attrs); - init(); } /** @@ -158,7 +157,6 @@ public class TextureView extends View { */ public TextureView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - init(); } /** @@ -176,11 +174,6 @@ public class TextureView extends View { */ public TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - init(); - } - - private void init() { - mLayerPaint = new Paint(); } /** @@ -260,7 +253,7 @@ public class TextureView extends View { * method will however be taken into account when rendering the content of * this TextureView. * - * @param layerType The ype of layer to use with this view, must be one of + * @param layerType The type of layer to use with this view, must be one of * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or * {@link #LAYER_TYPE_HARDWARE} * @param paint The paint used to compose the layer. This argument is optional @@ -268,16 +261,16 @@ public class TextureView extends View { * {@link #LAYER_TYPE_NONE} */ @Override - public void setLayerType(int layerType, Paint paint) { - if (paint != mLayerPaint) { - mLayerPaint = paint == null ? new Paint() : paint; - invalidate(); - } + public void setLayerType(int layerType, @Nullable Paint paint) { + setLayerPaint(paint); } @Override - public void setLayerPaint(Paint paint) { - setLayerType(/* ignored */ 0, paint); + public void setLayerPaint(@Nullable Paint paint) { + if (paint != mLayerPaint) { + mLayerPaint = paint; + invalidate(); + } } /** diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 6fa2aad2d65f3..83c6e9e1ab833 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -11741,7 +11741,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } } - /** + /** * Utility method to retrieve the inverse of the current mMatrix property. * We cache the matrix to avoid recalculating it when transform properties * have not changed. @@ -15626,7 +15626,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * * @attr ref android.R.styleable#View_layerType */ - public void setLayerType(int layerType, Paint paint) { + public void setLayerType(int layerType, @Nullable Paint paint) { if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) { throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, " + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE"); @@ -15645,8 +15645,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } mLayerType = layerType; - final boolean layerDisabled = (mLayerType == LAYER_TYPE_NONE); - mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint); + mLayerPaint = mLayerType == LAYER_TYPE_NONE ? null : paint; mRenderNode.setLayerPaint(mLayerPaint); // draw() behaves differently if we are on a layer, so we need to @@ -15680,12 +15679,12 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * * @see #setLayerType(int, android.graphics.Paint) */ - public void setLayerPaint(Paint paint) { + public void setLayerPaint(@Nullable Paint paint) { int layerType = getLayerType(); if (layerType != LAYER_TYPE_NONE) { - mLayerPaint = paint == null ? new Paint() : paint; + mLayerPaint = paint; if (layerType == LAYER_TYPE_HARDWARE) { - if (mRenderNode.setLayerPaint(mLayerPaint)) { + if (mRenderNode.setLayerPaint(paint)) { invalidateViewProperty(false, false); } } else { @@ -16855,7 +16854,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint); } else { // use layer paint to draw the bitmap, merging the two alphas, but also restore - int layerPaintAlpha = mLayerPaint.getAlpha(); + int layerPaintAlpha = mLayerPaint != null ? mLayerPaint.getAlpha() : 255; mLayerPaint.setAlpha((int) (alpha * layerPaintAlpha)); canvas.drawBitmap(cache, 0.0f, 0.0f, mLayerPaint); mLayerPaint.setAlpha(layerPaintAlpha);