1/*
2 * Copyright (C) 2015 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 */
14
15package android.support.v17.leanback.supportleanbackshowcase.cards;
16
17import android.content.Context;
18import android.graphics.Bitmap;
19import android.graphics.BitmapFactory;
20import android.support.v17.leanback.supportleanbackshowcase.R;
21import android.support.v17.leanback.supportleanbackshowcase.models.Card;
22import android.support.v17.leanback.widget.BaseCardView;
23import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
24import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.ImageView;
28import android.widget.TextView;
29
30public class CharacterCardView extends BaseCardView {
31
32    public CharacterCardView(Context context) {
33        super(context, null, R.style.CharacterCardStyle);
34        LayoutInflater.from(getContext()).inflate(R.layout.character_card, this);
35        setOnFocusChangeListener(new View.OnFocusChangeListener() {
36            @Override
37            public void onFocusChange(View v, boolean hasFocus) {
38                ImageView mainImage = findViewById(R.id.main_image);
39                View container = findViewById(R.id.container);
40                if (hasFocus) {
41                    container.setBackgroundResource(R.drawable.character_focused);
42                    mainImage.setBackgroundResource(R.drawable.character_focused);
43                } else {
44                    container.setBackgroundResource(R.drawable.character_not_focused_padding);
45                    mainImage.setBackgroundResource(R.drawable.character_not_focused);
46                }
47            }
48        });
49        setFocusable(true);
50    }
51
52    public void updateUi(Card card) {
53        TextView primaryText = findViewById(R.id.primary_text);
54        final ImageView imageView = findViewById(R.id.main_image);
55
56        primaryText.setText(card.getTitle());
57        if (card.getLocalImageResourceName() != null) {
58            int resourceId = card.getLocalImageResourceId(getContext());
59            Bitmap bitmap = BitmapFactory
60                    .decodeResource(getContext().getResources(), resourceId);
61            RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap);
62            drawable.setAntiAlias(true);
63            drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
64            imageView.setImageDrawable(drawable);
65        }
66    }
67
68
69}
70