ImageCardView.java revision 961a8f12d1fbeeb86ea066f9f2c693abb2ce50f2
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.graphics.drawable.Drawable; 18import android.support.v17.leanback.R; 19import android.text.TextUtils; 20import android.util.AttributeSet; 21import android.view.LayoutInflater; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.ImageView; 25import android.widget.TextView; 26 27/** 28 * A card view with an {@link ImageView} as its main region. 29 */ 30public class ImageCardView extends BaseCardView { 31 32 private ImageView mImageView; 33 private View mInfoArea; 34 private TextView mTitleView; 35 private TextView mContentView; 36 private ImageView mBadgeImage; 37 private ImageView mBadgeFadeMask; 38 39 public ImageCardView(Context context) { 40 this(context, null); 41 } 42 43 public ImageCardView(Context context, AttributeSet attrs) { 44 this(context, attrs, R.attr.imageCardViewStyle); 45 } 46 47 public ImageCardView(Context context, AttributeSet attrs, int defStyle) { 48 super(context, attrs, defStyle); 49 50 LayoutInflater inflater = LayoutInflater.from(context); 51 View v = inflater.inflate(R.layout.lb_image_card_view, this); 52 53 mImageView = (ImageView) v.findViewById(R.id.main_image); 54 mImageView.setVisibility(View.INVISIBLE); 55 mInfoArea = v.findViewById(R.id.info_field); 56 mTitleView = (TextView) v.findViewById(R.id.title_text); 57 mContentView = (TextView) v.findViewById(R.id.content_text); 58 mBadgeImage = (ImageView) v.findViewById(R.id.extra_badge); 59 mBadgeFadeMask = (ImageView) v.findViewById(R.id.fade_mask); 60 } 61 62 public void setMainImage(Drawable drawable) { 63 if (mImageView == null) { 64 return; 65 } 66 67 mImageView.setImageDrawable(drawable); 68 if (drawable == null) { 69 mImageView.setVisibility(View.INVISIBLE); 70 } else { 71 mImageView.setVisibility(View.VISIBLE); 72 fadeIn(mImageView); 73 } 74 } 75 76 public void setMainImageDimensions(int width, int height) { 77 ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); 78 lp.width = width; 79 lp.height = height; 80 mImageView.setLayoutParams(lp); 81 } 82 83 public Drawable getMainImage() { 84 if (mImageView == null) { 85 return null; 86 } 87 88 return mImageView.getDrawable(); 89 } 90 91 public void setTitleText(CharSequence text) { 92 if (mTitleView == null) { 93 return; 94 } 95 96 mTitleView.setText(text); 97 setTextMaxLines(); 98 } 99 100 public CharSequence getTitleText() { 101 if (mTitleView == null) { 102 return null; 103 } 104 105 return mTitleView.getText(); 106 } 107 108 public void setContentText(CharSequence text) { 109 if (mContentView == null) { 110 return; 111 } 112 113 mContentView.setText(text); 114 setTextMaxLines(); 115 } 116 117 public CharSequence getContentText() { 118 if (mContentView == null) { 119 return null; 120 } 121 122 return mContentView.getText(); 123 } 124 125 public void setBadgeImage(Drawable drawable) { 126 if (mBadgeImage == null) { 127 return; 128 } 129 130 if (drawable != null) { 131 mBadgeImage.setImageDrawable(drawable); 132 mBadgeImage.setVisibility(View.VISIBLE); 133 mBadgeFadeMask.setVisibility(View.VISIBLE); 134 } else { 135 mBadgeImage.setVisibility(View.GONE); 136 mBadgeFadeMask.setVisibility(View.GONE); 137 } 138 } 139 140 public Drawable getBadgeImage() { 141 if (mBadgeImage == null) { 142 return null; 143 } 144 145 return mBadgeImage.getDrawable(); 146 } 147 148 private void fadeIn(View v) { 149 v.setAlpha(0f); 150 v.animate().alpha(1f).setDuration(v.getContext().getResources().getInteger( 151 android.R.integer.config_shortAnimTime)).start(); 152 } 153 154 private void setTextMaxLines() { 155 if (TextUtils.isEmpty(getTitleText())) { 156 mContentView.setMaxLines(2); 157 } else { 158 mContentView.setMaxLines(1); 159 } 160 if (TextUtils.isEmpty(getContentText())) { 161 mTitleView.setMaxLines(2); 162 } else { 163 mTitleView.setMaxLines(1); 164 } 165 } 166} 167