如何在不从屏幕退出的情况下为对象设置动画

时间:2017-04-12 22:07:40

标签: android

我想为这样的对象设置动画: 现在我的问题是:如何测量(b)在不退出屏幕的情况下为我的对象(o)设置动画?

对象o;
nextX = random.nextInt(b);

ObjectAnimator a1;

a1 = ObjectAnimator.oFFloat(o,“x”,nextX)

1 个答案:

答案 0 :(得分:0)

您可以获得屏幕高度和宽度

 public static int getScreenHeight(Context ctx) {
        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }

    public static int getScreenWidth(Context ctx) {
        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
        return metrics.widthPixels;
    }

并像这样使用

Random randX = random.nextInt(getScreenWidth(ctx));
Random randY = random.nextInt(getScreenheight(ctx));

ObjectAnimator moveX = ObjectAnimator.ofFloat(object, "x", randX );
ObjectAnimator moveY = ObjectAnimator.ofFloat(object, "y", randY );
相关问题