gpt4 book ai didi

android - 从上到下 - 翻译动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:38 28 4
gpt4 key购买 nike

需要制作下一个动画(在android 2.2及以上):

1.从上到下移动按钮(点击他之后),

2.从底部回到顶部(再次点击他后)。

第一个动画效果很好,但第二个不行,btn 从底部“跳”到顶部而不是动画。

代码:

public class MainActivity extends Activity {

static RelativeLayout relativeLayout;
static Button btn;
static Boolean isUp = true;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.button1);
relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isUp){
isUp = false;
v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0));
}else{
isUp = true;
v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0));
}
}
});
}


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset)
{
TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition);
translateAnimation.setDuration(duration);
translateAnimation.setInterpolator(new AccelerateInterpolator());
translateAnimation.setStartOffset(startOffset);

//Stop animation after finishing.
//translateAnimation.setFillAfter(true);

translateAnimation.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation animation) { }
public void onAnimationRepeat(Animation animation) { }
public void onAnimationEnd(Animation animation) {
btn.setY(toYPosition);
}
});

return translateAnimation;
}
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />

</RelativeLayout>

最佳答案

好的,我解决了。

关于动画你应该知道的几个问题:

  1. 动画参数并不像你想象的那样简单“从(固定位置)”-->“到(固定位置)”。还有更多类似“从(当前位置/0)开始”-->“要走多少步,在哪个方向上(加号为正/负号为负)”

  2. 动画不会改变 View 在屏幕上的真实位置,因此如果你想在结束位置停止动画,你应该使用:

    animation.setFillAfter(true);
  3. 如果您确实想更改 View 的真实位置,您应该在“onAnimationEnd”上更新 View 参数(如下面的代码),或者计算位置并手动设置 Y/X 位置(再次在“onAnimationEnd”上), 比如:

    animatedView.setY(stopPosition);

代码:

    public class AnimationActivity extends Activity {

private boolean isUp;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {

public void onClick(final View v) {

final float direction = (isUp) ? -1 : 1;
final float yDelta = getScreenHeight() - (2 * v.getHeight());
final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM;

final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction);

animation.setDuration(500);

animation.setAnimationListener(new AnimationListener() {

public void onAnimationStart(Animation animation) {
}

public void onAnimationRepeat(Animation animation) {
}

public void onAnimationEnd(Animation animation) {

// fix flicking
// Source : http://stackoverflow.com/questions/9387711/android-animation-flicker
TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
anim.setDuration(1);
v.startAnimation(anim);


//set new params
LayoutParams params = new LayoutParams(v.getLayoutParams());
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(layoutTopOrBottomRule);
v.setLayoutParams(params);
}
});

v.startAnimation(animation);

//reverse direction
isUp = !isUp;
}
});
}

private float getScreenHeight() {

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return (float) displaymetrics.heightPixels;

}

关于android - 从上到下 - 翻译动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919264/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com