如果一个视图始终与父级底部对齐,最初的高度为0,我想为其设置动画,使其向上滑动到WRAP_CONTENT
的高度。我正在使用布局转换来实现这一目标:
viewGroup.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
其中viewGroup
是viewToAnimate
的父级并且animateLayoutChange=true
逻辑是:
val params = viewToAnimate.layoutParams
if (expand && params.height == 0) {
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
viewToAnimate.layoutParams = params
} else if (collapse && params.height != 0) {
params.height = 0
viewToAnimate.layoutParams = params
}
当视图扩展时,这很有用;视图很好地从底部向上滑动。但是,当高度设置为0时,视图会消失并且不会滑入。viewToAnimate
是具有一些TextViews
的相对布局,没有任何复杂。任何建议将不胜感激。
显示效果的视频(注意文字没有滑落,只是消失):https://www.dropbox.com/s/1pq7yh0bqx8ghif/2017_10_06_23_17_11.mp4?dl=0
查看动画:
<RelativeLayout>
..some other stuff...
<RelativeLayout
android:id="@+id/viewToAnimate"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="@color/white">
<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:textSize="50sp"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:1)
将 viewToAnimate 布局高度保持为&#34; wrap_content &#34;和textView高度为&#34; 0dp&#34; ..见下面的例子。
<RelativeLayout
android:animateLayoutChanges="true"
android:id="@+id/viewToAnimate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="@android:color/white">
<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Sample Text"
android:textSize="50sp"/>
</RelativeLayout>
final RelativeLayout viewToAnimate = (RelativeLayout) findViewById(R.id.viewToAnimate);
viewToAnimate.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
TextView sampleTV = (TextView)findViewById(R.id.sampleText);
ViewGroup.LayoutParams params = sampleTV.getLayoutParams();
if (params.height == 0) {
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}else {
params.height = 0;
}
sampleTV.setLayoutParams(params);
试着让我知道。
答案 1 :(得分:1)
尝试使用像
这样的对象动画师 TextView sampleTV = (TextView) findViewById(R.id.sampleText);
//initially translate the view to bottom
sampleTV.setTranslationY(sampleTV.getHeight());
// for sliding up
sampleTV.animate().translationY(0).setDuration(100).setInterpolator(new AccelerateDecelerateInterpolator()).start();
// for sliding down
sampleTV.animate().translationY(sampleTV.getHeight()).setDuration(100).setInterpolator(new AccelerateDecelerateInterpolator()).start();
答案 2 :(得分:1)
只需将其添加到您的XML中即可完成工作。
android:animateLayoutChanges="true"
像那样
<RelativeLayout>
..some other stuff...
<RelativeLayout
android:id="@+id/viewToAnimate"
android:layout_width="match_parent"
android:layout_height="0dp"
*******android:animateLayoutChanges="true"********
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="@color/white">
<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:textSize="50sp"/>
</RelativeLayout>
</RelativeLayout>
答案 3 :(得分:1)
我在视图内外设置动画的方法是,如果视图要在屏幕外,我将Visibility属性设置为&#34; Invisible&#34;然后当它动画进入视图时,我将它设置为&#34; Visible&#34;当动画开始时以及要在屏幕上制作动画时,我将其设置为&#34; Invisible&#34;当动画结束时。
所以不要改变身高设定的可见度。
if (expand) {
viewToAnimate.setVisibility(View.VISIBLE);
} else if (collapse) {
viewToAnimate.setVisibility(View.INVISIBLE);
}
在xml使用中设置可见性:
android:visibility="invisible"
希望这有帮助。
澄清:
我使用自定义动画和AnimationUtils.loadAnimation来获取动画,而不是使用setAnimationListener来设置布局/视图中可见动画的可见性。
示例:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.custom_animation_in_or_out_of_view);
animation.setAnimationListener(new Animation.AnimationListener(){
@Override public void onAnimationStart(Animation animation) {
//should always be visible on animation starting
viewToAnimate.setVisibility(View.INVISIBLE);
}
@Override public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
//set your viewToAnimate to the corresponding visibility...
}
});
viewToAnimate.startAnimation(animation);
用于滑入视图的自定义动画xml:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="1000" />
并滑出视野,只需切换来自YDelta&#39;的值。和#Delta&#39;。