Search in sources :

Example 16 with StateListAnimator

use of android.animation.StateListAnimator in project GreenHouse by utsanjan.

the class FloatingActionButtonImplLollipop method onElevationsChanged.

// com.google.android.material.floatingactionbutton.FloatingActionButtonImpl
@Override
void onElevationsChanged(float elevation, float hoveredFocusedTranslationZ, float pressedTranslationZ) {
    if (Build.VERSION.SDK_INT == 21) {
        this.view.refreshDrawableState();
    } else {
        StateListAnimator stateListAnimator = new StateListAnimator();
        stateListAnimator.addState(PRESSED_ENABLED_STATE_SET, createElevationAnimator(elevation, pressedTranslationZ));
        stateListAnimator.addState(HOVERED_FOCUSED_ENABLED_STATE_SET, createElevationAnimator(elevation, hoveredFocusedTranslationZ));
        stateListAnimator.addState(FOCUSED_ENABLED_STATE_SET, createElevationAnimator(elevation, hoveredFocusedTranslationZ));
        stateListAnimator.addState(HOVERED_ENABLED_STATE_SET, createElevationAnimator(elevation, hoveredFocusedTranslationZ));
        AnimatorSet set = new AnimatorSet();
        List<Animator> animators = new ArrayList<>();
        animators.add(ObjectAnimator.ofFloat(this.view, "elevation", elevation).setDuration(0L));
        if (Build.VERSION.SDK_INT >= 22 && Build.VERSION.SDK_INT <= 24) {
            animators.add(ObjectAnimator.ofFloat(this.view, View.TRANSLATION_Z, this.view.getTranslationZ()).setDuration(100L));
        }
        animators.add(ObjectAnimator.ofFloat(this.view, View.TRANSLATION_Z, 0.0f).setDuration(100L));
        set.playSequentially((Animator[]) animators.toArray(new Animator[0]));
        set.setInterpolator(ELEVATION_ANIM_INTERPOLATOR);
        stateListAnimator.addState(ENABLED_STATE_SET, set);
        stateListAnimator.addState(EMPTY_STATE_SET, createElevationAnimator(0.0f, 0.0f));
        this.view.setStateListAnimator(stateListAnimator);
    }
    if (shouldAddPadding()) {
        updatePadding();
    }
}
Also used : ObjectAnimator(android.animation.ObjectAnimator) Animator(android.animation.Animator) StateListAnimator(android.animation.StateListAnimator) StateListAnimator(android.animation.StateListAnimator) ArrayList(java.util.ArrayList) AnimatorSet(android.animation.AnimatorSet)

Example 17 with StateListAnimator

use of android.animation.StateListAnimator in project material-components-android by material-components.

the class FloatingActionButtonLollipop method onElevationsChanged.

@Override
void onElevationsChanged(final float elevation, final float pressedTranslationZ) {
    final int sdk = Build.VERSION.SDK_INT;
    if (sdk == 21) {
        // logic in the animations below).
        if (mView.isEnabled()) {
            mView.setElevation(elevation);
            if (mView.isFocused() || mView.isPressed()) {
                mView.setTranslationZ(pressedTranslationZ);
            } else {
                mView.setTranslationZ(0);
            }
        } else {
            mView.setElevation(0);
            mView.setTranslationZ(0);
        }
    } else {
        final StateListAnimator stateListAnimator = new StateListAnimator();
        // Animate elevation and translationZ to our values when pressed
        AnimatorSet set = new AnimatorSet();
        set.play(ObjectAnimator.ofFloat(mView, "elevation", elevation).setDuration(0)).with(ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, pressedTranslationZ).setDuration(PRESSED_ANIM_DURATION));
        set.setInterpolator(ANIM_INTERPOLATOR);
        stateListAnimator.addState(PRESSED_ENABLED_STATE_SET, set);
        // Same deal for when we're focused
        set = new AnimatorSet();
        set.play(ObjectAnimator.ofFloat(mView, "elevation", elevation).setDuration(0)).with(ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, pressedTranslationZ).setDuration(PRESSED_ANIM_DURATION));
        set.setInterpolator(ANIM_INTERPOLATOR);
        stateListAnimator.addState(FOCUSED_ENABLED_STATE_SET, set);
        // Animate translationZ to 0 if not pressed
        set = new AnimatorSet();
        set.playSequentially(ObjectAnimator.ofFloat(mView, "elevation", elevation).setDuration(0), // bug). The issue has been fixed in version 25.
        ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, mView.getTranslationZ()).setDuration(PRESSED_ANIM_DELAY), ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, 0f).setDuration(PRESSED_ANIM_DURATION));
        set.setInterpolator(ANIM_INTERPOLATOR);
        stateListAnimator.addState(ENABLED_STATE_SET, set);
        // Animate everything to 0 when disabled
        set = new AnimatorSet();
        set.play(ObjectAnimator.ofFloat(mView, "elevation", 0f).setDuration(0)).with(ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, 0f).setDuration(0));
        set.setInterpolator(ANIM_INTERPOLATOR);
        stateListAnimator.addState(EMPTY_STATE_SET, set);
        mView.setStateListAnimator(stateListAnimator);
    }
    if (mShadowViewDelegate.isCompatPaddingEnabled()) {
        updatePadding();
    }
}
Also used : StateListAnimator(android.animation.StateListAnimator) AnimatorSet(android.animation.AnimatorSet)

Example 18 with StateListAnimator

use of android.animation.StateListAnimator in project material-components-android by material-components.

the class ViewUtilsLollipop method setStateListAnimatorFromAttrs.

