Example usage for android.widget TextView getAlpha

List of usage examples for android.widget TextView getAlpha

Introduction

In this page you can find the example usage for android.widget TextView getAlpha.

Prototype

@ViewDebug.ExportedProperty(category = "drawing")
public float getAlpha() 

Source Link

Document

The opacity of the view.

Usage

From source file:com.android.tv.menu.MenuLayoutManager.java

private void setTempTitleView(TextView dest, TextView src) {
    dest.setVisibility(View.VISIBLE);
    dest.setText(src.getText());//from w  ww. j a  va2 s .c o  m
    dest.setTranslationY(0.0f);
    if (src.getVisibility() == View.VISIBLE) {
        dest.setAlpha(src.getAlpha());
        dest.setScaleX(src.getScaleX());
        dest.setScaleY(src.getScaleY());
    } else {
        dest.setAlpha(0.0f);
        dest.setScaleX(1.0f);
        dest.setScaleY(1.0f);
    }
    View parent = (View) src.getParent();
    dest.setLeft(src.getLeft() + parent.getLeft());
    dest.setRight(src.getRight() + parent.getLeft());
    dest.setTop(src.getTop() + parent.getTop());
    dest.setBottom(src.getBottom() + parent.getTop());
}

From source file:com.android.tv.menu.MenuLayoutManager.java

/**
 * Move the current selection to the given {@code position} with animation.
 * The animation specification is included in http://b/21069476
 *//*  w  ww .j a va 2  s  .  co  m*/
