得到Y位置百分比编程 [英] get Y position percentage programmatically
问题描述
我在做一些以便翻译动画。我想两种方式:通过XML和编程
I'm doing some translate animations with a view. I'm trying both ways: By xml and programmatically.
这是我如何定义被XML翻译:
This is how I have defined the translation by xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-70%" android:duration="1000"/>
</set>
此方式,它工作得很好,但我意识到,我需要更好的编程方式使用animationListener我可以定义操作来occurr当动画完成。
This way it works fine, but I've realized that I better need the programmatically way as using the animationListener I can define actions to occurr when the animation finishes.
这是我如何做到这一点编程方式:
This is how I do it programmatically:
slide_up = new TranslateAnimation(valuesContainer.getX(),
valuesContainer.getX(),
valuesContainer.getY(),
valuesContainer.getY() - 70);
slide_up.setDuration(1000);
slide_up.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//SOMETHING HAPPENS
}
});
定义 fromYDelta code>和
toYDelta code>值(图像只是移动Y轴)时,问题就来了。在XML中,我使用百分比(%)它和它的作品,我需要的方式,但我不知道如何设置的值同样的方式,但编程。
The problem comes when defining the fromYDelta
and toYDelta
values (the image just moves in the Y axis). In the xml, I do it using percentages (%) and it works the way I need, but I don't know how to set the values this same way but programmatically.
推荐答案
我来结合两者的方法,这样的解决方案。
I have come to a solution combining both of this methods.
我定义在这样的XML动画,堪称F.E. slide_up_anim.xml
I define the animation in a xml like this, called f.e. slide_up_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-70%" android:duration="1000"/>
</set>
然后,我设置AnimationListener这个动画是这样的:
And then, I set the AnimationListener for this animation this way:
slide_up = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_anim);
slide_up.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
这样我可以定义fromYDelta和toYDelta百分比,也听动画事件。
This way I can define the fromYDelta and toYDelta in percentage, and also listen for animation events.
这篇关于得到Y位置百分比编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!