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