Автоматическое анимирование изображений с помощью Android с эффектом плавного перехода без нажатия кнопки мыши

У меня недостаточно знаний о том, как сделать автоматическую анимацию с помощью просмотра изображений. crossfading анимация без клика. Таким образом, я основал источник, который является imageview с анимацией затухания. Но для этого нужно нажать на изображение, чтобы вызвать анимацию изменения изображения.

Я хотел, чтобы изображение автоматически изменилось с crossfading анимация.

Мой код ниже:

private BitmapDrawable drawables[];
private int mCurrentDrawable = 0;
private int imagesToShow[] = { R.drawable.bg_carousel1,R.drawable.bg_carousel2,
        R.drawable.bg_carousel3, R.drawable.bg_carousel4, R.drawable.bg_carousel5, R.drawable.bg_carousel6,
        R.drawable.bg_carousel7, R.drawable.bg_carousel8};

 // This app works by having two views, which get faded in/out for the cross-fade effect
    final ImageView prevImageView = findViewById(R.id.prevImageView);
    final ImageView nextImageView = findViewById(R.id.nextImageView);
    prevImageView.setBackgroundColor(Color.TRANSPARENT);
    nextImageView.setBackgroundColor(Color.TRANSPARENT);

    // Setup default ViewPropertyAnimator durations for the two ImageViews
    prevImageView.animate().setDuration(1000);
    nextImageView.animate().setDuration(1000);

    // NOte that a real app would do this more robustly, and not just load all possible
    // bitmaps at onCreate() time.
    drawables = new BitmapDrawable[imagesToShow.length];
    for (int i = 0; i < imagesToShow.length; ++i) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                imagesToShow[i]);
        drawables[i] = new BitmapDrawable(getResources(), bitmap);
    }
    prevImageView.setImageDrawable(drawables[0]);
    nextImageView.setImageDrawable(drawables[1]);



    prevImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Use ViewPropertyAnimator to fade the previous imageView out and the next one in
            prevImageView.animate().alpha(0).withLayer();
            nextImageView.animate().alpha(1).withLayer().
                    withEndAction(new Runnable() {
                        // When the animation ends, set up references to change the prev/next
                        // associations
                        @Override
                        public void run() {
                            mCurrentDrawable =
                                    (mCurrentDrawable + 1) % drawables.length;
                            int nextDrawableIndex =
                                    (mCurrentDrawable + 1) % drawables.length;
                            prevImageView.setImageDrawable(drawables[mCurrentDrawable]);
                            nextImageView.setImageDrawable(drawables[nextDrawableIndex]);
                            nextImageView.setAlpha(0f);
                            prevImageView.setAlpha(1f);
                        }
                    });
        }
    });

0 ответов

Другие вопросы по тегам