131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn/*
231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * Copyright (C) 2015 The Android Open Source Project
331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn *
431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * in compliance with the License. You may obtain a copy of the License at
631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn *
731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * http://www.apache.org/licenses/LICENSE-2.0
831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn *
931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * Unless required by applicable law or agreed to in writing, software distributed under the License
1031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * or implied. See the License for the specific language governing permissions and limitations under
1231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn * the License.
1331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn */
1431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
1531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnpackage android.support.v17.leanback.supportleanbackshowcase.cards;
1631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
1731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.content.Context;
1831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.graphics.Bitmap;
1931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.graphics.BitmapFactory;
2031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.support.v17.leanback.supportleanbackshowcase.R;
21ad31f63f5843898de645f6ee1ac244c872ded8ccRobert Hahnimport android.support.v17.leanback.supportleanbackshowcase.models.Card;
2231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.support.v17.leanback.widget.BaseCardView;
2331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.support.v4.graphics.drawable.RoundedBitmapDrawable;
2431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
2531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.view.LayoutInflater;
2631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.widget.ImageView;
2731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.widget.TextView;
2831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
2931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnpublic class TextCardView extends BaseCardView {
3031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
3131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn    public TextCardView(Context context) {
32c0bc6c89bc6fefcced0b13954b6b4debe0c89c73Dake Gu        super(context, null, R.style.TextCardStyle);
3331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        LayoutInflater.from(getContext()).inflate(R.layout.text_icon_card, this);
3431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        setFocusable(true);
3531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn    }
3631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
3731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn    public void updateUi(Card card) {
3831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        TextView extraText = (TextView) findViewById(R.id.extra_text);
3931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        TextView primaryText = (TextView) findViewById(R.id.primary_text);
4031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        final ImageView imageView = (ImageView) findViewById(R.id.main_image);
4131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
4231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        extraText.setText(card.getExtraText());
4331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        primaryText.setText(card.getTitle());
4431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
4531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        // Create a rounded drawable.
4631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        int resourceId = card.getLocalImageResourceId(getContext());
4731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        Bitmap bitmap = BitmapFactory
4831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn                .decodeResource(getContext().getResources(), resourceId);
4931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap);
5031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        drawable.setAntiAlias(true);
5131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        drawable.setCornerRadius(
5231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn                Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
5331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn        imageView.setImageDrawable(drawable);
5431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn    }
5531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn
5631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn}
57