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