出于某种原因,我的TranslateAnimation
没有将2 ImageView
移动到正确的位置。老实说,我不知道为什么ImageView
会离开屏幕!我定义minY, maxY, minX, maxX
允许我在这些常量之间生成坐标
我已经检查过我生成的X and Y
坐标位于屏幕maxY
和maxX
内。但是imageview不断被移出屏幕!
我的屏幕maxY = 2500
和maxX = 1599
。我正在使用三星Galaxy标签S
private static int[] generateRandomX(){
int[] numbers = new int[2];
int R;
int Low = 0;
int High = 800;
for(int i=0;i<numbers.length;i++) {
Random r = new Random();
R = r.nextInt(High - Low) + Low;
numbers[i] = R;
// Log.d("X pos -->", "X = "+numbers[i]);
}
return numbers;
}
private static int[] generateRandomY(){
int[] numbers = new int[2];
int R;
int Low = 1000;
int High = 2000;
for(int i=0;i<numbers.length;i++) {
Random r = new Random();
R = r.nextInt(High - Low) + Low;
numbers[i] = R;
// Log.d("Y pos -->", "Y = "+numbers[i]);
}
return numbers;
}
TranslateAnimation
这段代码负责从ImageView
0X, toX, 0Y, toY
public static void swipedDiceInThisDirection(float n1, float n2, float n3, float n4){
//Die1
moveDie1 = new TranslateAnimation(0, n1, 0, n2); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
moveDie1.setDuration(2000); // animation duration
//Die2
moveDie2 = new TranslateAnimation(0, n3, 0, n4);
moveDie2.setDuration(2000);
moveDie1.setFillAfter(true);
moveDie2.setFillAfter(true);
moveDie1.setFillEnabled(true);
moveDie2.setFillEnabled(true);
animationListeners(); //Animation listeners
determineDiceClicked();
}
答案 0 :(得分:0)
默认情况下,翻译动画不会包含绝对值,但会接受要应用的更改量(增量)。因此,如果您的视图位于x = 1000且随机数达到800的高范围,那么它将移动到1800,这将为您屏幕关闭。因此,为了达到800以上,您需要考虑当前视图的位置以及将其移动到新位置需要进行多少更改:
moveDie1 = new TranslateAnimation(0, n1-moveDie1.getX(), 0, n2-moveDie1.getY());
变为800-1000,因此更改-200会将视图移动到x = 800,就像您想要的那样。
或者只是使用另一种采用动画类型的translationanimation构造函数,以便您可以使用绝对值。
public TranslateAnimation(int fromXType,float fromXValue,int toXType,float toXValue,int fromYType,float fromYValue,int toYType,float toYValue)
moveDie1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, n1, Animation.ABSOLUTE, n2);