Search code examples
androidimageswitcher

ImageSwitcher image won't display onCreate()


I can't figure out why my ImageSwitcher won't display the image when the Activity first loads. I've set the image resource in the onCreate method but the image seems to be invisible until I start the animation. The animation should slide one image out and a second image in. When I click the button to start the animation, I can see the original image slide out from where it should be but I can't see it at the start. I've tried setting the image within the ViewFactory and just outside but it doesn't seem to work

Here's my imageSwitcher code:

mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
    mImageSwitcher.setOnClickListener(mImageSwitcherClickListener);

    mImageSwitcher.setFactory(new ViewFactory() {
        @Override
        public View makeView() {
            ImageView view = new ImageView(getApplicationContext());
            view.setScaleType(ImageView.ScaleType.CENTER);
            view.setLayoutParams(new
                    ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            view.setImageResource(R.mipmap.image);
            return view;
        }
    });
    mImageSwitcher.setImageResource(R.mipmap.image);

Here is the layout for the Activity:

<ImageSwitcher
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageSwitcher"
    android:layout_centerHorizontal="true"
    android:layout_gravity="bottom|center"
    android:alpha=".5"
    android:scaleX="4"
    android:scaleY="4"
    android:layout_marginBottom="150dp"
    android:animateFirstView="true"/>

Solution

  • In tutorials it shows you have to use animation

    try this

    Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
    imageSwitcher.setInAnimation(in);
    imageSwitcher.setOutAnimation(out); 
    

    reference from this link