ImageCardView.java revision a00bada00bff4a58436a39472ab14ccb7a8f619d
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 subclass of {@link BaseCardView} 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 private boolean mAttachedToWindow; 41 42 public ImageCardView(Context context) { 43 this(context, null); 44 } 45 46 public ImageCardView(Context context, AttributeSet attrs) { 47 this(context, attrs, R.attr.imageCardViewStyle); 48 } 49 50 public ImageCardView(Context context, AttributeSet attrs, int defStyle) { 51 super(context, attrs, defStyle); 52 53 LayoutInflater inflater = LayoutInflater.from(context); 54 View v = inflater.inflate(R.layout.lb_image_card_view, this); 55 56 mImageView = (ImageView) v.findViewById(R.id.main_image); 57 mImageView.setVisibility(View.INVISIBLE); 58 mInfoArea = v.findViewById(R.id.info_field); 59 mTitleView = (TextView) v.findViewById(R.id.title_text); 60 mContentView = (TextView) v.findViewById(R.id.content_text); 61 mBadgeImage = (ImageView) v.findViewById(R.id.extra_badge); 62 mBadgeFadeMask = (ImageView) v.findViewById(R.id.fade_mask); 63 64 if (mInfoArea != null) { 65 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbImageCardView, 66 defStyle, 0); 67 try { 68 setInfoAreaBackground( 69 a.getDrawable(R.styleable.lbImageCardView_infoAreaBackground)); 70 } finally { 71 a.recycle(); 72 } 73 } 74 } 75 76 /** 77 * Returns the main image view. 78 */ 79 public final ImageView getMainImageView() { 80 return mImageView; 81 } 82 83 /** 84 * Enables or disables adjustment of view bounds on the main image. 85 */ 86 public void setMainImageAdjustViewBounds(boolean adjustViewBounds) { 87 if (mImageView != null) { 88 mImageView.setAdjustViewBounds(adjustViewBounds); 89 } 90 } 91 92 /** 93 * Sets the ScaleType of the main image. 94 */ 95 public void setMainImageScaleType(ScaleType scaleType) { 96 if (mImageView != null) { 97 mImageView.setScaleType(scaleType); 98 } 99 } 100 101 /** 102 * Sets the image drawable with fade-in animation. 103 */ 104 public void setMainImage(Drawable drawable) { 105 setMainImage(drawable, true); 106 } 107 108 /** 109 * Sets the image drawable with optional fade-in animation. 110 */ 111 public void setMainImage(Drawable drawable, boolean fade) { 112 if (mImageView == null) { 113 return; 114 } 115 116 mImageView.setImageDrawable(drawable); 117 if (drawable == null) { 118 mImageView.animate().cancel(); 119 mImageView.setAlpha(1f); 120 mImageView.setVisibility(View.INVISIBLE); 121 } else { 122 mImageView.setVisibility(View.VISIBLE); 123 if (fade) { 124 fadeIn(); 125 } else { 126 mImageView.animate().cancel(); 127 mImageView.setAlpha(1f); 128 } 129 } 130 } 131 132 /** 133 * Sets the layout dimensions of the ImageView. 134 */ 135 public void setMainImageDimensions(int width, int height) { 136 ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); 137 lp.width = width; 138 lp.height = height; 139 mImageView.setLayoutParams(lp); 140 } 141 142 /** 143 * Returns the ImageView drawable. 144 */ 145 public Drawable getMainImage() { 146 if (mImageView == null) { 147 return null; 148 } 149 150 return mImageView.getDrawable(); 151 } 152 153 /** 154 * Returns the info area background drawable. 155 */ 156 public Drawable getInfoAreaBackground() { 157 if (mInfoArea != null) { 158 return mInfoArea.getBackground(); 159 } 160 return null; 161 } 162 163 /** 164 * Sets the info area background drawable. 165 */ 166 public void setInfoAreaBackground(Drawable drawable) { 167 if (mInfoArea != null) { 168 mInfoArea.setBackground(drawable); 169 if (mBadgeImage != null) { 170 mBadgeImage.setBackground(drawable); 171 } 172 } 173 } 174 175 /** 176 * Sets the info area background color. 177 */ 178 public void setInfoAreaBackgroundColor(int color) { 179 if (mInfoArea != null) { 180 mInfoArea.setBackgroundColor(color); 181 if (mBadgeImage != null) { 182 mBadgeImage.setBackgroundColor(color); 183 } 184 } 185 } 186 187 /** 188 * Sets the title text. 189 */ 190 public void setTitleText(CharSequence text) { 191 if (mTitleView == null) { 192 return; 193 } 194 195 mTitleView.setText(text); 196 setTextMaxLines(); 197 } 198 199 /** 200 * Returns the title text. 201 */ 202 public CharSequence getTitleText() { 203 if (mTitleView == null) { 204 return null; 205 } 206 207 return mTitleView.getText(); 208 } 209 210 /** 211 * Sets the content text. 212 */ 213 public void setContentText(CharSequence text) { 214 if (mContentView == null) { 215 return; 216 } 217 218 mContentView.setText(text); 219 setTextMaxLines(); 220 } 221 222 /** 223 * Returns the content text. 224 */ 225 public CharSequence getContentText() { 226 if (mContentView == null) { 227 return null; 228 } 229 230 return mContentView.getText(); 231 } 232 233 /** 234 * Sets the badge image drawable. 235 */ 236 public void setBadgeImage(Drawable drawable) { 237 if (mBadgeImage == null) { 238 return; 239 } 240 241 if (drawable != null) { 242 mBadgeImage.setImageDrawable(drawable); 243 mBadgeImage.setVisibility(View.VISIBLE); 244 mBadgeFadeMask.setVisibility(View.VISIBLE); 245 } else { 246 mBadgeImage.setVisibility(View.GONE); 247 mBadgeFadeMask.setVisibility(View.GONE); 248 } 249 } 250 251 /** 252 * Returns the badge image drawable. 253 */ 254 public Drawable getBadgeImage() { 255 if (mBadgeImage == null) { 256 return null; 257 } 258 259 return mBadgeImage.getDrawable(); 260 } 261 262 private void fadeIn() { 263 mImageView.setAlpha(0f); 264 if (mAttachedToWindow) { 265 mImageView.animate().alpha(1f).setDuration(mImageView.getResources().getInteger( 266 android.R.integer.config_shortAnimTime)); 267 } 268 } 269 270 @Override 271 public boolean hasOverlappingRendering() { 272 return false; 273 } 274 275 private void setTextMaxLines() { 276 if (TextUtils.isEmpty(getTitleText())) { 277 mContentView.setMaxLines(2); 278 } else { 279 mContentView.setMaxLines(1); 280 } 281 if (TextUtils.isEmpty(getContentText())) { 282 mTitleView.setMaxLines(2); 283 } else { 284 mTitleView.setMaxLines(1); 285 } 286 } 287 288 @Override 289 protected void onAttachedToWindow() { 290 super.onAttachedToWindow(); 291 mAttachedToWindow = true; 292 if (mImageView.getAlpha() == 0) { 293 fadeIn(); 294 } 295 } 296 297 @Override 298 protected void onDetachedFromWindow() { 299 mAttachedToWindow = false; 300 mImageView.animate().cancel(); 301 mImageView.setAlpha(1f); 302 super.onDetachedFromWindow(); 303 } 304} 305