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.view.View; 2731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.widget.ImageView; 2831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnimport android.widget.TextView; 2931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 3031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahnpublic class CharacterCardView extends BaseCardView { 3131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 3231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn public CharacterCardView(Context context) { 33c0bc6c89bc6fefcced0b13954b6b4debe0c89c73Dake Gu super(context, null, R.style.CharacterCardStyle); 3431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn LayoutInflater.from(getContext()).inflate(R.layout.character_card, this); 3531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn setOnFocusChangeListener(new View.OnFocusChangeListener() { 3631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn @Override 3731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn public void onFocusChange(View v, boolean hasFocus) { 38fa2e2acf79d791a90410025daad438968550d18cAlan Viverette ImageView mainImage = findViewById(R.id.main_image); 3931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn View container = findViewById(R.id.container); 4031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn if (hasFocus) { 4131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn container.setBackgroundResource(R.drawable.character_focused); 4231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn mainImage.setBackgroundResource(R.drawable.character_focused); 4331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } else { 4431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn container.setBackgroundResource(R.drawable.character_not_focused_padding); 4531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn mainImage.setBackgroundResource(R.drawable.character_not_focused); 4631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } 4731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } 4831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn }); 4931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn setFocusable(true); 5031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } 5131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 5231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn public void updateUi(Card card) { 53fa2e2acf79d791a90410025daad438968550d18cAlan Viverette TextView primaryText = findViewById(R.id.primary_text); 54fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final ImageView imageView = findViewById(R.id.main_image); 5531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 5631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn primaryText.setText(card.getTitle()); 5731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn if (card.getLocalImageResourceName() != null) { 5831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn int resourceId = card.getLocalImageResourceId(getContext()); 5931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn Bitmap bitmap = BitmapFactory 6031475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn .decodeResource(getContext().getResources(), resourceId); 6131475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap); 6231475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn drawable.setAntiAlias(true); 6331475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f); 6431475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn imageView.setImageDrawable(drawable); 6531475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } 6631475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn } 6731475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 6831475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn 6931475959eb09fd873551909dd21d00304b6a4ee9Robert Hahn} 70