From 580ce14809b7c57bcd27525063993313c7d550f6 Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 15 Jan 2018 20:20:31 -0800 Subject: [PATCH] Refine animations --- .../com/github/shadowsocks/utils/Utils.kt | 6 --- .../widget/BottomMarginBehavior.kt | 40 ------------------- ...ardBehavior.kt => ShrinkUpwardBehavior.kt} | 40 ++++++------------- mobile/src/main/res/layout/layout_main.xml | 2 +- 4 files changed, 14 insertions(+), 74 deletions(-) delete mode 100644 mobile/src/main/java/com/github/shadowsocks/widget/BottomMarginBehavior.kt rename mobile/src/main/java/com/github/shadowsocks/widget/{MoveUpwardBehavior.kt => ShrinkUpwardBehavior.kt} (69%) diff --git a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt index 95b3b9b7..e328fcff 100644 --- a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt +++ b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt @@ -9,7 +9,6 @@ import android.support.annotation.AttrRes import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager import android.support.v7.util.SortedList -import android.view.accessibility.AccessibilityManager import android.util.TypedValue import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.JniHelper @@ -22,11 +21,6 @@ private val fieldChildFragmentManager by lazy { field } -fun isAccessibilityEnabled(context: Context): Boolean { - val am = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager - return am.isEnabled() -} - fun String.isNumericAddress() = JniHelper.parseNumericAddress(this) != null fun String.parseNumericAddress(): InetAddress? { val addr = JniHelper.parseNumericAddress(this) diff --git a/mobile/src/main/java/com/github/shadowsocks/widget/BottomMarginBehavior.kt b/mobile/src/main/java/com/github/shadowsocks/widget/BottomMarginBehavior.kt deleted file mode 100644 index e420f4df..00000000 --- a/mobile/src/main/java/com/github/shadowsocks/widget/BottomMarginBehavior.kt +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************************************* - * * - * Copyright (C) 2017 by Max Lv * - * Copyright (C) 2017 by Mygod Studio * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - * * - *******************************************************************************/ - -package com.github.shadowsocks.widget - -import android.content.Context -import android.support.design.widget.CoordinatorLayout -import android.util.AttributeSet -import android.view.View -import com.github.shadowsocks.R - -class BottomMarginBehavior : CoordinatorLayout.Behavior { - constructor() : super() - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) - - override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean = - dependency.id == R.id.stat // sorry I'm too lazy to write an attribute - - override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean { - (child.layoutParams as CoordinatorLayout.LayoutParams).bottomMargin = (parent.height - dependency.y).toInt() - return true - } -} diff --git a/mobile/src/main/java/com/github/shadowsocks/widget/MoveUpwardBehavior.kt b/mobile/src/main/java/com/github/shadowsocks/widget/ShrinkUpwardBehavior.kt similarity index 69% rename from mobile/src/main/java/com/github/shadowsocks/widget/MoveUpwardBehavior.kt rename to mobile/src/main/java/com/github/shadowsocks/widget/ShrinkUpwardBehavior.kt index 0bcc6f8b..9f550f94 100644 --- a/mobile/src/main/java/com/github/shadowsocks/widget/MoveUpwardBehavior.kt +++ b/mobile/src/main/java/com/github/shadowsocks/widget/ShrinkUpwardBehavior.kt @@ -20,34 +20,27 @@ package com.github.shadowsocks.widget -import android.animation.Animator -import android.animation.AnimatorListenerAdapter import android.animation.ValueAnimator import android.content.Context -import android.os.Build import android.support.design.widget.CoordinatorLayout -import android.support.design.widget.SnackbarAnimation import android.support.design.widget.Snackbar -import android.support.v4.view.ViewCompat +import android.support.design.widget.SnackbarAnimation import android.util.AttributeSet import android.view.View - -import com.github.shadowsocks.utils.isAccessibilityEnabled +import android.view.accessibility.AccessibilityManager /** * Full credits go to: https://stackoverflow.com/a/35904421/2245107 */ -class MoveUpwardBehavior : CoordinatorLayout.Behavior { - constructor() : super() - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) +class ShrinkUpwardBehavior(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior(context, attrs) { + private val accessibility = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean = - dependency is Snackbar.SnackbarLayout + dependency is Snackbar.SnackbarLayout override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean { - val params = child.layoutParams - params.height = parent.height - dependency.height - child.layoutParams = params + child.layoutParams.height = dependency.y.toInt() + child.requestLayout() return true } @@ -55,25 +48,18 @@ class MoveUpwardBehavior : CoordinatorLayout.Behavior { * Based on BaseTransientBottomBar.animateViewOut (support lib 27.0.2). */ override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) { - if (!isAccessibilityEnabled(parent.getContext())) { + if (accessibility.isEnabled) child.layoutParams.height = parent.height else { val animator = ValueAnimator() val start = child.height animator.setIntValues(start, parent.height) animator.interpolator = SnackbarAnimation.FAST_OUT_SLOW_IN_INTERPOLATOR animator.duration = SnackbarAnimation.ANIMATION_DURATION - animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener { - override fun onAnimationUpdate(animator: ValueAnimator) { - val currentValue = animator.animatedValue as Int - val params = child.layoutParams - params.height = currentValue - child.layoutParams = params - } - }) + @Suppress("NAME_SHADOWING") + animator.addUpdateListener { animator -> + child.layoutParams.height = animator.animatedValue as Int + child.requestLayout() + } animator.start() - } else { - val params = child.layoutParams - params.height = parent.height - child.layoutParams = params } } } diff --git a/mobile/src/main/res/layout/layout_main.xml b/mobile/src/main/res/layout/layout_main.xml index 64f3be44..480022ba 100644 --- a/mobile/src/main/res/layout/layout_main.xml +++ b/mobile/src/main/res/layout/layout_main.xml @@ -12,7 +12,7 @@ + app:layout_behavior="com.github.shadowsocks.widget.ShrinkUpwardBehavior">