android 按键上浮动画_android – 浮动动作按钮动画

从@Zielony的回答中,我确切地说到了我想要的地方.

下面是正确应用效果的代码.

scale_fab_in.xml

android:duration="500"

android:fromXScale="0"

android:fromYScale="0"

android:pivotX="50%"

android:pivotY="50%"

android:toXScale="1"

android:toYScale="1"

android:interpolator="@android:interpolator/overshoot"/>

scale_fab_out.xml

android:duration="400"

android:fromXScale="1"

android:fromYScale="1"

android:pivotX="50%"

android:pivotY="50%"

android:toXScale="0"

android:toYScale="0"

android:interpolator="@android:interpolator/overshoot"/>

编辑2016年2月16日 – 另一种方式:

将下面的代码放在您的FAB代码或任何其他视图中.

//global

private static final int FAB_ANIM_DURATION = 200;

public void hide() {

// Only use scale animation if FAB is visible

if (getVisibility() == View.VISIBLE) {

// Pivots indicate where the animation begins from

float pivotX = getPivotX() + getTranslationX();

float pivotY = getPivotY() + getTranslationY();

// Animate FAB shrinking

ScaleAnimation anim = new ScaleAnimation(1, 0, 1, 0, pivotX, pivotY);

anim.setDuration(FAB_ANIM_DURATION);

anim.setInterpolator(getInterpolator());

startAnimation(anim);

}

setVisibility(View.INVISIBLE);

}

public void show() {

show(0, 0);

}

public void show(float translationX, float translationY) {

// Set FAB's translation

setTranslation(translationX, translationY);

// Only use scale animation if FAB is hidden

if (getVisibility() != View.VISIBLE) {

// Pivots indicate where the animation begins from

float pivotX = getPivotX() + translationX;

float pivotY = getPivotY() + translationY;

ScaleAnimation anim;

// If pivots are 0, that means the FAB hasn't been drawn yet so just use the

// center of the FAB

if (pivotX == 0 || pivotY == 0) {

anim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

} else {

anim = new ScaleAnimation(0, 1, 0, 1, pivotX, pivotY);

}

// Animate FAB expanding

anim.setDuration(FAB_ANIM_DURATION);

anim.setInterpolator(getInterpolator());

startAnimation(anim);

}

setVisibility(View.VISIBLE);

}

private void setTranslation(float translationX, float translationY) {

if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {

animate().setInterpolator(getInterpolator()).setDuration(FAB_ANIM_DURATION)

.translationX(translationX).translationY(translationY);

}

}

private Interpolator getInterpolator() {

return AnimationUtils.loadInterpolator(getContext(), R.interpolator.fab_interpolator);

}

@android:interpolator/decelerate_cubic

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值