Android的 - 动画问题 [英] Android - Animation Issue
本文介绍了Android的 - 动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个视图从屏幕底部向上滑动到顶部,它做的动画,但在最后它返回到屏幕上的底部。我想不通为什么。继承人的code:
公共无效效果基本show(){ 动画幻灯片= NULL;
幻灯片=新TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0,
Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
0.0,Animation.RELATIVE_TO_SELF,-5.0f); slide.setDuration(4000);
。SubviewManager.getSelectedRouteView()startAnimation(幻灯片); slide.setAnimationListener(新Animation.AnimationListener(){ @覆盖
公共无效onAnimationStart(动画动画){ } @覆盖
公共无效onAnimationRepeat(动画动画){
} @覆盖
公共无效onAnimationEnd(动画动画){ 。SubviewManager.getSelectedRouteView()clearAnimation(); RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
。SubviewManager.getSelectedRouteView()的getWidth(),SubviewManager.getSelectedRouteView()的getHeight())。
Log.e(规则,规则+ lp.getRules()的toString());
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
SubviewManager.getSelectedRouteView()setLayoutParams(LP)。 } });
}< XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < RelativeLayout的
机器人:ID =@ + ID / selected_route_layout
机器人:填充=10dip
机器人:背景=#FFF
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_alignParentBottom =真正的> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / time_label
机器人:文字颜色=#1D1C18
机器人:layout_marginRight =10dp
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / eta_label
机器人:文字颜色=#7EC82F
机器人:layout_alignBottom =@ + ID / time_label
机器人:layout_toRightOf =@ + ID / time_label
机器人:layout_toEndOf =@ + ID / time_label/> <的LinearLayout
机器人:layout_marginTop =10dip
机器人:layout_marginBottom =10dip
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / time_label
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真
机器人:ID =@ + ID / route_breakdown
机器人:layout_centerVertical =真
机器人:重力=center_vertical/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / via_label
机器人:文字颜色=#BDBDBD
机器人:layout_below =@ + ID / route_breakdown
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/> <的ImageButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@空
机器人:ID =@ + ID / arrow_up
机器人:填充=15dip
机器人:SRC =@绘制/ transit_symbol_up_arrow
机器人:layout_centerVertical =真
机器人:layout_alignParentRight =真
机器人:layout_alignParentEnd =真/>
< / RelativeLayout的>< / RelativeLayout的>
继承人的XML:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < RelativeLayout的
机器人:ID =@ + ID / selected_route_layout
机器人:填充=10dip
机器人:背景=#FFF
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_alignParentBottom =真正的> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / time_label
机器人:文字颜色=#1D1C18
机器人:layout_marginRight =10dp
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / eta_label
机器人:文字颜色=#7EC82F
机器人:layout_alignBottom =@ + ID / time_label
机器人:layout_toRightOf =@ + ID / time_label
机器人:layout_toEndOf =@ + ID / time_label/> <的LinearLayout
机器人:layout_marginTop =10dip
机器人:layout_marginBottom =10dip
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / time_label
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真
机器人:ID =@ + ID / route_breakdown
机器人:layout_centerVertical =真
机器人:重力=center_vertical/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字=中文字
机器人:ID =@ + ID / via_label
机器人:文字颜色=#BDBDBD
机器人:layout_below =@ + ID / route_breakdown
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/> <的ImageButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@空
机器人:ID =@ + ID / arrow_up
机器人:填充=15dip
机器人:SRC =@绘制/ transit_symbol_up_arrow
机器人:layout_centerVertical =真
机器人:layout_alignParentRight =真
机器人:layout_alignParentEnd =真/>
< / RelativeLayout的>< / RelativeLayout的>
解决方案
也许您正在使用旧的动画工具和你刚才介绍了其缺点之一。考虑使用动画和更改您的视图的TranslationX和TranslationY属性。
一种可能的例子:
yourView.animate()。translationY(newy指定).setDuration(持续时间).withEndAction(新的Runnable(){
@覆盖
公共无效的run(){
//你的onAnimationEnd行动
}
})。开始();
I'm trying to slide a view up from the bottom of the screen to the top and its doing the animation but at the end it goes back to the bottom on the screen. I can't figure out why. Heres the code:
public void slideUp() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -5.0f);
slide.setDuration(4000);
SubviewManager.getSelectedRouteView().startAnimation(slide);
slide.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
SubviewManager.getSelectedRouteView().clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
SubviewManager.getSelectedRouteView().getWidth(), SubviewManager.getSelectedRouteView().getHeight());
Log.e("Rules", "Rules " + lp.getRules().toString());
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
SubviewManager.getSelectedRouteView().setLayoutParams(lp);
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/selected_route_layout"
android:padding="10dip"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/time_label"
android:textColor="#1D1C18"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/eta_label"
android:textColor="#7EC82F"
android:layout_alignBottom="@+id/time_label"
android:layout_toRightOf="@+id/time_label"
android:layout_toEndOf="@+id/time_label" />
<LinearLayout
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/time_label"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/route_breakdown"
android:layout_centerVertical="true"
android:gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/via_label"
android:textColor="#BDBDBD"
android:layout_below="@+id/route_breakdown"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/arrow_up"
android:padding="15dip"
android:src="@drawable/transit_symbol_up_arrow"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
Heres the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/selected_route_layout"
android:padding="10dip"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/time_label"
android:textColor="#1D1C18"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/eta_label"
android:textColor="#7EC82F"
android:layout_alignBottom="@+id/time_label"
android:layout_toRightOf="@+id/time_label"
android:layout_toEndOf="@+id/time_label" />
<LinearLayout
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/time_label"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/route_breakdown"
android:layout_centerVertical="true"
android:gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/via_label"
android:textColor="#BDBDBD"
android:layout_below="@+id/route_breakdown"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/arrow_up"
android:padding="15dip"
android:src="@drawable/transit_symbol_up_arrow"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
解决方案
Probably you're using old tools for animation and you've just described one of its drawbacks. Consider using Animator and changing your view's TranslationX and TranslationY properties.
One of possible examples:
yourView.animate().translationY(newY).setDuration(duration).withEndAction(new Runnable() {
@Override
public void run() {
// your onAnimationEnd actions
}
}).start();
这篇关于Android的 - 动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文