Java Android缩放动画-反向问题

Java Android缩放动画-反向问题,java,android,android-intent,mobile,android-animation,Java,Android,Android Intent,Mobile,Android Animation,我设法给我的图像添加了一个缩放动画,使它从原来的大小增长到更大的大小。但是,我需要添加另一个动画代码副本,使其缩放回“原始大小”。当布尔值为真时,我尝试循环动画。 我对参数进行了一些调整,但无法使其正常工作。以下是我目前的代码: class AnimateButton extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params

我设法给我的图像添加了一个缩放动画,使它从原来的大小增长到更大的大小。但是,我需要添加另一个动画代码副本,使其缩放回“原始大小”。当布尔值为真时,我尝试循环动画。

我对参数进行了一些调整,但无法使其正常工作。以下是我目前的代码:

class AnimateButton extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            Boolean isGlowing = true; //Make it run forever
            while (isGlowing) {
                scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
                scal_grow.setDuration(1500);
                scal_grow.setFillAfter(true);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btn_layer.setAnimation(scal_grow);
                    }
                });

                try {
                    Thread.sleep(1500);
                } catch (Exception e) { }

                     //Add a reverse animation such that it goes back to the original size

            }
        return null;
     }
}
class AnimateButton扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
Boolean isGlowing=true;//使其永远运行
当(发光){
scal_grow=新的缩放动画(0,1.2f,0,1.2f,Animation.RELATIVE_TO_SELF,(float)0.5,Animation.RELATIVE_TO_SELF,(float)0.5);
鳞片生长设定持续时间(1500);
scal_grow.setFillAfter(true);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
btn_layer.setAnimation(scal_grow);
}
});
试一试{
睡眠(1500);
}捕获(例外e){}
//添加反向动画,使其恢复到原始大小
}
返回null;
}
}

我应该做些什么改变呢?

要取消缩放,您可以使用以下方法:

 ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
 unscal_grow .setDuration(1500);
 unscal_grow .setFillAfter(true);
缩放和取消缩放的方法:

 private void scaleAndUnscale() {
    ScaleAnimation scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
    scal_grow.setDuration(1500);
    scal_grow.setFillAfter(true);

    image1.startAnimation(scal_grow);

    scal_grow.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
            unscal_grow.setDuration(1500);
            unscal_grow.setStartOffset(1500);
            unscal_grow.setFillAfter(true);

            image1.startAnimation(unscal_grow);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

我绝对建议不要使用遗留的
android.view.animation
动画,而是使用。旧的视图动画不是很好,并且不像您期望的那样在视图上工作

使用属性动画,这个脉冲动画非常简单。您可以用XML对其进行如下定义:

<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:valueFrom="1"
    android:valueTo="1.2"
    android:valueType="floatType"
    android:repeatCount="infinite"
    android:repeatMode="reverse"/>

这将给您带来更多的预期体验,并为您处理线程和计时。当您想停止动画时,只需调用
s.cancel()
,就可以了。

在android中,除了UIThread(主线程)之外,任何其他线程都不能进行动画和UI更新。
删除AsyncTask并尝试使用ViewPropertyImator,它在性能方面优于ScaleImation。另外,这只是一条线

按比例:

btn_layer.animate().scaleX(1.2f).scaleY(1.2f).setDuration(1500).start();
要取消销售:

btn_layer.animate().scaleX(0.8f).scaleY(0.8f).setDuration(1500).start();
更新

PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();


您正在创建一个异步任务来设置按钮的动画吗?是的。为了确保它在后台运行@Ramijemli似乎没有取消缩放,mateI检查了它,它工作了。关于睡眠,我不认为你对AsyncTask动画的解决方案是好的。您可能希望将处理程序与postDelayed一起使用,因此它将在UI线程上运行,无需睡眠。实际上,如果假设它只运行一次,则更简单的解决方案是在OnAnimationEnd中运行非缩放动画。我希望它在布尔值为真时运行。我已经说过了,伙计:)能给我你试过的完整解决方案吗?我似乎无法让它工作我已经添加了一种方法来实现不缩放的缩放这做动画,但它收缩和缩放到右侧。基本上不是我心目中的动画,伙计。不过,我会试着绕着它转一圈。视图上设置的重力是多少?如果是右重力或右重力,或者在布局中将其对齐,因为这实际上改变了视图的属性,视图动画只改变绘制的内容(即,触摸目标将被关闭)@rharter这可以使用一个ObjectAnimator实现。不确定你的意思,@RamiJemli,在我的示例中,我使用的是ObjectAnimator。对不起,我指的是ObjectAnimator的一个实例。不需要两行和一个动画集。我一行接一行地使用了这两行,我看不到按钮的变化,尽管你确定?我百分之百肯定它们能起作用。第一个是缩放,第二个是取消缩放。在取消缩放之前,您可能需要等待,但不要同时执行此操作。何时要取消缩放按钮?只需在大约1.5秒的总时间跨度内进行缩放和取消缩放的平滑动画
PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();