11

ImageViewを定期的に変更するアクティビティがあります。そのために、以下のコード行を書きました。

imageview.setImageUri(resId);

リソース ID を増やしています。正常に動作しますが、ある画像から別の画像に突然遷移します。私はそれを望んでいません。画像ビューから別の画像へのスムーズな移行が必要です。どうやってやるの?

4

3 に答える 3

17

これを試して

ImageView demoImage = (ImageView) findViewById(R.id.DemoImage);
int imagesToShow[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3 };

animate(demoImage, imagesToShow, 0,false);  



  private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) {

  //imageView <-- The View which displays the images
  //images[] <-- Holds R references to the images to display
  //imageIndex <-- index of the first image to show in images[] 
  //forever <-- If equals true then after the last image it starts all over again with the first image resulting in an infinite loop. You have been warned.

    int fadeInDuration = 500; // Configure time values here
    int timeBetween = 3000;
    int fadeOutDuration = 1000;

    imageView.setVisibility(View.INVISIBLE);    //Visible or invisible by default - this will apply when the animation ends
    imageView.setImageResource(images[imageIndex]);

    Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
    fadeIn.setDuration(fadeInDuration);

    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
    fadeOut.setStartOffset(fadeInDuration + timeBetween);
    fadeOut.setDuration(fadeOutDuration);

    AnimationSet animation = new AnimationSet(false); // change to false
    animation.addAnimation(fadeIn);
    animation.addAnimation(fadeOut);
    animation.setRepeatCount(1);
    imageView.setAnimation(animation);

    animation.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation) {
            if (images.length - 1 > imageIndex) {
                animate(imageView, images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array
            }
            else {
                if (forever == true){
                animate(imageView, images, 0,forever);  //Calls itself to start the animation all over again in a loop if forever = true
                }
            }
        }
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
        }
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }
    });
}
于 2013-07-11T07:31:24.970 に答える
2

アルファ アニメーションを試してください。最初にイメージビューをフェードアウトし、アニメーションの最後でリソースを変更してから、イメージビューをフェードインします。

于 2013-07-11T07:25:33.470 に答える
1

スムーズに移行するには、Android でアニメーションを使用する必要があります。まず、次のリンクを読んでください: http://www.vogella.com/articles/AndroidAnimation/article.html

アニメーションに関するスタックオーバーフローに関する同様の質問が多数あり、このトピックに関する多くのチュートリアルがネット上で利用可能です。Googleで簡単に検索すると、大量の結果が得られます

于 2013-07-11T07:25:28.810 に答える