From 0a4e2e304319bb21da877d4cf59dcdcbb386d3ad Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 17 Jan 2016 09:14:16 -0800 Subject: Fix clipPath not working with Hardware Acceleration before 4.3 --- .../github/daneren2005/dsub/view/CardView.java | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/github/daneren2005/dsub/view/CardView.java b/app/src/main/java/github/daneren2005/dsub/view/CardView.java index 576aa52d..b9e0bcce 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/CardView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/CardView.java @@ -8,12 +8,15 @@ import android.graphics.RectF; import android.os.Build; import android.util.AttributeSet; import android.util.Log; +import android.view.View; import android.widget.FrameLayout; import github.daneren2005.dsub.R; import github.daneren2005.dsub.util.DrawableTint; public class CardView extends FrameLayout{ + private static final String TAG = CardView.class.getSimpleName(); + public CardView(Context context) { super(context); init(context); @@ -37,11 +40,14 @@ public class CardView extends FrameLayout{ @Override public void onDraw(Canvas canvas) { - Path clipPath = new Path(); - - float roundedDp = getResources().getDimension(R.dimen.Card_Radius); - clipPath.addRoundRect(new RectF(canvas.getClipBounds()), roundedDp, roundedDp, Path.Direction.CW); - canvas.clipPath(clipPath); + try { + Path clipPath = new Path(); + float roundedDp = getResources().getDimension(R.dimen.Card_Radius); + clipPath.addRoundRect(new RectF(canvas.getClipBounds()), roundedDp, roundedDp, Path.Direction.CW); + canvas.clipPath(clipPath); + } catch(Exception e) { + Log.e(TAG, "Failed to clip path on canvas", e); + } super.onDraw(canvas); } @@ -51,5 +57,12 @@ public class CardView extends FrameLayout{ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setElevation(getResources().getInteger(R.integer.Card_Elevation)); } + + // clipPath is not supported with Hardware Acceleration before API 18 + // http://stackoverflow.com/questions/8895677/work-around-canvas-clippath-that-is-not-supported-in-android-any-more/8895894#8895894 + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 && isHardwareAccelerated()) { + Log.d(TAG, "Change to software"); + setLayerType(View.LAYER_TYPE_SOFTWARE, null); + } } } -- cgit v1.2.3