Android - 带淡化的动画ImageView

时间:2017-06-12 20:21:56

标签: android android-imageview android-transitions

我创建了以下方法来为单个ImageView中的多个图像设置动画。我的问题是图像过渡是突然的,所以我现在要做的是使图像 淡出 从一个到下一个。

那么我怎么能实现这一目标(不创建额外的图像)?

protected static void setAnimatedImagesForImageView(Context context, ImageView imageView, Uri[] imageUris, int durationMillis) {

    AnimationDrawable animation = new AnimationDrawable();
    boolean imageAdded = false;
    Drawable drawable;
    for (int i = 0; i < imageUris.length; i++) {
        if (imageUris[i] != null) {
            drawable = getDrawable(context, imageUris[i]);
            if (drawable != null) {
                animation.addFrame(drawable, durationMillis);
                imageAdded = true;
            }
        }
    }
    if (imageAdded) {
        animation.setOneShot(false);
        imageView.setImageDrawable(animation);
        animation.start();
    }

}

public static Drawable getDrawable(Context context, Uri drawableUri) {

    if (drawableUri == null) {
        return null;
    }
    else {
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(drawableUri);
            return Drawable.createFromStream(inputStream, drawableUri.toString());
        } catch (Exception e) {
            Log.e(LOG_TAG, "Could not get Drawable for Uri", e);
            return null;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

问题在于您将所有图像加载在一起并且不会在它们之间产生延迟以显示动画。将此方法放入您的班级并手动调用它以获取第一个图像&#34; loadImage(images [0]);&#34 ;,此处图片是包含所有图像的数组:

public void loadImage(int position) {

    Animation a = new AlphaAnimation(1.00f, 0.00f);

    a.setDuration(1000);
    a.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation animation) {

            // TODO Auto-generated method stub

        }

        public void onAnimationRepeat(Animation animation) {

            // TODO Auto-generated method stub

        }

        public void onAnimationEnd(Animation animation) {

            yourView.setBackgroundResource(images[position]);

            // Call your new created method again for your next image here.

            if(position < images.length() -1) {
                loadImage(position + 1);
            }

        }

    });

    yourView.startAnimation(a);

}

我希望它对你有用!

答案 1 :(得分:0)

我最终找到了一个简单的解决方案 - 添加:

/**
 * Find a user using the user identifier in the subject claim.
 *
 * @param bool|string $token
 *
 * @return mixed
 */

public function toUser($token = false)
{
    $payload = $this->getPayload($token);

    if (! $user = $this->user->getBy($this->identifier, $payload['sub'])) {
        return false;
    }

    return $user;
}

所以我的初始方法现在是:

animation.setEnterFadeDuration(fadeImageDurationMillis);
animation.setExitFadeDuration(fadeImageDurationMillis);