0

我正在尝试使用 Android 和图像识别 API 创建在线扫描仪。我正在尝试使用动画触发器创建漂亮的布局。它看起来像这样

我目前拥有一切,它运行良好,但有一个例外:动画。这个蓝色触发器的动画是可以的,但是当 AsyncTask 正在向 API 发布照片时,激光的动画会暂停一段时间。我应该怎么做才能防止这种行为?

这是我的动画代码:

final View laser = findViewById(R.id.laser);
laser.setDrawingCacheEnabled(true);

final View trigger = findViewById(R.id.trigger);
Animation fadeAnimation = new AlphaAnimation(1.0f, 0.5f);
fadeAnimation.setRepeatCount(Animation.INFINITE);
fadeAnimation.setRepeatMode(Animation.REVERSE);
fadeAnimation.setDuration(3000);
trigger.setDrawingCacheEnabled(true);
trigger.startAnimation(fadeAnimation);

trigger.post(new Runnable() {
    @Override
    public void run() {
        final Animation translateDownAnimation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.25f,
            Animation.RELATIVE_TO_PARENT, 0.75f);
        final Animation translateUpAnimation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.75f,
            Animation.RELATIVE_TO_PARENT, 0.25f);
        translateDownAnimation.setDuration(ANIMATION_TIME);
        translateDownAnimation.setRepeatCount(0);
        translateDownAnimation.setRepeatMode(Animation.RESTART);
        translateUpAnimation.setDuration(ANIMATION_TIME);
        translateUpAnimation.setRepeatCount(0);
        translateUpAnimation.setRepeatMode(Animation.RESTART);
        translateDownAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
            translateUpAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        translateDownAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                laser.setBackgroundResource(R.drawable.scanner_line_down);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                laser.startAnimation(translateUpAnimation);
            }
        });
        translateUpAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                laser.setBackgroundResource(R.drawable.scanner_line_up);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                laser.startAnimation(translateDownAnimation);
            }
        });
        laser.setVisibility(View.VISIBLE);
        laser.startAnimation(translateDownAnimation);
    }
});

如果有帮助,我可以准备适当的 .apk 文件。

4

0 回答 0