动画setY(让它更流畅)

时间:2016-08-04 13:11:56

标签: android animation

所以我有一个看起来像这样的wifi信号强度等级:

Wifi Signal Strength scala

每当信号变得更好或更差时,我都会设置比例的y坐标:

<LoginButton
      style={[styles.loginButton, styles.loginButtonFB]}
      publishPermissions={["publish_actions"]}
      onLoginFinished={
        (error, result) => {
          if (error) {
            alert("login has error: " + result.error);
          } else if (result.isCancelled) {
            alert("login is cancelled.");
          } else {
            AccessToken.getCurrentAccessToken().then(
              (data) => {
                this.setState({
                  loading: true,
                })
                // HERE IS WHAT YOU NEED
                fetch(`https://graph.facebook.com/me?access_token=${data.accessToken.toString()}`)
                  .then((response) => response.json())
                  .then((res) => {
                    var socialOptions = {
                      email: res.email,
                      firstName: res.first_name,
                      lastName: res.last_name,
                      token: data.accessToken.toString(),
                      uid: data.userID,
                      loginType: 'facebook',
                    }
                    // ignore this--custom api call sending the data to my own api
                    api.socialRegister(socialOptions)
                      .then((response) => {
                        // working
                        this.handleResponse(response);

                      })
                      .catch((error) => console.log('error', error))
                  })
                  .catch((err) => console.log('error occurred', err.message));

              }) // end getCurrentAccessToken

            }
          }
        }

       onLogoutFinished={() => alert("logout")}/>

这看起来非常笨拙,我想为规模的oldY添加一个平滑的动画到newY。我很少了解动画,我尝试了以下内容:

View dBmLine = scale.findViewById(Math.abs(dBm));

float dBmLineMiddle = dBmLine.getY() + dBmLine.getHeight() / 2;
float newY = arrowMiddle - dBmLineMiddle;

scale.setY(newY);

但是规模没有移动,只是闪烁。

1 个答案:

答案 0 :(得分:1)

  

scale.animate()setDuration(50).translationY(newY);

50是毫秒,1000 = 1秒。

此外,如果您想在该动画上添加一个监听器,您可以使用:

scale.animate().setDuration(50).translationY(300).setListener(new Animator.AnimatorListener() {
     @Override
     public void onAnimationStart(Animator animator) {

     }

     @Override
     public void onAnimationEnd(Animator animator) {

     }

     @Override
     public void onAnimationCancel(Animator animator) {

     }

     @Override
     public void onAnimationRepeat(Animator animator) {

     }
});