GuidanceStylingRelativeLayout.java revision c39d9c75590eca86a5e7e32a8824ba04a0d42e9b
1package android.support.v17.leanback.widget;
2
3import android.content.Context;
4import android.content.res.TypedArray;
5import android.graphics.Paint;
6import android.graphics.drawable.Drawable;
7import android.support.v17.leanback.R;
8import android.util.AttributeSet;
9import android.widget.ImageView;
10import android.widget.RelativeLayout;
11import android.widget.TextView;
12
13/**
14 * Relative layout implementation that lays out child views based on provided keyline percent(
15 * distance of TitleView baseline from the top).
16 *
17 * Repositioning child views in PreDraw callback in {@link GuidanceStylist} was interfering with
18 * fragment transition. To avoid that, we do that in the onLayout pass.
19 */
20class GuidanceStylingRelativeLayout extends RelativeLayout {
21    private float mTitleKeylinePercent;
22
23    public GuidanceStylingRelativeLayout(Context context) {
24        this(context, null);
25    }
26
27    public GuidanceStylingRelativeLayout(Context context, AttributeSet attrs) {
28        this(context, attrs, 0);
29    }
30
31    public GuidanceStylingRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
32        super(context, attrs, defStyle);
33        init();
34    }
35
36    private void init() {
37        TypedArray ta = getContext().getTheme().obtainStyledAttributes(
38                R.styleable.LeanbackGuidedStepTheme);
39        mTitleKeylinePercent = ta.getFloat(R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline,
40                40);
41        ta.recycle();
42    }
43
44    @Override
45    protected void onLayout(boolean changed, int l, int t, int r, int b) {
46        super.onLayout(changed, l, t, r, b);
47
48        TextView mTitleView = (TextView) getRootView().findViewById(R.id.guidance_title);
49        TextView mBreadcrumbView = (TextView) getRootView().findViewById(R.id.guidance_breadcrumb);
50        TextView mDescriptionView = (TextView) getRootView().findViewById(
51                R.id.guidance_description);
52        ImageView mIconView = (ImageView) getRootView().findViewById(R.id.guidance_icon);
53        int mTitleKeylinePixels = (int) (getMeasuredHeight() * mTitleKeylinePercent / 100);
54
55        if (mTitleView != null && mTitleView.getParent() == this) {
56            Paint textPaint = mTitleView.getPaint();
57            int titleViewTextHeight = -textPaint.getFontMetricsInt().top;
58            int mBreadcrumbViewHeight = mBreadcrumbView.getMeasuredHeight();
59            int guidanceTextContainerTop = mTitleKeylinePixels
60                    - titleViewTextHeight - mBreadcrumbViewHeight - mTitleView.getPaddingTop();
61            int offset = guidanceTextContainerTop - mBreadcrumbView.getTop();
62
63            if (mBreadcrumbView != null && mBreadcrumbView.getParent() == this) {
64                mBreadcrumbView.offsetTopAndBottom(offset);
65            }
66
67            mTitleView.offsetTopAndBottom(offset);
68
69            if (mDescriptionView != null && mDescriptionView.getParent() == this) {
70                mDescriptionView.offsetTopAndBottom(offset);
71            }
72        }
73
74        if (mIconView != null && mIconView.getParent() == this) {
75            Drawable drawable = mIconView.getDrawable();
76            if (drawable != null) {
77                mIconView.offsetTopAndBottom(
78                        mTitleKeylinePixels - mIconView.getMeasuredHeight() / 2);
79            }
80        }
81    }
82}
83