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.presenters;
16
17import android.animation.ObjectAnimator;
18import android.content.Context;
19import android.graphics.drawable.Drawable;
20import android.support.v17.leanback.supportleanbackshowcase.R;
21import android.support.v17.leanback.widget.ImageCardView;
22import android.view.View;
23import android.widget.ImageView;
24
25/**
26 * This Presenter will display cards which consists of a single icon which will be highlighted by a
27 * surrounding circle when the card is focused. AndroidTV uses these cards for entering settings
28 * menu.
29 */
30public class IconCardPresenter extends ImageCardViewPresenter {
31    private static final int ANIMATION_DURATION = 200;
32
33    public IconCardPresenter(Context context) {
34        super(context, R.style.IconCardTheme);
35    }
36
37    @Override
38    protected ImageCardView onCreateView() {
39        final ImageCardView imageCardView = super.onCreateView();
40        final ImageView image = imageCardView.getMainImageView();
41        image.setBackgroundResource(R.drawable.icon_focused);
42        image.getBackground().setAlpha(0);
43        imageCardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
44            @Override
45            public void onFocusChange(View v, boolean hasFocus) {
46                animateIconBackground(image.getBackground(), hasFocus);
47            }
48        });
49        return imageCardView;
50    }
51
52    private void animateIconBackground(Drawable drawable, boolean hasFocus) {
53        if (hasFocus) {
54            ObjectAnimator.ofInt(drawable, "alpha", 0, 255).setDuration(ANIMATION_DURATION).start();
55        } else {
56            ObjectAnimator.ofInt(drawable, "alpha", 255, 0).setDuration(ANIMATION_DURATION).start();
57        }
58    }
59}
60