![](/img/trans.png)
[英]How to animate a view in Android and have it stay in the new position/size?
[英]Android -> how to animate to the new position
這里是簡單的 xml android animation:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-110"
android:toYDelta="-110" android:duration="1000" android:fillAfter="true" />
我想將動畫 object 從屏幕中心移動到 0、0 位置。 我是怎么做的貓(它應該適用於所有屏幕分辨率)
謝謝你們的幫助。 但是我已經通過其他方式動態地解決了我的問題,沒有 xml。 這是此方法的完整代碼:
public void replace(int xTo, int yTo, float xScale, float yScale) {
// create set of animations
replaceAnimation = new AnimationSet(false);
// animations should be applied on the finish line
replaceAnimation.setFillAfter(true);
// create scale animation
ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
scale.setDuration(1000);
// create translation animation
TranslateAnimation trans = new TranslateAnimation(0, 0,
TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
TranslateAnimation.ABSOLUTE, yTo - getTop());
trans.setDuration(1000);
// add new animations to the set
replaceAnimation.addAnimation(scale);
replaceAnimation.addAnimation(trans);
// start our animation
startAnimation(replaceAnimation);
}
我的解決方案
謝謝你們的幫助。 但我已經通過其他方式動態修復了我的問題,沒有xml。 這是這個方法的完整代碼:
public void replace(int xTo, int yTo, float xScale, float yScale) {
// create set of animations
replaceAnimation = new AnimationSet(false);
// animations should be applied on the finish line
replaceAnimation.setFillAfter(true);
// create scale animation
ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
scale.setDuration(1000);
// create translation animation
TranslateAnimation trans = new TranslateAnimation(0, 0,
TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
TranslateAnimation.ABSOLUTE, yTo - getTop());
trans.setDuration(1000);
// add new animations to the set
replaceAnimation.addAnimation(scale);
replaceAnimation.addAnimation(trans);
// start our animation
startAnimation(replaceAnimation);
}
首先,在占據整個屏幕的容器中插入對象。 然后像這樣修改動畫xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="50%p" android:fromYDelta="50%p"
android:toXDelta="0%p" android:toYDelta="0%p"
android:duration="1000"
android:fillAfter="true" />
您還可以查看此Android API參考http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element
%p后綴轉換元素“相對於父級大小的百分比”。
如果您希望它在所有屏幕上工作,您將無法對x,y值進行硬編碼。 動畫允許您使用百分比。 這個動畫會將你的視圖從0%x和y移動(相對於它自己。換句話說,它在動畫之前它將從那里開始)它將移動到0%px和y(這意味着0%相對於父母)這應該帶你到左上角。 根據您想要的角落多遠,您可以嘗試5%p或10%p來獲得額外的余量。
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="0%p"
android:toYDelta="0%p" android:duration="1000" android:fillAfter="true" />
In my case I used an ImageView, randomly moved it using setY() and setX() and then animated it on the new position with a Scale animation in xml.
那沒有用。 它總是從最初的 position 開始,然后擴展到新的 position。
我解決它的方法是將動畫 View 包裝在 RelativeLayout 中,然后移動帶有 ImageView 的 RelativeLayout。
PivotX 和 PivotY 始終使用父視圖的中心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.