static void setStateListAnimatorFromAttrs(View view, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    final Context context = view.getContext();
    final TypedArray a = context.obtainStyledAttributes(attrs, STATE_LIST_ANIM_ATTRS, defStyleAttr, defStyleRes);
    try {
        if (a.hasValue(0)) {
            StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, a.getResourceId(0, 0));
            view.setStateListAnimator(sla);
        }
    } finally {
        a.recycle();
    }
}
Also used : Context(android.content.Context) TypedArray(android.content.res.TypedArray) StateListAnimator(android.animation.StateListAnimator)

Example 19 with StateListAnimator

use of android.animation.StateListAnimator in project iosched by google.

the class ViewUtilsLollipop method setDefaultAppBarLayoutStateListAnimator.

/**
 * Creates and sets a {@link StateListAnimator} with a custom elevation value
 */
static void setDefaultAppBarLayoutStateListAnimator(final View view, final float elevation) {
    final int dur = view.getResources().getInteger(R.integer.app_bar_elevation_anim_duration);
    final StateListAnimator sla = new StateListAnimator();
    // Enabled and collapsible, but not collapsed means not elevated
    sla.addState(new int[] { android.R.attr.enabled, R.attr.state_collapsible, -R.attr.state_collapsed }, ObjectAnimator.ofFloat(view, "elevation", 0f).setDuration(dur));
    // Default enabled state
    sla.addState(new int[] { android.R.attr.enabled }, ObjectAnimator.ofFloat(view, "elevation", elevation).setDuration(dur));
    // Disabled state
    sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0).setDuration(0));
    view.setStateListAnimator(sla);
}
Also used : StateListAnimator(android.animation.StateListAnimator)

Example 20 with StateListAnimator

use of android.animation.StateListAnimator in project guanggoo-android by mzlogin.

the class MainActivity method initViews.

private void initViews() {
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    // 设置抽屉菜单图标为图片原色
    navigationView.setItemIconTintList(null);
    mAvatarImageView = navigationView.getHeaderView(0).findViewById(R.id.avatar);
    mUsernameTextView = navigationView.getHeaderView(0).findViewById(R.id.username);
    mSearchImageView = navigationView.getHeaderView(0).findViewById(R.id.search);
    View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            switch(view.getId()) {
                case R.id.avatar:
                case R.id.username:
                    openUserProfile();
                    break;
                case R.id.search:
                    openPage(ConstantUtil.SEARCH_URL, null);
                    break;
                default:
                    break;
            }
        }
    };
    mAvatarImageView.setOnClickListener(listener);
    mUsernameTextView.setOnClickListener(listener);
    mSearchImageView.setOnClickListener(listener);
    openPage(ConstantUtil.HOME_URL, getString(R.string.app_name));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            boolean canGoBack = (getSupportFragmentManager().getBackStackEntryCount() > 1);
            if (canGoBack) {
                onBackPressed();
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        }
    });
    getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {

        @Override
        public void onBackStackChanged() {
            Fragment fragment = getCurrentFragment();
            if (fragment instanceof BaseFragment) {
                BaseFragment baseFragment = (BaseFragment) fragment;
                setTitle(baseFragment.getTitle());
                boolean canGoBack = (getSupportFragmentManager().getBackStackEntryCount() > 1);
                if (canGoBack) {
                    toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
                    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                } else {
                    toolbar.setNavigationIcon(R.drawable.ic_menu);
                    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                }
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    StateListAnimator stateListAnimator = new StateListAnimator();
                    float elevation = 0;
                    if (!baseFragment.getClass().isAnnotationPresent(StartsWithAppBar.class)) {
                        elevation = DimensUtil.getDensity(getWindowManager()) * 5;
                    }
                    stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(mMainAppBar, "elevation", elevation));
                    mMainAppBar.setStateListAnimator(stateListAnimator);
                }
            }
        }
    });
}
Also used : NavigationView(com.google.android.material.navigation.NavigationView) ActionBarDrawerToggle(androidx.appcompat.app.ActionBarDrawerToggle) StateListAnimator(android.animation.StateListAnimator) NavigationView(com.google.android.material.navigation.NavigationView) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) BaseFragment(org.mazhuang.guanggoo.base.BaseFragment) Fragment(androidx.fragment.app.Fragment) BaseFragment(org.mazhuang.guanggoo.base.BaseFragment) FragmentManager(androidx.fragment.app.FragmentManager) DrawerLayout(androidx.drawerlayout.widget.DrawerLayout) Toolbar(androidx.appcompat.widget.Toolbar)

Aggregations

StateListAnimator (android.animation.StateListAnimator)25 SuppressLint (android.annotation.SuppressLint)11 Drawable (android.graphics.drawable.Drawable)10 View (android.view.View)10 ImageView (android.widget.ImageView)10 Outline (android.graphics.Outline)9 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)9 ViewOutlineProvider (android.view.ViewOutlineProvider)9 ActionBar (org.telegram.ui.ActionBar.ActionBar)9 CombinedDrawable (org.telegram.ui.Components.CombinedDrawable)9 AnimatorSet (android.animation.AnimatorSet)8 Context (android.content.Context)8 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)8 RecyclerView (androidx.recyclerview.widget.RecyclerView)8 RecyclerListView (org.telegram.ui.Components.RecyclerListView)8 Paint (android.graphics.Paint)7 ViewGroup (android.view.ViewGroup)7 FrameLayout (android.widget.FrameLayout)7 Animator (android.animation.Animator)6 ObjectAnimator (android.animation.ObjectAnimator)6