1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.support.v7.widget; 17 18import android.content.Context; 19import android.content.res.ColorStateList; 20import android.support.annotation.Nullable; 21import android.support.annotation.RequiresApi; 22import android.view.View; 23 24@RequiresApi(21) 25class CardViewApi21Impl implements CardViewImpl { 26 27 @Override 28 public void initialize(CardViewDelegate cardView, Context context, 29 ColorStateList backgroundColor, float radius, float elevation, float maxElevation) { 30 final RoundRectDrawable background = new RoundRectDrawable(backgroundColor, radius); 31 cardView.setCardBackground(background); 32 33 View view = cardView.getCardView(); 34 view.setClipToOutline(true); 35 view.setElevation(elevation); 36 setMaxElevation(cardView, maxElevation); 37 } 38 39 @Override 40 public void setRadius(CardViewDelegate cardView, float radius) { 41 getCardBackground(cardView).setRadius(radius); 42 } 43 44 @Override 45 public void initStatic() { 46 } 47 48 @Override 49 public void setMaxElevation(CardViewDelegate cardView, float maxElevation) { 50 getCardBackground(cardView).setPadding(maxElevation, 51 cardView.getUseCompatPadding(), cardView.getPreventCornerOverlap()); 52 updatePadding(cardView); 53 } 54 55 @Override 56 public float getMaxElevation(CardViewDelegate cardView) { 57 return getCardBackground(cardView).getPadding(); 58 } 59 60 @Override 61 public float getMinWidth(CardViewDelegate cardView) { 62 return getRadius(cardView) * 2; 63 } 64 65 @Override 66 public float getMinHeight(CardViewDelegate cardView) { 67 return getRadius(cardView) * 2; 68 } 69 70 @Override 71 public float getRadius(CardViewDelegate cardView) { 72 return getCardBackground(cardView).getRadius(); 73 } 74 75 @Override 76 public void setElevation(CardViewDelegate cardView, float elevation) { 77 cardView.getCardView().setElevation(elevation); 78 } 79 80 @Override 81 public float getElevation(CardViewDelegate cardView) { 82 return cardView.getCardView().getElevation(); 83 } 84 85 @Override 86 public void updatePadding(CardViewDelegate cardView) { 87 if (!cardView.getUseCompatPadding()) { 88 cardView.setShadowPadding(0, 0, 0, 0); 89 return; 90 } 91 float elevation = getMaxElevation(cardView); 92 final float radius = getRadius(cardView); 93 int hPadding = (int) Math.ceil(RoundRectDrawableWithShadow 94 .calculateHorizontalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 95 int vPadding = (int) Math.ceil(RoundRectDrawableWithShadow 96 .calculateVerticalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 97 cardView.setShadowPadding(hPadding, vPadding, hPadding, vPadding); 98 } 99 100 @Override 101 public void onCompatPaddingChanged(CardViewDelegate cardView) { 102 setMaxElevation(cardView, getMaxElevation(cardView)); 103 } 104 105 @Override 106 public void onPreventCornerOverlapChanged(CardViewDelegate cardView) { 107 setMaxElevation(cardView, getMaxElevation(cardView)); 108 } 109 110 @Override 111 public void setBackgroundColor(CardViewDelegate cardView, @Nullable ColorStateList color) { 112 getCardBackground(cardView).setColor(color); 113 } 114 115 @Override 116 public ColorStateList getBackgroundColor(CardViewDelegate cardView) { 117 return getCardBackground(cardView).getColor(); 118 } 119 120 private RoundRectDrawable getCardBackground(CardViewDelegate cardView) { 121 return ((RoundRectDrawable) cardView.getCardBackground()); 122 } 123} 124