public void setSelectedPositionSmooth(final int position) {
    if (DEBUG) {
        Log.d(TAG, "setSelectedPositionSmooth(position=" + position + ") {previousPosition=" + mSelectedPosition
                + "}");
    }
    if (mMenuView.getVisibility() != View.VISIBLE) {
        setSelectedPosition(position);
        return;
    }
    if (mSelectedPosition == position) {
        return;
    }
    boolean oldIndexValid = Utils.isIndexValid(mMenuRowViews, mSelectedPosition);
    SoftPreconditions.checkState(oldIndexValid, TAG, "No previous selection: " + mSelectedPosition);
    if (!oldIndexValid) {
        return;
    }
    boolean newIndexValid = Utils.isIndexValid(mMenuRowViews, position);
    SoftPreconditions.checkArgument(newIndexValid, TAG, "position " + position);
    if (!newIndexValid) {
        return;
    }
    MenuRow row = mMenuRows.get(position);
    if (!row.isVisible()) {
        Log.e(TAG, "Moving to the invisible row: " + position);
        return;
    }
    if (mAnimatorSet != null) {
        // Do not cancel the animation here. The property values should be set to the end values
        // when the animation finishes.
        mAnimatorSet.end();
    }
    if (mTitleFadeOutAnimator != null) {
        // Cancel the animation instead of ending it in order that the title animation starts
        // again from the intermediate state.
        mTitleFadeOutAnimator.cancel();
    }
    final int oldPosition = mSelectedPosition;
    mSelectedPosition = position;
    if (DEBUG)
        dumpChildren("startRowAnimation()");

    MenuRowView currentView = mMenuRowViews.get(position);
    // Show the children of the next row.
    currentView.getTitleView().setVisibility(View.VISIBLE);
    currentView.getContentsView().setVisibility(View.VISIBLE);
    // Request focus after the new contents view shows up.
    mMenuView.requestFocus();
    if (mTempTitleViewForOld == null) {
        // Initialize here because we don't know when the views are inflated.
        mTempTitleViewForOld = (TextView) mMenuView.findViewById(R.id.temp_title_for_old);
        mTempTitleViewForCurrent = (TextView) mMenuView.findViewById(R.id.temp_title_for_current);
    }

    // Animations.
    mPropertyValuesAfterAnimation.clear();
    List<Animator> animators = new ArrayList<>();
    boolean scrollDown = position > oldPosition;
    List<Rect> layouts = getViewLayouts(mMenuView.getLeft(), mMenuView.getTop(), mMenuView.getRight(),
            mMenuView.getBottom());

    // Old row.
    MenuRow oldRow = mMenuRows.get(oldPosition);
    MenuRowView oldView = mMenuRowViews.get(oldPosition);
    View oldContentsView = oldView.getContentsView();
    // Old contents view.
    animators.add(createAlphaAnimator(oldContentsView, 1.0f, 0.0f, 1.0f, mLinearOutSlowIn)
            .setDuration(mOldContentsFadeOutDuration));
    final TextView oldTitleView = oldView.getTitleView();
    setTempTitleView(mTempTitleViewForOld, oldTitleView);
    Rect oldLayoutRect = layouts.get(oldPosition);
    if (scrollDown) {
        // Old title view.
        if (oldRow.hideTitleWhenSelected() && oldTitleView.getVisibility() != View.VISIBLE) {
            // This case is not included in the animation specification.
            mTempTitleViewForOld.setScaleX(1.0f);
            mTempTitleViewForOld.setScaleY(1.0f);
            animators.add(createAlphaAnimator(mTempTitleViewForOld, 0.0f, oldView.getTitleViewAlphaDeselected(),
                    mFastOutLinearIn));
            int offset = oldLayoutRect.top - mTempTitleViewForOld.getTop();
            animators.add(createTranslationYAnimator(mTempTitleViewForOld, offset + mRowScrollUpAnimationOffset,
                    offset));
        } else {
            animators
                    .add(createScaleXAnimator(mTempTitleViewForOld, oldView.getTitleViewScaleSelected(), 1.0f));
            animators
                    .add(createScaleYAnimator(mTempTitleViewForOld, oldView.getTitleViewScaleSelected(), 1.0f));
            animators.add(createAlphaAnimator(mTempTitleViewForOld, oldTitleView.getAlpha(),
                    oldView.getTitleViewAlphaDeselected(), mLinearOutSlowIn));
            animators.add(createTranslationYAnimator(mTempTitleViewForOld, 0,
                    oldLayoutRect.top - mTempTitleViewForOld.getTop()));
        }
        oldTitleView.setAlpha(oldView.getTitleViewAlphaDeselected());
        oldTitleView.setVisibility(View.INVISIBLE);
    } else {
        Rect currentLayoutRect = new Rect(layouts.get(position));
        // Old title view.
        // The move distance in the specification is 32dp(mRowScrollUpAnimationOffset).
        // But if the height of the upper row is small, the upper row will move down a lot. In
        // this case, this row needs to move more than the specification to avoid the overlap of
        // the two titles.
        // The maximum is to the top of the start position of mTempTitleViewForOld.
        int distanceCurrentTitle = currentLayoutRect.top - currentView.getTop();
        int distance = Math.max(mRowScrollUpAnimationOffset, distanceCurrentTitle);
        int distanceToTopOfSecondTitle = oldLayoutRect.top - mRowScrollUpAnimationOffset - oldView.getTop();
        animators.add(
                createTranslationYAnimator(oldTitleView, 0.0f, Math.min(distance, distanceToTopOfSecondTitle)));
        animators.add(createAlphaAnimator(oldTitleView, 1.0f, 0.0f, 1.0f, mLinearOutSlowIn)
                .setDuration(mOldContentsFadeOutDuration));
        animators.add(createScaleXAnimator(oldTitleView, oldView.getTitleViewScaleSelected(), 1.0f));
        animators.add(createScaleYAnimator(oldTitleView, oldView.getTitleViewScaleSelected(), 1.0f));
        mTempTitleViewForOld.setScaleX(1.0f);
        mTempTitleViewForOld.setScaleY(1.0f);
        animators.add(createAlphaAnimator(mTempTitleViewForOld, 0.0f, oldView.getTitleViewAlphaDeselected(),
                mFastOutLinearIn));
        int offset = oldLayoutRect.top - mTempTitleViewForOld.getTop();
        animators.add(
                createTranslationYAnimator(mTempTitleViewForOld, offset - mRowScrollUpAnimationOffset, offset));
    }
    // Current row.
    Rect currentLayoutRect = new Rect(layouts.get(position));
    TextView currentTitleView = currentView.getTitleView();
    View currentContentsView = currentView.getContentsView();
    currentContentsView.setAlpha(0.0f);
    if (scrollDown) {
        // Current title view.
        setTempTitleView(mTempTitleViewForCurrent, currentTitleView);
        // The move distance in the specification is 32dp(mRowScrollUpAnimationOffset).
        // But if the height of the upper row is small, the upper row will move up a lot. In
        // this case, this row needs to start the move from more than the specification to avoid
        // the overlap of the two titles.
        // The maximum is to the top of the end position of mTempTitleViewForCurrent.
        int distanceOldTitle = oldView.getTop() - oldLayoutRect.top;
        int distance = Math.max(mRowScrollUpAnimationOffset, distanceOldTitle);
        int distanceTopOfSecondTitle = currentView.getTop() - mRowScrollUpAnimationOffset
                - currentLayoutRect.top;
        animators.add(createTranslationYAnimator(currentTitleView, Math.min(distance, distanceTopOfSecondTitle),
                0.0f));
        currentView.setTop(currentLayoutRect.top);
        ObjectAnimator animator = createAlphaAnimator(currentTitleView, 0.0f, 1.0f, mFastOutLinearIn)
                .setDuration(mCurrentContentsFadeInDuration);
        animator.setStartDelay(mOldContentsFadeOutDuration);
        currentTitleView.setAlpha(0.0f);
        animators.add(animator);
        animators.add(createScaleXAnimator(currentTitleView, 1.0f, currentView.getTitleViewScaleSelected()));
        animators.add(createScaleYAnimator(currentTitleView, 1.0f, currentView.getTitleViewScaleSelected()));
        animators.add(createTranslationYAnimator(mTempTitleViewForCurrent, 0.0f, -mRowScrollUpAnimationOffset));
        animators.add(createAlphaAnimator(mTempTitleViewForCurrent, currentView.getTitleViewAlphaDeselected(),
                0, mLinearOutSlowIn));
        // Current contents view.
        animators.add(createTranslationYAnimator(currentContentsView, mRowScrollUpAnimationOffset, 0.0f));
        animator = createAlphaAnimator(currentContentsView, 0.0f, 1.0f, mFastOutLinearIn)
                .setDuration(mCurrentContentsFadeInDuration);
        animator.setStartDelay(mOldContentsFadeOutDuration);
        animators.add(animator);
    } else {
        currentView.setBottom(currentLayoutRect.bottom);
        // Current title view.
        int currentViewOffset = currentLayoutRect.top - currentView.getTop();
        animators.add(createTranslationYAnimator(currentTitleView, 0, currentViewOffset));
        animators.add(createAlphaAnimator(currentTitleView, currentView.getTitleViewAlphaDeselected(), 1.0f,
                mFastOutSlowIn));
        animators.add(createScaleXAnimator(currentTitleView, 1.0f, currentView.getTitleViewScaleSelected()));
        animators.add(createScaleYAnimator(currentTitleView, 1.0f, currentView.getTitleViewScaleSelected()));
        // Current contents view.
        animators.add(createTranslationYAnimator(currentContentsView,
                currentViewOffset - mRowScrollUpAnimationOffset, currentViewOffset));
        ObjectAnimator animator = createAlphaAnimator(currentContentsView, 0.0f, 1.0f, mFastOutLinearIn)
                .setDuration(mCurrentContentsFadeInDuration);
        animator.setStartDelay(mOldContentsFadeOutDuration);
        animators.add(animator);
    }
    // Next row.
    int nextPosition;
    if (scrollDown) {
        nextPosition = findNextVisiblePosition(position);
        if (nextPosition != -1) {
            MenuRowView nextView = mMenuRowViews.get(nextPosition);
            Rect nextLayoutRect = layouts.get(nextPosition);
            animators.add(createTranslationYAnimator(nextView,
                    nextLayoutRect.top + mRowScrollUpAnimationOffset - nextView.getTop(),
                    nextLayoutRect.top - nextView.getTop()));
            animators.add(createAlphaAnimator(nextView, 0.0f, 1.0f, mFastOutLinearIn));
        }
    } else {
        nextPosition = findNextVisiblePosition(oldPosition);
        if (nextPosition != -1) {
            MenuRowView nextView = mMenuRowViews.get(nextPosition);
            animators.add(createTranslationYAnimator(nextView, 0, mRowScrollUpAnimationOffset));
            animators.add(createAlphaAnimator(nextView, nextView.getTitleViewAlphaDeselected(), 0.0f, 1.0f,
                    mLinearOutSlowIn));
        }
    }
    // Other rows.
    int count = mMenuRowViews.size();
    for (int i = 0; i < count; ++i) {
        MenuRowView view = mMenuRowViews.get(i);
        if (view.getVisibility() == View.VISIBLE && i != oldPosition && i != position && i != nextPosition) {
            Rect rect = layouts.get(i);
            animators.add(createTranslationYAnimator(view, 0, rect.top - view.getTop()));
        }
    }
    // Run animation.
    final List<ViewPropertyValueHolder> propertyValuesAfterAnimation = new ArrayList<>();
    propertyValuesAfterAnimation.addAll(mPropertyValuesAfterAnimation);
    mAnimatorSet = new AnimatorSet();
    mAnimatorSet.playTogether(animators);
    mAnimatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animator) {
            if (DEBUG)
                dumpChildren("onRowAnimationEndBefore");
            mAnimatorSet = null;
            // The property values which are different from the end values and need to be
            // changed after the animation are set here.
            // e.g. setting translationY to 0, alpha of the contents view to 1.
            for (ViewPropertyValueHolder holder : propertyValuesAfterAnimation) {
                holder.property.set(holder.view, holder.value);
            }
            oldTitleView.setVisibility(View.VISIBLE);
            mMenuRowViews.get(oldPosition).onDeselected();
            mMenuRowViews.get(position).onSelected(true);
            mTempTitleViewForOld.setVisibility(View.GONE);
            mTempTitleViewForCurrent.setVisibility(View.GONE);
            layout(mMenuView.getLeft(), mMenuView.getTop(), mMenuView.getRight(), mMenuView.getBottom());
            if (DEBUG)
                dumpChildren("onRowAnimationEndAfter");

            MenuRow currentRow = mMenuRows.get(position);
            if (currentRow.hideTitleWhenSelected()) {
                View titleView = mMenuRowViews.get(position).getTitleView();
                mTitleFadeOutAnimator = createAlphaAnimator(titleView, titleView.getAlpha(), 0.0f,
                        mLinearOutSlowIn);
                mTitleFadeOutAnimator.setStartDelay(TITLE_SHOW_DURATION_BEFORE_HIDDEN_MS);
                mTitleFadeOutAnimator.addListener(new AnimatorListenerAdapter() {
                    private boolean mCanceled;

                    @Override
                    public void onAnimationCancel(Animator animator) {
                        mCanceled = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        mTitleFadeOutAnimator = null;
                        if (!mCanceled) {
                            mMenuRowViews.get(position).onSelected(false);
                        }
                    }
                });
                mTitleFadeOutAnimator.start();
            }
        }
    });
    mAnimatorSet.start();
    if (DEBUG)
        dumpChildren("startedRowAnimation()");
}