gpt4 book ai didi

android翻译动画剪辑未在屏幕上显示的 View 部分

转载 作者:行者123 更新时间:2023-11-30 03:29:06 28 4
gpt4 key购买 nike

我有一个相对布局,其中包含一些 float 在 GridView 上的 TextView 。当我在网格中选择一个项目时,布局向下移动到屏幕的尽头,只有大约 1/5 的部分是可见的。这是使用简单的翻译动画完成的。现在,当我单击网格中的另一个按钮时,我需要将相对布局移回其在 GridView 顶部的原始位置。我也用 Translate Animation 做到了这一点,这正在发生,但只有折叠时可见的 RelativeLayout 部分正在平移(移动),而另一部分在动画结束后变得可见。这看起来真的很难看。

我知道 Android 不会绘制屏幕上不可见的 View 部分。有没有办法强制绘制完整部分?

我的动画代码如下:

  public void smoothExpanderAnimate(final int finalTarget,final RelativeLayout expander,final int animateToY,final boolean setExpanded,final boolean refreshGrid)
{
final RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)expander.getLayoutParams();
TranslateAnimation anim = new TranslateAnimation(0f, 0f, 0f, animateToY);
anim.setDuration(400);

//timeListHeight = list.getHeight();
//anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//Should I try something here ?

}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
head_params.topMargin = finalTarget;
expander.clearAnimation();
expander.setLayoutParams(head_params);

isExpanded = setExpanded;
isAnimating = false;



if(!setExpanded)
{
setChildHeight(timeListHeight, list);
}
}
});



isAnimating = true;
expander.startAnimation(anim);
}

最佳答案

我找到了解决方案。

如果您不需要支持较低的 API 级别并且可以使用 API 级别 11 以上,您可以为此使用 ObjectAnimator。 ObjectAnimator 有一个 translatey 方法,效果很好。

如果您需要支持 API 8 及更高版本,您需要确保您的 View 在屏幕范围内,然后进行动画处理。以下代码修改成功了。

我知道这看起来很奇怪,如果有更好的方法,请告诉我。

 public void smoothExpanderAnimate(boolean addSpecialFix,final int finalTarget,final RelativeLayout expander,final int animateToY,final boolean setExpanded,final boolean refreshGrid)
{
final RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)expander.getLayoutParams();
TranslateAnimation anim = new TranslateAnimation(0f, 0f, 0f, animateToY);
anim.setDuration(400);
expander.setVisibility(View.INVISIBLE); //Make View invisible

if(isExpanded && addSpecialFix){

//THIS IS THE FIX

int old = head_params.topMargin;
head_params.topMargin = finalTarget;
expander.clearAnimation();
expander.setLayoutParams(head_params); //move invisible view to the final position
anim = new TranslateAnimation(0f, 0f, old-closedY, 0);
Log.d("asdasd", old+"");
anim.setDuration(400);


}
//timeListHeight = list.getHeight();
//anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
head_params.topMargin = finalTarget;
expander.clearAnimation();
expander.setLayoutParams(head_params);

isExpanded = setExpanded;
isAnimating = false;
expander.setVisibility(View.VISIBLE);
if(refreshGrid)
{
mCalAdapter.notifyDataSetChanged();
}

if(!setExpanded)
{
setListHeight(timeListHeight, list);
}
}
});



isAnimating = true;
expander.startAnimation(anim);
}

关于android翻译动画剪辑未在屏幕上显示的 View 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17785945/

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