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.view.View; 22 23class CardViewApi21 implements CardViewImpl { 24 25 @Override 26 public void initialize(CardViewDelegate cardView, Context context, 27 ColorStateList backgroundColor, float radius, float elevation, float maxElevation) { 28 final RoundRectDrawable background = new RoundRectDrawable(backgroundColor, radius); 29 cardView.setCardBackground(background); 30 31 View view = cardView.getCardView(); 32 view.setClipToOutline(true); 33 view.setElevation(elevation); 34 setMaxElevation(cardView, maxElevation); 35 } 36 37 @Override 38 public void setRadius(CardViewDelegate cardView, float radius) { 39 getCardBackground(cardView).setRadius(radius); 40 } 41 42 @Override 43 public void initStatic() { 44 } 45 46 @Override 47 public void setMaxElevation(CardViewDelegate cardView, float maxElevation) { 48 getCardBackground(cardView).setPadding(maxElevation, 49 cardView.getUseCompatPadding(), cardView.getPreventCornerOverlap()); 50 updatePadding(cardView); 51 } 52 53 @Override 54 public float getMaxElevation(CardViewDelegate cardView) { 55 return getCardBackground(cardView).getPadding(); 56 } 57 58 @Override 59 public float getMinWidth(CardViewDelegate cardView) { 60 return getRadius(cardView) * 2; 61 } 62 63 @Override 64 public float getMinHeight(CardViewDelegate cardView) { 65 return getRadius(cardView) * 2; 66 } 67 68 @Override 69 public float getRadius(CardViewDelegate cardView) { 70 return getCardBackground(cardView).getRadius(); 71 } 72 73 @Override 74 public void setElevation(CardViewDelegate cardView, float elevation) { 75 cardView.getCardView().setElevation(elevation); 76 } 77 78 @Override 79 public float getElevation(CardViewDelegate cardView) { 80 return cardView.getCardView().getElevation(); 81 } 82 83 @Override 84 public void updatePadding(CardViewDelegate cardView) { 85 if (!cardView.getUseCompatPadding()) { 86 cardView.setShadowPadding(0, 0, 0, 0); 87 return; 88 } 89 float elevation = getMaxElevation(cardView); 90 final float radius = getRadius(cardView); 91 int hPadding = (int) Math.ceil(RoundRectDrawableWithShadow 92 .calculateHorizontalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 93 int vPadding = (int) Math.ceil(RoundRectDrawableWithShadow 94 .calculateVerticalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 95 cardView.setShadowPadding(hPadding, vPadding, hPadding, vPadding); 96 } 97 98 @Override 99 public void onCompatPaddingChanged(CardViewDelegate cardView) { 100 setMaxElevation(cardView, getMaxElevation(cardView)); 101 } 102 103 @Override 104 public void onPreventCornerOverlapChanged(CardViewDelegate cardView) { 105 setMaxElevation(cardView, getMaxElevation(cardView)); 106 } 107 108 @Override 109 public void setBackgroundColor(CardViewDelegate cardView, @Nullable ColorStateList color) { 110 getCardBackground(cardView).setColor(color); 111 } 112 113 @Override 114 public ColorStateList getBackgroundColor(CardViewDelegate cardView) { 115 return getCardBackground(cardView).getColor(); 116 } 117 118 private RoundRectDrawable getCardBackground(CardViewDelegate cardView) { 119 return ((RoundRectDrawable) cardView.getCardBackground()); 120 } 121}