ImageCardView.java revision 5358b0ca6ed795892bd097fdf15d41fb6b1a03d2
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import android.content.Context; 17import android.content.res.TypedArray; 18import android.graphics.drawable.Drawable; 19import android.support.v17.leanback.R; 20import android.text.TextUtils; 21import android.util.AttributeSet; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.ImageView; 26import android.widget.ImageView.ScaleType; 27import android.widget.TextView; 28 29/** 30 * A card view with an {@link ImageView} as its main region. 31 */ 32public class ImageCardView extends BaseCardView { 33 34 private ImageView mImageView; 35 private View mInfoArea; 36 private TextView mTitleView; 37 private TextView mContentView; 38 private ImageView mBadgeImage; 39 private ImageView mBadgeFadeMask; 40 41 public ImageCardView(Context context) { 42 this(context, null); 43 } 44 45 public ImageCardView(Context context, AttributeSet attrs) { 46 this(context, attrs, R.attr.imageCardViewStyle); 47 } 48 49 public ImageCardView(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 52 LayoutInflater inflater = LayoutInflater.from(context); 53 View v = inflater.inflate(R.layout.lb_image_card_view, this); 54 55 mImageView = (ImageView) v.findViewById(R.id.main_image); 56 mImageView.setVisibility(View.INVISIBLE); 57 mInfoArea = v.findViewById(R.id.info_field); 58 mTitleView = (TextView) v.findViewById(R.id.title_text); 59 mContentView = (TextView) v.findViewById(R.id.content_text); 60 mBadgeImage = (ImageView) v.findViewById(R.id.extra_badge); 61 mBadgeFadeMask = (ImageView) v.findViewById(R.id.fade_mask); 62 63 if (mInfoArea != null) { 64 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbImageCardView, 65 defStyle, 0); 66 try { 67 setInfoAreaBackground( 68 a.getDrawable(R.styleable.lbImageCardView_infoAreaBackground)); 69 } finally { 70 a.recycle(); 71 } 72 } 73 } 74 75 public final ImageView getMainImageView() { 76 return mImageView; 77 } 78 79 public void setMainImageAdjustViewBounds(boolean adjustViewBounds) { 80 if (mImageView != null) { 81 mImageView.setAdjustViewBounds(adjustViewBounds); 82 } 83 } 84 85 public void setMainImageScaleType(ScaleType scaleType) { 86 if (mImageView != null) { 87 mImageView.setScaleType(scaleType); 88 } 89 } 90 91 /** 92 * Set drawable with fade-in animation. 93 */ 94 public void setMainImage(Drawable drawable) { 95 setMainImage(drawable, true); 96 } 97 98 /** 99 * Set drawable with optional fade-in animation. 100 */ 101 public void setMainImage(Drawable drawable, boolean fade) { 102 if (mImageView == null) { 103 return; 104 } 105 106 mImageView.setImageDrawable(drawable); 107 if (drawable == null) { 108 mImageView.animate().cancel(); 109 mImageView.setAlpha(1f); 110 mImageView.setVisibility(View.INVISIBLE); 111 } else { 112 mImageView.setVisibility(View.VISIBLE); 113 if (fade) { 114 fadeIn(mImageView); 115 } else { 116 mImageView.animate().cancel(); 117 mImageView.setAlpha(1f); 118 } 119 } 120 } 121 122 public void setMainImageDimensions(int width, int height) { 123 ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); 124 lp.width = width; 125 lp.height = height; 126 mImageView.setLayoutParams(lp); 127 } 128 129 public Drawable getMainImage() { 130 if (mImageView == null) { 131 return null; 132 } 133 134 return mImageView.getDrawable(); 135 } 136 137 public Drawable getInfoAreaBackground() { 138 if (mInfoArea != null) { 139 return mInfoArea.getBackground(); 140 } 141 return null; 142 } 143 144 public void setInfoAreaBackground(Drawable drawable) { 145 if (mInfoArea != null) { 146 mInfoArea.setBackground(drawable); 147 if (mBadgeImage != null) { 148 mBadgeImage.setBackground(drawable); 149 } 150 } 151 } 152 153 public void setInfoAreaBackgroundColor(int color) { 154 if (mInfoArea != null) { 155 mInfoArea.setBackgroundColor(color); 156 if (mBadgeImage != null) { 157 mBadgeImage.setBackgroundColor(color); 158 } 159 } 160 } 161 162 public void setTitleText(CharSequence text) { 163 if (mTitleView == null) { 164 return; 165 } 166 167 mTitleView.setText(text); 168 setTextMaxLines(); 169 } 170 171 public CharSequence getTitleText() { 172 if (mTitleView == null) { 173 return null; 174 } 175 176 return mTitleView.getText(); 177 } 178 179 public void setContentText(CharSequence text) { 180 if (mContentView == null) { 181 return; 182 } 183 184 mContentView.setText(text); 185 setTextMaxLines(); 186 } 187 188 public CharSequence getContentText() { 189 if (mContentView == null) { 190 return null; 191 } 192 193 return mContentView.getText(); 194 } 195 196 public void setBadgeImage(Drawable drawable) { 197 if (mBadgeImage == null) { 198 return; 199 } 200 201 if (drawable != null) { 202 mBadgeImage.setImageDrawable(drawable); 203 mBadgeImage.setVisibility(View.VISIBLE); 204 mBadgeFadeMask.setVisibility(View.VISIBLE); 205 } else { 206 mBadgeImage.setVisibility(View.GONE); 207 mBadgeFadeMask.setVisibility(View.GONE); 208 } 209 } 210 211 public Drawable getBadgeImage() { 212 if (mBadgeImage == null) { 213 return null; 214 } 215 216 return mBadgeImage.getDrawable(); 217 } 218 219 private void fadeIn(View v) { 220 v.setAlpha(0f); 221 v.animate().alpha(1f).setDuration(v.getContext().getResources().getInteger( 222 android.R.integer.config_shortAnimTime)).start(); 223 } 224 225 private void setTextMaxLines() { 226 if (TextUtils.isEmpty(getTitleText())) { 227 mContentView.setMaxLines(2); 228 } else { 229 mContentView.setMaxLines(1); 230 } 231 if (TextUtils.isEmpty(getContentText())) { 232 mTitleView.setMaxLines(2); 233 } else { 234 mTitleView.setMaxLines(1); 235 } 236 } 237 238 @Override 239 protected void onDetachedFromWindow() { 240 mImageView.animate().cancel(); 241 mImageView.setAlpha(1f); 242 super.onDetachedFromWindow(); 243 } 244} 245