Skip to content

Commit 4c509de

Browse files
committed
BlocksActivity: 添加fab出入动画
1 parent 3f59ea9 commit 4c509de

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2016 Alex Zhang aka. ztc1997
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.rayfantasy.icode.extension
18+
19+
import android.support.design.widget.FloatingActionButton
20+
import android.view.animation.DecelerateInterpolator
21+
22+
fun FloatingActionButton.toggle(visible: Boolean, duration: Long) {
23+
val translationY = (if (visible) 0 else height + getMarginBottom()).toFloat()
24+
if (duration != 0L)
25+
animate()
26+
.translationY(translationY)
27+
.setDuration(duration)
28+
.setInterpolator(DecelerateInterpolator())
29+
.start()
30+
else
31+
setTranslationY(translationY)
32+
}

app/src/main/kotlin/com/rayfantasy/icode/extension/_View.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ package com.rayfantasy.icode.extension
22

33
import android.support.design.widget.Snackbar
44
import android.view.View
5+
import android.view.ViewGroup
56

67
public fun View.snackBar(resId: Int, duration: Int = Snackbar.LENGTH_SHORT) = Snackbar.make(this, resId, duration).show()
78
public fun View.snackBar(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT) = Snackbar.make(this, text, duration).show()
9+
10+
fun View.getMarginBottom(): Int {
11+
var marginBottom = 0
12+
val layoutParams = layoutParams
13+
if (layoutParams is ViewGroup.MarginLayoutParams) {
14+
marginBottom = layoutParams.bottomMargin;
15+
}
16+
return marginBottom
17+
}

app/src/main/kotlin/com/rayfantasy/icode/ui/activity/BlocksActivity.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.balysv.materialmenu.MaterialMenuDrawable
1212
import com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredDefaultItemAnimator
1313
import com.rayfantasy.icode.R
1414
import com.rayfantasy.icode.databinding.ActivityBlocksBinding
15+
import com.rayfantasy.icode.extension.toggle
1516
import com.rayfantasy.icode.extra.PreloadLinearLayoutManager
1617
import com.rayfantasy.icode.model.ICodeTheme
1718
import com.rayfantasy.icode.postutil.PostUtil
@@ -72,7 +73,7 @@ class BlocksActivity : ActivityBindingStatus() {
7273
toast("rc = $rc")
7374
t.printStackTrace()
7475
})
75-
block_fab.onClick { startActivity<ReplyActivity>("id" to codeGood.id) }
76+
fab.onClick { startActivity<ReplyActivity>("id" to codeGood.id) }
7677

7778
toolbar.navigationIcon = menuDrawable
7879
if (intent.hasExtra("y") && intent.hasExtra("height")) {
@@ -81,6 +82,14 @@ class BlocksActivity : ActivityBindingStatus() {
8182
menuDrawable.animateIconState(MaterialMenuDrawable.IconState.ARROW)
8283
} else {
8384
menuDrawable.iconState = MaterialMenuDrawable.IconState.ARROW
85+
with(fab) {
86+
alpha = 0f
87+
post {
88+
toggle(false, 0)
89+
alpha = 1f
90+
toggle(true, TRANSFORM_DURATION_BG)
91+
}
92+
}
8493
}
8594
recyclerView.alpha = 0f
8695
recyclerView.post {
@@ -109,6 +118,8 @@ class BlocksActivity : ActivityBindingStatus() {
109118
if (intent.hasExtra("y") && intent.hasExtra("height")) {
110119
if (intent.getBooleanExtra("arrowAnim", true)) {
111120
menuDrawable.animateIconState(MaterialMenuDrawable.IconState.BURGER)
121+
} else {
122+
fab.toggle(false, TRANSFORM_DURATION_BG)
112123
}
113124
recyclerView.adapter = null
114125
recyclerView.animate()

app/src/main/res/layout/activity_blocks.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<include layout="@layout/content_blocks"/>
3434

3535
<android.support.design.widget.FloatingActionButton
36-
android:id="@+id/block_fab"
36+
android:id="@+id/fab"
3737
android:layout_width="wrap_content"
3838
android:layout_height="wrap_content"
3939
android:layout_gravity="bottom|end"

0 commit comments

Comments
 (0)